九、Docker单机编排Compose
九、Docker单机编排Compose

九、Docker单机编排Compose

9.1 Docker Compose介绍

当在宿主机启动较多的容器的时候,如果手动操作会觉得比较麻烦而且容易出错,此时推荐使用docker单机编排工具docker-compose

docker-compose是docker容器的一种单机编排服务,docker-compose是一个管理多个容器的工具,比如:可以解决容器之间的依赖关系,就像启动一个nginx前端服务的时候调用后端的tomcat,那就得先启动tomcat,但是启动tomcat容器还需要依赖数据库,那还得先启动数据库,docker-compose可以用来解决这样的嵌套依赖关系,并且可以替代docker命令对容器进行创建、启动和停止等手工的操作

因此,如果说docker命令就像linux的命令,docker-compose就像shell脚本,可以自动化执行容器批量操作,从而实现自动化的容器管理,或者说docker命令相当于ansible命令,那个docker-compose文件,就相当于ansible-playbook的yaml文件

docker-compose项目是Docker官方的开源项目,负责实现对Dokcer容器集群的快速编排,docker-compose将所管理的容器分为三层,分别是工程(project),服务(service)以及容器(container)

github地址:https://github.com/docker/compose

官方地址:https://docs.docker.com/compose

9.2 安装和准备

9.2.1 安装docker-compose

9.2.1.1 方法1:在线安装,通过在线pip安装

python-pip包将安装一个pip的命令,pip命令是一个python安装包的安装工具,其类似于ubuntu的apt或者redhat的yum,但是pip只安装python相关的安装包,可以在多种操作系统安装和使用pip

此方式当前安装的版本较新,为docker-compose-1.29.2,推荐使用

pip仓库换源

root@ubuntu-18:~# mkdir .pip
root@ubuntu-18:~# cat .pip/pip.conf
[global]
timeout = 1
index-url = http://mirrors.aliyun.com/pypi/simple/
index-index-url = http://pypi.douban.com/simple/
[install]
trusted-host =
  mirrors.aliyun.com
  pypi.douban.com
#centos,依赖epel源
[root@centos8-2 ~]# yum -y install epel-release
[root@centos8-2 ~]# yum -y install python3-pip
[root@centos8-2 ~]# pip3 install --upgrade pip
[root@centos8-2 ~]# pip3 install docker-compose

#ubuntu安装
root@ubuntu-18:~# apt update
root@ubuntu-18:~# apt -y install python3-pip
root@ubuntu-18:~# pip3 install --upgrade pip
root@ubuntu-18:~# pip3 install docker-compose

9.2.1.2 方法二:离线安装,直接从github下载对应版本

参考说明:https://github.com/docker/compose/releases

此方法安装版本方便指定,推荐使用,但网络下载较慢

[root@centos8-1 ~]# wget https://github.com/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` -O /usr/bin/docker-compose
[root@centos8-1 ~]# chmod +x /usr/bin/docker-compose

9.2.3 查看命令格式

官方文档:https://docs.docker.com/compose/reference/

docker-compose --help
Define and run multi-container applications with Docker.
Usage:
docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
docker-compose -h|--help
#选项说明:
-f,–file FILE                   #指定Compose 模板文件,默认为docker-compose.yml
-p,–project-name NAME           #指定项目名称,默认将使用当前所在目录名称作为项目名。
--verbose                         #显示更多输出信息
--log-level LEVEL                 #定义日志级别 (DEBUG, INFO, WARNING, ERROR, CRITICAL)
--no-ansi                         #不显示ANSI 控制字符
-v, --version                     #显示版本

