前端开发入门到精通的在线学习网站

网站首页 > 资源文章 正文

docker-compose从入门到放弃(docker-compose init)

qiguaw 2024-08-29 11:21:42 资源文章 24 ℃ 0 评论

1.1、简介

Docker Compose 是 Docker 官方编排(Orchestration)项目之一,负责快速的部署分布式应用。轻松高效的管理容器,定义运行多个容器。面向项目进行管理。

1.2、下载

curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.5/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

1.3、授权

$ sudo chmod +x /usr/local/bin/docker-compose

1.4、创建软连接

sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

1.5、是否安装成功

docker-compose --version
[root@VM-0-5-centos ~]# docker-compose --version
docker-compose version 1.25.5, build 8a1c60f6

1.6、卸载

sudo rm /usr/local/bin/docker-compose

1.2、常用命令

? docker-compose --help
Define and run multi-container applications with Docker.

Usage:
docker-compose [-f <arg>...] [options] [--] [COMMAND] [ARGS...]
docker-compose -h|--help

Options:
-f, --file FILE             Specify an alternate compose file
                            (default: docker-compose.yml)
-p, --project-name NAME     Specify an alternate project name
                            (default: directory name)
-c, --context NAME         Specify a context name
--verbose                   Show more output
--log-level LEVEL           Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
--no-ansi                   Do not print ANSI control characters
-v, --version               Print version and exit
-H, --host HOST             Daemon socket to connect to

--tls                       Use TLS; implied by --tlsverify
--tlscacert CA_PATH         Trust certs signed only by this CA
--tlscert CLIENT_CERT_PATH Path to TLS certificate file
--tlskey TLS_KEY_PATH       Path to TLS key file
--tlsverify                 Use TLS and verify the remote
--skip-hostname-check       Don't check the daemon's hostname against the
                            name specified in the client certificate
--project-directory PATH   Specify an alternate working directory
                            (default: the path of the Compose file)
--compatibility             If set, Compose will attempt to convert keys
                            in v3 files to their non-Swarm equivalent (DEPRECATED)
--env-file PATH             Specify an alternate environment file

Commands:
build             Build or rebuild services #构建或重新构建服务
config             Validate and view the Compose file
create             Create services
down               Stop and remove containers, networks, images, and volumes #停止服务并且移除容器,网络配置
events             Receive real time events from containers # 从容器中接收实时事件
exec               Execute a command in a running container
help               Get help on a command
images             List images
kill               Kill containers
logs               View output from containers
pause             Pause services
port               Print the public port for a port binding
ps                 List containers
pull               Pull service images
push               Push service images
restart           Restart services
rm                 Remove stopped containers
run               Run a one-off command
scale             Set number of containers for a service
start             Start services
stop               Stop services
top               Display the running processes
unpause           Unpause services
up                 Create and start containers
version           Show version information and quit
                                                                               
~   

已安装nginx为例,下面是测试yml

version: '3'
services: 
nginx:
  restart: always
  privileged: true
  image: nginx:1.19.6-alpine
  ports: 
    - 8080:80
    - 443:443
  volumes: 
    - /home/blog/conf/nginx.conf:/etc/nginx/nginx.conf 
    - /home/blog/public:/usr/share/nginx/html
    - /home/blog/ssl:/etc/nginx/cert 
    - /home/blog/logs:/var/log/nginx

1.2.1、查看版本

docker-compose version

[root@VM-0-5-centos blog-compose]# docker-compose version
docker-compose version 1.25.5, build 8a1c60f6
docker-py version: 4.1.0 
CPython version: 3.7.5
OpenSSL version: OpenSSL 1.1.0l 10 Sep 2019

1.2.2、创建或重新构建项目中的服务容器

#在yml文件目录下面运行
docker-compose build
[root@VM-0-5-centos blog-compose]# docker-compose build
nginx uses an image, skipping

1.2.3、校验compose文件格式是否正确,正确显示配置,不正确显示错误原因

# 正确示例,volumes后面的rw表示读写权限
[root@VM-0-5-centos blog-compose]# docker-compose config
services:
nginx:
  image: nginx:1.19.6-alpine
  ports:
  - 8080:80/tcp
  - 443:443/tcp
  privileged: true
  restart: always
  volumes:
  - /home/blog/conf/nginx.conf:/etc/nginx/nginx.conf:rw               
  - /home/blog/public:/usr/share/nginx/html:rw
  - /home/blog/ssl:/etc/nginx/cert:rw
  - /home/blog/logs:/var/log/nginx:rw
