docker-compose 管理多个docker容器实例

时间:2021-12-02 03:02:13

Compose 安装

运行此命令下载最新版本的Docker Compose

$ curl -L https://github.com/docker/compose/releases/download/1.18.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

对二进制文件应用可执行权限

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

验证

$ docker-compose --version
docker-compose version 1.16.1, build 6d1ac21
用镜像 pre-official-ui 启动多个容器实例

root@weifeng02 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> b0bd7b10a79d 4 weeks ago 276 MB
pre-official-ui latest 0069c632f5de 5 weeks ago 276 MB
[root@weifeng02 ~]# cat docker-compose.yml
nginx02:
image: pre-official-ui
ports:
- 80
restart: always

  

 启动命令

[root@weifeng02 ~]# docker-compose up -d   --scale nginx02=5
Starting root_nginx02_1 ... done
Starting root_nginx02_2 ... done
Starting root_nginx02_3 ... done
Starting root_nginx02_4 ... done
Starting root_nginx02_5 ... done
[root@weifeng02 ~]#
[root@weifeng02 ~]#
[root@weifeng02 ~]# docker-compose ps
Name Command State Ports
-----------------------------------------------------------------
root_nginx02_1 nginx Up 443/tcp, 0.0.0.0:32796->80/tcp
root_nginx02_2 nginx Up 443/tcp, 0.0.0.0:32797->80/tcp
root_nginx02_3 nginx Up 443/tcp, 0.0.0.0:32798->80/tcp
root_nginx02_4 nginx Up 443/tcp, 0.0.0.0:32795->80/tcp
root_nginx02_5 nginx Up 443/tcp, 0.0.0.0:32794->80/tcp [root@weifeng02 ~]# docker-compose stop nginx02
Stopping root_nginx02_1 ... done
Stopping root_nginx02_3 ... done
Stopping root_nginx02_5 ... done
Stopping root_nginx02_4 ... done
Stopping root_nginx02_2 ... done
[root@weifeng02 ~]# docker-compose ps
Name Command State Ports
-----------------------------------------
root_nginx02_1 nginx Exit 0
root_nginx02_2 nginx Exit 0
root_nginx02_3 nginx Exit 0
root_nginx02_4 nginx Exit 0
root_nginx02_5 nginx Exit 0
[root@weifeng02 ~]#
[root@weifeng02 ~]# docker-compose start nginx02
Starting nginx02 ... done

  

杀死一个docker 进程 可以自动重启 

[root@weifeng02 ~]# ps aux|grep docker
root 968 0.0 2.1 811344 40992 ? Ssl 02:43 0:07 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupdriver=systemd --userland-proxy-path=/usr/libexec/docker/docker-proxy-current --init-path=/usr/libexec/docker/docker-init-current --seccomp-profile=/etc/docker/seccomp.json --selinux-enabled --log-driver=journald --signature-verification=false --storage-driver overlay2
root 1024 0.0 0.8 525104 16096 ? Ssl 02:43 0:03 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --shim docker-containerd-shim --runtime docker-runc --runtime-args --systemd-cgroup=true
root 5100 0.0 0.0 0 0 ? Z 05:13 0:00 [docker-proxy-cu] <defunct>
root 5116 0.0 0.1 100796 2632 ? Sl 05:13 0:00 /usr/libexec/docker/docker-proxy-current -proto tcp -host-ip 0.0.0.0 -host-port 32805 -container-ip 172.17.0.3 -container-port 80
root 5136 0.0 0.2 100796 4676 ? Sl 05:13 0:00 /usr/libexec/docker/docker-proxy-current -proto tcp -host-ip 0.0.0.0 -host-port 32807 -container-ip 172.17.0.5 -container-port 80
root 5155 0.0 0.2 264812 5028 ? Sl 05:13 0:00 /usr/bin/docker-containerd-shim-current 95d8ffb08fbe6179e07bb0df1a74c825948a22b3bcd4ae7a65d63cfcf06ab933 /var/run/docker/libcontainerd/95d8ffb08fbe6179e07bb0df1a74c825948a22b3bcd4ae7a65d63cfcf06ab933 /usr/libexec/docker/docker-runc-current
root 5158 0.0 0.2 264812 5028 ? Sl 05:13 0:00 /usr/bin/docker-containerd-shim-current 4dbe39e8a8321b0433ca07353f0781675efb2517cee03c5f19ac1771b15e0d36 /var/run/docker/libcontainerd/4dbe39e8a8321b0433ca07353f0781675efb2517cee03c5f19ac1771b15e0d36 /usr/libexec/docker/docker-runc-current
root 5164 0.0 0.3 264812 7072 ? Sl 05:13 0:00 /usr/bin/docker-containerd-shim-current 96e77c7dfc87aa0da8ac02e81f8b697e92c0cbc9b9e727091cd554e2f406bd40 /var/run/docker/libcontainerd/96e77c7dfc87aa0da8ac02e81f8b697e92c0cbc9b9e727091cd554e2f406bd40 /usr/libexec/docker/docker-runc-current
root 5547 0.0 0.1 100796 2628 ? Sl 05:19 0:00 /usr/libexec/docker/docker-proxy-current -proto tcp -host-ip 0.0.0.0 -host-port 32809 -container-ip 172.17.0.4 -container-port 80
root 5551 0.0 0.1 264812 2936 ? Sl 05:19 0:00 /usr/bin/docker-containerd-shim-current 5324cbb9c45541506a12b5a63dad5340db2a71e49fc15345cedf7c196350ed4a /var/run/docker/libcontainerd/5324cbb9c45541506a12b5a63dad5340db2a71e49fc15345cedf7c196350ed4a /usr/libexec/docker/docker-runc-current
root 5631 0.0 0.0 112708 980 pts/1 R+ 05:21 0:00 grep --color=auto docker

  

[root@weifeng02 ~]# kill -9 5155
[root@weifeng02 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5324cbb9c455 pre-official-ui "nginx" 8 minutes ago Up 2 minutes 443/tcp, 0.0.0.0:32809->80/tcp root_nginx03_2
4dbe39e8a832 pre-official-ui "nginx" 8 minutes ago Up 8 minutes 443/tcp, 0.0.0.0:32807->80/tcp root_nginx03_3
96e77c7dfc87 pre-official-ui "nginx" 8 minutes ago Up 8 minutes 443/tcp, 0.0.0.0:32805->80/tcp root_nginx03_1
95d8ffb08fbe pre-official-ui "nginx" 8 minutes ago Up 1 second 443/tcp, 0.0.0.0:32810->80/tcp root_nginx03_5