#以下为命令选项,需要在docker-compose.yml|yaml 文件所在在目录里执行
config -q                         #查看当前配置,没有错误不输出任何信息
up                                #创建并启动容器
build                             #构建镜像
bundle                            #从当前docker compose 文件生成一个以<当前目录>为名称的json格式的Docker Bundle 备份文件
create                            #创建服务
down                              #停止和删除所有容器、网络、镜像和卷
events                            #从容器接收实时事件,可以指定json 日志格式
exec                              #进入指定容器进行操作
help                              #显示帮助细信息
images                            #显示镜像信息
kill                              #强制终止运行中的容器
logs                              #查看容器的日志
pause                             #暂停服务
port                              #查看端口
ps                                #列出容器
pull                              #重新拉取镜像,镜像发生变化后,需要重新拉取镜像
push                              #上传镜像
restart                           #重启服务
rm                                #删除已经停止的服务
run                               #一次性运行容器
scale                             #设置指定服务运行的容器个数
start                             #启动服务
stop                              #停止服务
top                               #显示容器运行状态
unpause                           #取消暂定

9.2.3 docker compose 文件格式

官方文档:https://docs.docker.com/compose/compose-file/

docker-compose 文件是一个yaml格式的文件,所以注意行首的缩进很严格

默认docker-compose命令会调用当前目录下的docker-compose.yml的文件,因此一般执行docker-compose命令前先就入docker-compose.yml文件所在目录

docker-compose文件的格式有很多版本,根据版本的不同,语法和格式都有所不同,参考一下列表

Compose file formatDocker Engine release
3.718.06.0+
3.618.02.0+
3.517.12.0+
3.417.09.0+
3.317.06.0+
3.217.04.0+
3.11.13.1+
3.01.13.0+
2.417.12.0+
2.317.06.0+
2.21.13.0+
2.11.12.0+
2.01.10.0+
1.01.9.1.+

docker-compose版本众多,以下通过具体示例说明docker-compose的使用方法

9.3 从docker compose 启动容器

注意:使用Docker compose之前,先要安装docker

9.3.1 创建docker compose 文件

docker compose文件可在任意目录,创建文件名docker-compose.yml 配置文件,注意前后的缩进

[root@centos8-1 docker-compose]# cat docker-compose.yml 
service-nginx-web:
  image: nginx
  container_name: nginx-web
  expose: 
    - 80
    - 443
  ports: 
    - "80:80"
    - "443:443"
service-httpd-web:
  image: httpd
  container_name: httpd-web
  expose: 
    - 80
    - 443
  ports: 
    - "81:80"
    - "8443:443"

9.3.2 查看配置和格式检查

[root@centos8-1 docker-compose]# docker-compose config
services:
  service-httpd-web:
    container_name: httpd-web
    expose:
    - 80
    - 443
    image: httpd
    network_mode: bridge
    ports:
    - 81:80/tcp
    - 8443:443/tcp
  service-nginx-web:
    container_name: nginx-web
    expose:
    - 80
    - 443
    image: nginx
    network_mode: bridge
    ports:
    - 80:80/tcp
    - 443:443/tcp
version: '1'

[root@centos8-1 docker-compose]# docker-compose config -q


#改错docker-compose文件格式
[root@centos8-1 docker-compose]# cat docker-compose.yml
service-nginx-web    #删除了:
  image: nginx
  container_name: nginx-web
  expose: 
    - 80
    - 443
  ports: 
    - "80:80"
    - "443:443"
service-httpd-web:
  image: httpd
  container_name: httpd-web
  expose: 
    - 80
    - 443
  ports: 
    - "81:80"
    - "8443:443"
 
[root@centos8-1 docker-compose]# docker-compose config -q
ERROR: yaml.scanner.ScannerError: mapping values are not allowed here
  in "./docker-compose.yml", line 2, column 8

9.3.3 启动容器

注意:必须要在docker compose文件所在目录执行