version: '3.0'

我们将yml故意写错误,改成如下

[root@VM-0-5-centos blog-compose]# cat docker-compose.yml
version: '3'
services: 
nginx:
  restart: always
  privileged: true
  image: nginx:1.19.6-alpine
  ports: 
    8080:79                                                               #这故意错
    - 443:443
  volumes: 
    - /home/blog/conf/nginx.conf:/etc/nginx/nginx.conf 
    - /home/blog/public:/usr/share/nginx/html
    - /home/blog/ssl:/etc/nginx/cert 
    - /home/blog/logs:/var/log/nginx


#校验yml是否正确,发现确实给我们提示了对应的错误,漂亮啊
[root@VM-0-5-centos blog-compose]# docker-compose config
ERROR: The Compose file './docker-compose.yml' is invalid because:
services.nginx.ports contains an invalid type, it should be an array

1.2.4、基于docker-compose.yml启动管理的容器

# 以后台的方式运行容器。不会在终端上打印运行日志
docker-compose up -d


[root@VM-0-5-centos blog-compose]# docker-compose up -d
WARNING: The Docker Engine you're using is running in swarm mode.

Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node.

To deploy your application across the swarm, use `docker stack deploy`.

Creating network "blog-compose_default" with the default driver
Creating blog-compose_nginx_1 ... done

1.2.5、将会停止 up 命令所启动的容器,并移除网络。关闭并删除容器

# docker-compose down
[root@VM-0-5-centos blog-compose]# docker-compose down
Stopping blog-compose_nginx_1 ... done
Removing blog-compose_nginx_1 ... done
Removing network blog-compose_default

1.2.6、开启|关闭|重启已经存在的由docker-compose维护的容器

docker-compose start|stop|restart

1.2.7、查看由docker-compose管理的容器

# docker-compose ps 列出项目中目前的所有容器
[root@VM-0-5-centos blog-compose]# docker-compose ps
      Name                     Command               State                     Ports                   
----------------------------------------------------------------------------------------------------------
blog-compose_nginx_1   /docker-entrypoint.sh ngin ...   Up     0.0.0.0:443->443/tcp, 0.0.0.0:8080->80/tcp

# 只显示容器的 ID 信息
[root@VM-0-5-centos blog-compose]# docker-compose ps -q
112dc10af750ba9d9a24c5a10483f956e198e9b219176d8222960780e29b2947

1.2.8、查看服务容器日志

[root@VM-0-5-centos blog-compose]# docker-compose logs --help
View output from containers.

Usage: logs [options] [SERVICE...]
# 默认会取最新的日志
Options:
  --no-color         Produce monochrome output.                                                 #去除颜色输出,默认是有颜色的,用于区分不同的服务器
  -f, --follow       Follow log output.                                                                 #
  -t, --timestamps   Show timestamps.                                                                       # 显示日志时间    
  --tail="all"       Number of lines to show from the end of the logs       # 指定输入日志行数
                      for each container.
# 通过--tail控制输出日志的行数
[root@VM-0-5-centos blog-compose]# docker-compose logs --tail=2
Attaching to blog-compose_nginx_1
nginx_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginx_1 | /docker-entrypoint.sh: Configuration complete; ready for start up
# 会显示日志的时间格式
[root@VM-0-5-centos blog-compose]# docker-compose logs -t --tail=2
Attaching to blog-compose_nginx_1
nginx_1 | 2021-01-29T14:15:49.998633632Z /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginx_1 | 2021-01-29T14:15:50.005081422Z /docker-entrypoint.sh: Configuration complete; ready for start up

1.2.9、docker-compose images列出 Compose 文件中包含的镜像。

[root@VM-0-5-centos blog-compose]# docker-compose images
    Container         Repository       Tag         Image Id       Size  
---------------------------------------------------------------------------
blog-compose_nginx_1   nginx       1.19.6-alpine   629df02b47c8   22.34 MB

1.2.10、docker-compose scale 设置指定服务运行的容器个数。通过service=num的参数来设置数量

[root@VM-0-5-centos blog-compose]# docker-compose up -d --scale nginx=5
WARNING: The Docker Engine you're using is running in swarm mode.

Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node.

To deploy your application across the swarm, use `docker stack deploy`.

Creating network "blog-compose_default" with the default driver
Creating blog-compose_nginx_1 ... done
Creating blog-compose_nginx_2 ... done
Creating blog-compose_nginx_3 ... done
Creating blog-compose_nginx_4 ... done
Creating blog-compose_nginx_5 ... done

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表