#前台启动
[root@centos8-1 docker-compose]# docker-compose up
Pulling service-nginx-web (nginx:)...
latest: Pulling from library/nginx
a330b6cecb98: Pull complete
e0ad2c0621bc: Pull complete
9e56c3e0e6b7: Pull complete
09f31c94adc6: Pull complete
32b26e9cdb83: Pull complete
20ab512bbb07: Pull complete
Digest: sha256:853b221d3341add7aaadf5f81dd088ea943ab9c918766e295321294b035f3f3e
Status: Downloaded newer image for nginx:latest
Pulling service-httpd-web (httpd:)...
latest: Pulling from library/httpd
a330b6cecb98: Already exists
14e3dd65f04d: Pull complete
fe59ad2e7efe: Pull complete
68eb42ff9345: Pull complete
9d5052bb82be: Pull complete
Digest: sha256:ead5178e79caa09343750bb03ff98e87ed57c537f2ae12685beb3a573cce8f55
Status: Downloaded newer image for httpd:latest
Creating nginx-web ... done
Creating httpd-web ... done
Attaching to nginx-web, httpd-web
nginx-web            | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx-web            | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
nginx-web            | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
nginx-web            | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
nginx-web            | 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
nginx-web            | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginx-web            | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
nginx-web            | /docker-entrypoint.sh: Configuration complete; ready for start up
nginx-web            | 2021/09/23 08:29:25 [notice] 1#1: using the "epoll" event method
nginx-web            | 2021/09/23 08:29:25 [notice] 1#1: nginx/1.21.3
nginx-web            | 2021/09/23 08:29:25 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6) 
nginx-web            | 2021/09/23 08:29:25 [notice] 1#1: OS: Linux 4.18.0-240.el8.x86_64
nginx-web            | 2021/09/23 08:29:25 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
nginx-web            | 2021/09/23 08:29:25 [notice] 1#1: start worker processes
nginx-web            | 2021/09/23 08:29:25 [notice] 1#1: start worker process 30
nginx-web            | 2021/09/23 08:29:25 [notice] 1#1: start worker process 31
nginx-web            | 2021/09/23 08:29:25 [notice] 1#1: start worker process 32
nginx-web            | 2021/09/23 08:29:25 [notice] 1#1: start worker process 33
httpd-web            | AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
httpd-web            | AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
httpd-web            | [Thu Sep 23 08:29:25.864464 2021] [mpm_event:notice] [pid 1:tid 139903936455808] AH00489: Apache/2.4.49 (Unix) configured -- resuming normal operations
httpd-web            | [Thu Sep 23 08:29:25.864629 2021] [core:notice] [pid 1:tid 139903936455808] AH00094: Command line: 'httpd -D FOREGROUND'
nginx-web            | 172.17.0.1 - - [23/Sep/2021:08:29:49 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.61.1" "-"
httpd-web            | 172.17.0.1 - - [23/Sep/2021:08:29:52 +0000] "GET / HTTP/1.1" 200 45

#以上是前台执行不退出

5.3.4 验证docker-compose执行结果

#上面命令是前台执行,因此,要查看结果,需要在开一个终端进行观察
[root@centos8-1 docker-compose]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                                                        NAMES
b598fe37e07f   httpd     "httpd-foreground"       11 seconds ago   Up 10 seconds   0.0.0.0:81->80/tcp, :::81->80/tcp, 0.0.0.0:8443->443/tcp, :::8443->443/tcp   httpd-web
e017596611a7   nginx     "/docker-entrypoint.…"   11 seconds ago   Up 10 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp     nginx-web

#必须要跟docker-compose.yml在同一个目录
[root@centos8-1 ~]# docker-compose ps
ERROR: 
        Can't find a suitable configuration file in this directory or any
        parent. Are you in the right directory?

        Supported filenames: docker-compose.yml, docker-compose.yaml, compose.yml, compose.yaml
 

[root@centos8-1 docker-compose]# docker-compose ps
  Name                 Command               State                                   Ports                                
--------------------------------------------------------------------------------------------------------------------------
httpd-web   httpd-foreground                 Up      0.0.0.0:8443->443/tcp,:::8443->443/tcp,                              
                                                     0.0.0.0:81->80/tcp,:::81->80/tcp                                     
nginx-web   /docker-entrypoint.sh ngin ...   Up      0.0.0.0:443->443/tcp,:::443->443/tcp,                                
                                                     0.0.0.0:80->80/tcp,:::80->80/tcp

[root@centos8-1 docker-compose]# curl localhost -I
HTTP/1.1 200 OK
Server: nginx/1.21.3
Date: Thu, 23 Sep 2021 08:35:17 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 07 Sep 2021 15:21:03 GMT
Connection: keep-alive
ETag: "6137835f-267"
Accept-Ranges: bytes

[root@centos8-1 docker-compose]# curl localhost:81 -I
HTTP/1.1 200 OK
Date: Thu, 23 Sep 2021 08:35:21 GMT
Server: Apache/2.4.49 (Unix)
Last-Modified: Mon, 11 Jun 2007 18:53:14 GMT
ETag: "2d-432a5e4a73a80"
Accept-Ranges: bytes
Content-Length: 45
Content-Type: text/html

9.3.5 结束前台执行

使用Ctrl+C即可结束前台执行

^CGracefully stopping... (press Ctrl+C again to force)
Stopping httpd-web ... done
Stopping nginx-web ... done


[root@centos8-1 docker-compose]# docker-compose ps
  Name                 Command               State    Ports
-----------------------------------------------------------
httpd-web   httpd-foreground                 Exit 0        
nginx-web   /docker-entrypoint.sh ngin ...   Exit 0        
[root@centos8-1 docker-compose]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS                      PORTS     NAMES
b598fe37e07f   httpd     "httpd-foreground"       8 minutes ago   Exited (0) 47 seconds ago             httpd-web
e017596611a7   nginx     "/docker-entrypoint.…"   8 minutes ago   Exited (0) 48 seconds ago             nginx-web

#通过dokcer-compose再运行容器
[root@centos8-1 docker-compose]# docker-compose start
Starting service-nginx-web ... done
Starting service-httpd-web ... done
[root@centos8-1 docker-compose]# docker-compose ps
  Name                 Command               State                                   Ports                                
--------------------------------------------------------------------------------------------------------------------------
httpd-web   httpd-foreground                 Up      0.0.0.0:8443->443/tcp,:::8443->443/tcp,                              
                                                     0.0.0.0:81->80/tcp,:::81->80/tcp                                     
nginx-web   /docker-entrypoint.sh ngin ...   Up      0.0.0.0:443->443/tcp,:::443->443/tcp,                                
                                                     0.0.0.0:80->80/tcp,:::80->80/tcp

#关闭容器
[root@centos8-1 docker-compose]# docker-compose kill
Killing httpd-web ... done
Killing nginx-web ... done
[root@centos8-1 docker-compose]# docker-compose ps
  Name                 Command                State     Ports
-------------------------------------------------------------
httpd-web   httpd-foreground                 Exit 137        
nginx-web   /docker-entrypoint.sh ngin ...   Exit 137

9.3.6 后台执行

[root@centos8-1 docker-compose]# docker-compose up -d
Creating httpd-web ... done
Creating nginx-web ... done

[root@centos8-1 docker-compose]# curl localhost -I
HTTP/1.1 200 OK
Server: nginx/1.21.3
Date: Thu, 23 Sep 2021 08:46:03 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 07 Sep 2021 15:21:03 GMT
Connection: keep-alive
ETag: "6137835f-267"
Accept-Ranges: bytes

[root@centos8-1 docker-compose]# curl localhost:81 -I
HTTP/1.1 200 OK
Date: Thu, 23 Sep 2021 08:46:07 GMT
Server: Apache/2.4.49 (Unix)
Last-Modified: Mon, 11 Jun 2007 18:53:14 GMT
ETag: "2d-432a5e4a73a80"
Accept-Ranges: bytes
Content-Length: 45
Content-Type: text/html

9.3.7 删除容器

#只删除停止的容器
[root@centos8-1 docker-compose]# docker-compose rm
Going to remove httpd-web, nginx-web
Are you sure? [yN] y
Removing httpd-web ... done
Removing nginx-web ... done

[root@centos8-1 docker-compose]# docker-compose up -d
Creating httpd-web ... done
Creating nginx-web ... done
[root@centos8-1 docker-compose]# docker-compose ps
  Name                 Command               State                                   Ports                                
--------------------------------------------------------------------------------------------------------------------------
httpd-web   httpd-foreground                 Up      0.0.0.0:8443->443/tcp,:::8443->443/tcp,                              
                                                     0.0.0.0:81->80/tcp,:::81->80/tcp                                     
nginx-web   /docker-entrypoint.sh ngin ...   Up      0.0.0.0:443->443/tcp,:::443->443/tcp,                                
                                                     0.0.0.0:80->80/tcp,:::80->80/tcp
[root@centos8-1 docker-compose]# docker-compose rm
No stopped containers

#停止并删除容器
[root@centos8-1 docker-compose]# docker-compose down
Stopping nginx-web ... done
Stopping httpd-web ... done
Removing nginx-web ... done
Removing httpd-web ... done
[root@centos8-1 docker-compose]# docker-compose ps
Name   Command   State   Ports
------------------------------
[root@centos8-1 docker-compose]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

#也会自动删除镜像
[root@centos8-1 docker-compose]# docker-compose images
Container   Repository   Tag   Image Id   Size
----------------------------------------------

9.3.8 停止和启动与日志查看

[root@centos8-1 docker-compose]# docker-compose stop
Stopping httpd-web ... done
Stopping nginx-web ... done
[root@centos8-1 docker-compose]# docker-compose ps
  Name                 Command               State    Ports
-----------------------------------------------------------
httpd-web   httpd-foreground                 Exit 0        
nginx-web   /docker-entrypoint.sh ngin ...   Exit 0        
[root@centos8-1 docker-compose]# docker-compose start
Starting service-nginx-web ... done
Starting service-httpd-web ... done
[root@centos8-1 docker-compose]# docker-compose ps
  Name                 Command               State                                   Ports                                
--------------------------------------------------------------------------------------------------------------------------
httpd-web   httpd-foreground                 Up      0.0.0.0:8443->443/tcp,:::8443->443/tcp,                              
                                                     0.0.0.0:81->80/tcp,:::81->80/tcp                                     
nginx-web   /docker-entrypoint.sh ngin ...   Up      0.0.0.0:443->443/tcp,:::443->443/tcp,                                
                                                     0.0.0.0:80->80/tcp,:::80->80/tcp

#执行上面操作时,同时在另一个终端,观察日志事件
[root@centos8-1 docker-compose]# docker-compose events
2021-09-23 16:48:14.511244 container kill 42675c00c4c9083c54821dfdf868112d714f73f3a8fdd2e1a87ac26a119963a4 (image=httpd, name=httpd-web, signal=28)
2021-09-23 16:48:14.511297 container kill 60dcd8b03365639ada1cd0aa4ca81aa42ba5dcf7a5e3110d96df13e690cbf0ea (image=nginx, maintainer=NGINX Docker Maintainers <docker-maint@nginx.com>, name=nginx-web, signal=3)
2021-09-23 16:48:14.547278 container die 60dcd8b03365639ada1cd0aa4ca81aa42ba5dcf7a5e3110d96df13e690cbf0ea (exitCode=0, image=nginx, maintainer=NGINX Docker Maintainers <docker-maint@nginx.com>, name=nginx-web)
2021-09-23 16:48:14.631230 container stop 60dcd8b03365639ada1cd0aa4ca81aa42ba5dcf7a5e3110d96df13e690cbf0ea (image=nginx, maintainer=NGINX Docker Maintainers <docker-maint@nginx.com>, name=nginx-web)
2021-09-23 16:48:15.555729 container die 42675c00c4c9083c54821dfdf868112d714f73f3a8fdd2e1a87ac26a119963a4 (exitCode=0, image=httpd, name=httpd-web)
2021-09-23 16:48:15.653184 container stop 42675c00c4c9083c54821dfdf868112d714f73f3a8fdd2e1a87ac26a119963a4 (image=httpd, name=httpd-web)
2021-09-23 16:48:36.028033 container start 42675c00c4c9083c54821dfdf868112d714f73f3a8fdd2e1a87ac26a119963a4 (image=httpd, name=httpd-web)
2021-09-23 16:48:36.045617 container start 60dcd8b03365639ada1cd0aa4ca81aa42ba5dcf7a5e3110d96df13e690cbf0ea (image=nginx, maintainer=NGINX Docker Maintainers <docker-maint@nginx.com>, name=nginx-web)

#以json格式显示日志
[root@centos8-1 docker-compose]# docker-compose events --json
{"time": "2021-09-23T16:50:09.142570", "type": "container", "action": "kill", "id": "42675c00c4c9083c54821dfdf868112d714f73f3a8fdd2e1a87ac26a119963a4", "service": "service-httpd-web", "attributes": {"image": "httpd", "name": "httpd-web", "signal": "28"}}
{"time": "2021-09-23T16:50:09.142930", "type": "container", "action": "kill", "id": "60dcd8b03365639ada1cd0aa4ca81aa42ba5dcf7a5e3110d96df13e690cbf0ea", "service": "service-nginx-web", "attributes": {"image": "nginx", "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>", "name": "nginx-web", "signal": "3"}}
{"time": "2021-09-23T16:50:09.189668", "type": "container", "action": "die", "id": "60dcd8b03365639ada1cd0aa4ca81aa42ba5dcf7a5e3110d96df13e690cbf0ea", "service": "service-nginx-web", "attributes": {"exitCode": "0", "image": "nginx", "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>", "name": "nginx-web"}}
{"time": "2021-09-23T16:50:09.277085", "type": "container", "action": "stop", "id": "60dcd8b03365639ada1cd0aa4ca81aa42ba5dcf7a5e3110d96df13e690cbf0ea", "service": "service-nginx-web", "attributes": {"image": "nginx", "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>", "name": "nginx-web"}}
{"time": "2021-09-23T16:50:10.189354", "type": "container", "action": "die", "id": "42675c00c4c9083c54821dfdf868112d714f73f3a8fdd2e1a87ac26a119963a4", "service": "service-httpd-web", "attributes": {"exitCode": "0", "image": "httpd", "name": "httpd-web"}}
{"time": "2021-09-23T16:50:10.287454", "type": "container", "action": "stop", "id": "42675c00c4c9083c54821dfdf868112d714f73f3a8fdd2e1a87ac26a119963a4", "service": "service-httpd-web", "attributes": {"image": "httpd", "name": "httpd-web"}}

9.3.9 暂停和恢复

[root@centos8-1 docker-compose]# docker-compose pause
Pausing nginx-web ... done
Pausing httpd-web ... done
[root@centos8-1 docker-compose]# docker-compose ps
  Name                 Command               State                                   Ports                                
--------------------------------------------------------------------------------------------------------------------------
httpd-web   httpd-foreground                 Paused   0.0.0.0:8443->443/tcp,:::8443->443/tcp,                             
                                                      0.0.0.0:81->80/tcp,:::81->80/tcp                                    
nginx-web   /docker-entrypoint.sh ngin ...   Paused   0.0.0.0:443->443/tcp,:::443->443/tcp,                               
                                                      0.0.0.0:80->80/tcp,:::80->80/tcp

#恢复
[root@centos8-1 docker-compose]# docker-compose unpause
Unpausing httpd-web ... done
Unpausing nginx-web ... done
[root@centos8-1 docker-compose]# docker-compose ps
  Name                 Command               State                                   Ports                                
--------------------------------------------------------------------------------------------------------------------------
httpd-web   httpd-foreground                 Up      0.0.0.0:8443->443/tcp,:::8443->443/tcp,                              
                                                     0.0.0.0:81->80/tcp,:::81->80/tcp                                     
nginx-web   /docker-entrypoint.sh ngin ...   Up      0.0.0.0:443->443/tcp,:::443->443/tcp,                                
                                                     0.0.0.0:80->80/tcp,:::80->80/tcp

9.3.10 指定同时启动容器的数量

[root@centos8-1 docker-compose]# cat docker-compose.yml 
service-nginx-web:
  image: nginx
 #container_name: nginx-web #同时启动多个同一镜像容器,不要指定容器名,否则会冲突
  expose: 
    - 80
    - 443
 #ports:                   #同时启动多个同一镜像容器,不要指定端口号,否则会冲突
 #  - "80:80"
 #  - "443:443"

[root@centos8-1 docker-compose]# docker-compose ps
Name   Command   State   Ports
------------------------------
[root@centos8-1 docker-compose]# docker-compose up -d --scale service-nginx-web=3
Creating docker-compose_service-nginx-web_1 ... done
Creating docker-compose_service-nginx-web_2 ... done
Creating docker-compose_service-nginx-web_3 ... done
[root@centos8-1 docker-compose]# docker-compose ps
               Name                             Command               State        Ports     
---------------------------------------------------------------------------------------------
docker-compose_service-nginx-web_1   /docker-entrypoint.sh ngin ...   Up      443/tcp, 80/tcp
docker-compose_service-nginx-web_2   /docker-entrypoint.sh ngin ...   Up      443/tcp, 80/tcp
docker-compose_service-nginx-web_3   /docker-entrypoint.sh ngin ...   Up      443/tcp, 80/tcp

9.3.11 扩容与缩容

#扩容
[root@centos8-1 docker-compose]# docker-compose ps
               Name                             Command               State        Ports     
---------------------------------------------------------------------------------------------
docker-compose_service-nginx-web_1   /docker-entrypoint.sh ngin ...   Up      443/tcp, 80/tcp
docker-compose_service-nginx-web_2   /docker-entrypoint.sh ngin ...   Up      443/tcp, 80/tcp
docker-compose_service-nginx-web_3   /docker-entrypoint.sh ngin ...   Up      443/tcp, 80/tcp
[root@centos8-1 docker-compose]# docker-compose scale service-nginx-web=4
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
Creating docker-compose_service-nginx-web_4 ... done
[root@centos8-1 docker-compose]# docker-compose ps
               Name                             Command               State        Ports     
---------------------------------------------------------------------------------------------
docker-compose_service-nginx-web_1   /docker-entrypoint.sh ngin ...   Up      443/tcp, 80/tcp
docker-compose_service-nginx-web_2   /docker-entrypoint.sh ngin ...   Up      443/tcp, 80/tcp
docker-compose_service-nginx-web_3   /docker-entrypoint.sh ngin ...   Up      443/tcp, 80/tcp
docker-compose_service-nginx-web_4   /docker-entrypoint.sh ngin ...   Up      443/tcp, 80/tcp

#缩容,缩容为0,即删除容器
[root@centos8-1 docker-compose]# docker-compose scale service-nginx-web=0
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
Stopping and removing docker-compose_service-nginx-web_1 ... done
Stopping and removing docker-compose_service-nginx-web_2 ... done
Stopping and removing docker-compose_service-nginx-web_3 ... done
Stopping and removing docker-compose_service-nginx-web_4 ... done
[root@centos8-1 docker-compose]# docker-compose ps
Name   Command   State   Ports
------------------------------

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注