单机编排Docker-compose
当在宿主机启动较多的容器时候,如果都是手动操作会觉得比较麻烦而且容器
出错,这个时候推荐使用 docker 单机编排工具 docker-compose,docker-compose
是 docker 容器的一种单机编排服务,docker-compose 是一个管理多个容器的工
具,比如可以解决容器之间的依赖关系,就像启动一个 nginx 前端服务的时候会
调用后端的 tomcat,那就得先启动 tomcat,但是启动 tomcat 容器还需要依赖数
据库,那就还得先启动数据库,docker-compose 就可以解决这样的嵌套依赖关系,
其完全可以替代 docker run 对容器进行创建、启动和停止。
docker-compose 项目是 Docker 官方的开源项目,负责实现对 Docker 容器集群
的快速编排,docker-compose 将所管理的容器分为三层,分别是工程(project),
服务(service)以及容器(container)。
github 地址 https://github.com/docker/compose
查看 docker-compose 帮助:
#官方文档
https://docs.docker.com/compose/reference/
查看 docker-compose 帮助
[root@ubuntu-1804 ~]# docker-compose --help
从 docker compose 启动单个容器
目录可以在任意目录,推荐放在有意义的位置。
[root@ubuntu-1804 ~]# mkdir /opt/docker-comp -p
单个容器的 docker compose 文件:
编写一个 yml 格式的配置 docker-compose 文件,启动一个 nginx 服务,由于格式为 yml格式,因此要注意前后的缩进及上下行的等级关系。
[root@ubuntu-1804 docker-comp]# cat docker-compose.yml
version: '3.8'
services:
nginx-server:
image: nginx-18:v1
container_name: nginx-web1
expose:
- 80
- 443
ports:
- "80:80"
- "443:443"
-d后台启动,如果已经有镜像就不用再下载
[root@ubuntu-1804 docker-comp]# docker-compose up -d
Creating nginx-web1 ... done
查看
[root@ubuntu-1804 docker-comp]# docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------------------------
nginx-web1 /apps/nginx/sbin/nginx -g ... Up 0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
伸缩扩容
先改配置文件
[root@ubuntu-1804 docker-comp]# cat docker-compose.yml
version: '3.8'
services:
nginx-server:
image: nginx-18:v1
#container_name: nginx-web1
expose:
- 80
- 443
# ports:
# - "80:80"
# - "443:443"
执行命令动态扩容
[root@ubuntu-1804 docker-comp]# docker-compose scale nginx-server=3
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
Creating docker-comp_nginx-server_1 ... done
Creating docker-comp_nginx-server_2 ... done
Creating docker-comp_nginx-server_3 ... done
再次查看
[root@ubuntu-1804 docker-comp]# docker-compose ps
Name Command State Ports
-------------------------------------------------------------------------------------
docker-comp_nginx-server_1 /apps/nginx/sbin/nginx -g ... Up 443/tcp, 80/tcp
docker-comp_nginx-server_2 /apps/nginx/sbin/nginx -g ... Up 443/tcp, 80/tcp
docker-comp_nginx-server_3 /apps/nginx/sbin/nginx -g ... Up 443/tcp, 80/tcp
缩容
# 等于多少个就是只有多少个
[root@ubuntu-1804 docker-comp]# docker-compose scale nginx-server=1
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
Stopping and removing docker-comp_nginx-server_2 ... done
Stopping and removing docker-comp_nginx-server_3 ... done
查看就只有一个
[root@ubuntu-1804 docker-comp]# docker-compose ps
Name Command State Ports
-------------------------------------------------------------------------------------
docker-comp_nginx-server_1 /apps/nginx/sbin/nginx -g ... Up 443/tcp, 80/tcp
从 docker compose 启动多个容器:
多台在一个yml文件
[root@ubuntu-1804 docker-comp]# cat docker-compose.yml
version: '3.8'
services:
nginx-server:
image: nginx-18:v1
container_name: nginx-web1
network_mode: bridge
expose:
- 80
- 443
ports:
- "80:80"
- "443:443"
tomcat-server:
image: harbor.longxuan.vip/m44/tomcat-web:app1
container_name: tomcat-web1
network_mode: bridge
expose:
- 8080
ports:
- "8080:8080"
networks:
default:
external:
name: bridge
后台启动
[root@ubuntu-1804 docker-comp]# docker-compose up -d
Creating tomcat-web1 ... done
Creating nginx-web1 ... done
查看
[root@ubuntu-1804 docker-comp]# docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------------------------------
nginx-web1 /apps/nginx/sbin/nginx -g ... Up 0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
tomcat-web1 /apps/tomcat/bin/run_tomcat.sh Up 0.0.0.0:8080->8080/tcp
查看网络
[root@ubuntu-1804 docker-comp]# docker network list
NETWORK ID NAME DRIVER SCOPE
45410b39ca9b bridge bridge local
85da98e2a685 docker-comp_default bridge local
87b448b70501 host host local
f14531f6983b longxuan-net bridge local
e1ef972a5715 none null local
nginx和Tomcat都在同一个网络网段
[root@ubuntu-1804 docker-comp]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fe2839c99a01 nginx-18:v1 "/apps/nginx/sbin/ng…" 6 minutes ago Up 6 minutes 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp nginx-web1
2fec080d6700 harbor.longxuan.vip/m44/tomcat-web:app1 "/apps/tomcat/bin/ru…" 6 minutes ago Up 6 minutes 0.0.0.0:8080->8080/tcp tomcat-web1
# nginx
[root@ubuntu-1804 docker-comp]# docker exec -it fe2839c99a01 bash
[root@fe2839c99a01 /]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
98: eth0@if99: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:0a:0a:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 10.10.0.2/24 brd 10.10.0.255 scope global eth0
valid_lft forever preferred_lft forever
[root@fe2839c99a01 /]# exit
exit
# tomcat
[root@ubuntu-1804 docker-comp]# docker exec -it 2fec080d6700 bash
[root@2fec080d6700 /]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
100: eth0@if101: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:0a:0a:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 10.10.0.3/24 brd 10.10.0.255 scope global eth0
valid_lft forever preferred_lft forever
测试
实现访问Nginx就可以调度到tomcat
编写 docker-compose.yml 文件,实现单机版本的 nginx + tomcat 的动静分离 web
站点,要求从 nginx 作为访问入口,当访问指定 URL 的时候转发至 tomcat 服务器响应。
方法一:
# 进入nginx容器改配置文件
[root@ubuntu-1804 docker-comp]# docker exec -it fe2839c99a01 bash
# 添加一条location
[root@fe2839c99a01 /]# vim /apps/nginx/conf/nginx.conf
location /myapp {
proxy_pass http://10.10.0.3:8080; # 这是Tomcat容器IP地址
}
# 检查语法
[root@fe2839c99a01 /]# /apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
# 重新加载
[root@fe2839c99a01 /]# /apps/nginx/sbin/nginx -s reload
方法二:
# 删除(危险命令参数慎用)
[root@ubuntu-1804 docker-comp]# docker-compose down
Stopping nginx-web1 ... done
Stopping tomcat-web1 ... done
Removing nginx-web1 ... done
Removing tomcat-web1 ... done
改配置
[root@ubuntu-1804 docker-comp]# cat docker-compose.yml
version: '3.8'
services:
nginx-server:
image: nginx-18:v1
container_name: nginx-web1
#network_mode: bridge
expose:
- 80
- 443
ports:
- "80:80"
- "443:443"
networks:
- frone
- backend
tomcat-server:
image: harbor.longxuan.vip/m44/tomcat-web:app1
container_name: tomcat-web1
#network_mode: bridge
expose:
- 8080
ports:
- "8080:8080"
networks:
- backend
networks:
frone: # 自定义前端的网络
driver: bridge
backend: # 自定义后端服务的网络
driver: bridge
default: # 默认的docker0网络
external:
name: bridge
启动
[root@ubuntu-1804 docker-comp]# docker-compose up -d
Creating network "docker-comp_frone" with driver "bridge"
Creating network "docker-comp_backend" with driver "bridge"
Creating tomcat-web1 ... done
Creating nginx-web1 ... done
查看
[root@ubuntu-1804 docker-comp]# docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------------------------------
nginx-web1 /apps/nginx/sbin/nginx -g ... Up 0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
tomcat-web1 /apps/tomcat/bin/run_tomcat.sh Up 0.0.0.0:8080->8080/tcp
查看容器网络
[root@ubuntu-1804 docker-comp]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f8ed4ae67f68 harbor.longxuan.vip/m44/tomcat-web:app1 "/apps/tomcat/bin/ru…" 10 minutes ago Up 10 minutes 0.0.0.0:8080->8080/tcp tomcat-web1
2308affaaa3c nginx-18:v1 "/apps/nginx/sbin/ng…" 10 minutes ago Up 10 minutes 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp nginx-web1
nginx
[root@ubuntu-1804 docker-comp]# docker exec -it 2308affaaa3c bash
[root@2308affaaa3c /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.20.0.3 netmask 255.255.0.0 broadcast 172.20.255.255
ether 02:42:ac:14:00:03 txqueuelen 0 (Ethernet)
RX packets 13 bytes 1006 (1006.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.19.0.2 netmask 255.255.0.0 broadcast 172.19.255.255
ether 02:42:ac:13:00:02 txqueuelen 0 (Ethernet)
RX packets 18 bytes 1452 (1.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
tomcat
[root@ubuntu-1804 docker-comp]# docker exec -it f8ed4ae67f68 bash
[root@f8ed4ae67f68 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.20.0.2 netmask 255.255.0.0 broadcast 172.20.255.255
ether 02:42:ac:14:00:02 txqueuelen 0 (Ethernet)
RX packets 16 bytes 1276 (1.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
改nginx配置文件
[root@ubuntu-1804 docker-comp]# docker exec -it 2308affaaa3c bash
[root@2308affaaa3c /]# vim /apps/nginx/conf/nginx.conf
location /myapp {
proxy_pass http://172.20.0.2:8080;
}
[root@2308affaaa3c /]# /apps/nginx/sbin/nginx -s reload
实现通过HAproxy调度到nginx如果是访问Tomcat也通过nginx调度
[root@ubuntu-1804 docker-comp]# mkdir case1
[root@ubuntu-1804 docker-comp]# cd case1/
先制作Tomcat
[root@ubuntu-1804 case1]# cat docker-compose.yml
version: '3.8'
services:
service-tomcat-app1:
image: harbor.longxuan.vip/m44/tomcat-web:app2
container_name: tomcat-app1-container1
networks:
- front
expose:
- 8080
ports:
- "8080:8080"
networks:
front:
driver: bridge
backend:
driver: bridge
default:
external:
name: bridge
启动
[root@ubuntu-1804 case1]# docker-compose up -d
WARNING: Some networks were defined but are not used by any service: backend
Creating network "case1_front" with driver "bridge"
Creating tomcat-app1-container1 ... done
创建需要挂载的目录
[root@ubuntu-1804 case1]# mkdir /data/tomcat/logs
[root@ubuntu-1804 case1]# mkdir /data/tomcat/webapps/m44 -p
[root@ubuntu-1804 case1]# echo /data/tomcat/webapps/m44/index.html > /data/tomcat/webapps/m44/index.html
改配置文件
[root@ubuntu-1804 case1]# cat docker-compose.yml
version: '3.8'
services:
service-tomcat-app1:
image: harbor.longxuan.vip/m44/tomcat-web:app2
container_name: tomcat-app1-container1
volumes:
- /data/tomcat/webapps/m44:/data/tomcat/webapp/m44
- /data/tomcat/logs:/apps/tomcat/logs
networks:
- front
expose:
- 8080
ports:
- "8080:8080"
networks:
front:
driver: bridge
backend:
driver: bridge
default:
external:
name: bridge
再次启动
[root@ubuntu-1804 case1]# docker-compose up -d
WARNING: Some networks were defined but are not used by any service: backend
Recreating tomcat-app1-container1 ... done
在制作nginx同时还能通过nginx访问Tomcat
[root@ubuntu-1804 case1]# cat docker-compose.yml
version: '3.8'
services:
service-nginx-m44:
image: harbor.longxuan.vip/m44/nginx-18:v1
container_name: nginx-m44-app1
networks:
- front
expose:
- 80
ports:
- "80:80"
links:
- service-tomcat-app1
service-tomcat-app1:
image: harbor.longxuan.vip/m44/tomcat-web:app2
container_name: tomcat-app1-container1
volumes:
- /data/tomcat/webapps/m44:/data/tomcat/webapps/m44
- /data/tomcat/logs:/apps/tomcat/logs
networks:
- front
networks:
front:
driver: bridge
backend:
driver: bridge
default:
external:
name: bridge
启动
[root@ubuntu-1804 case1]# docker-compose up -d
WARNING: Some networks were defined but are not used by any service: backend
Recreating tomcat-app1-container1 ... done
Creating nginx-m44-app1 ... done
测试
[root@ubuntu-1804 case1]# docker ps
CONTAINER ID IMAGE COMMAND CREATED S
d113bda237ad harbor.longxuan.vip/m44/nginx-18:v1 "/apps/nginx/sbin/ng…" 46 seconds ago U
da758a26ea0b harbor.longxuan.vip/m44/tomcat-web:app2 "/apps/tomcat/bin/ru…" 3 minutes ago U
[root@ubuntu-1804 case1]# docker exec -it d113bda237ad bash
[root@d113bda237ad /]# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.25.0.3 d113bda237ad
[root@d113bda237ad /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.0.3 netmask 255.255.0.0 broadcast 172.25.255.255
ether 02:42:ac:19:00:03 txqueuelen 0 (Ethernet)
RX packets 24 bytes 2512 (2.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 11 bytes 1158 (1.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@d113bda237ad /]# ping service-tomcat-app1
PING service-tomcat-app1 (172.25.0.2) 56(84) bytes of data.
64 bytes from tomcat-app1-container1.case1_front (172.25.0.2): icmp_seq=1 ttl=64 time=0.135 ms
64 bytes from tomcat-app1-container1.case1_front (172.25.0.2): icmp_seq=2 ttl=64 time=0.131 ms
因为nginx镜像没有做这个location,所以必须进入nginx容器改配置文件
# 添加location
[root@d113bda237ad /]# vim /apps/nginx/conf/nginx.conf
location /m44 {
proxy_pass http://service-tomcat-app1:8080;
}
制作haproxy
# 创建目录
[root@ubuntu-1804 docker-comp]# mkdir case2
[root@ubuntu-1804 docker-comp]# cd case2/
前提是有制作好的haproxy镜像
haproxy配置文件如下:
[06:05:54 root@ubuntu-1804 docker-comp]# docker exec -it 0885b9e51756 bash
[root@0885b9e51756 /]# cat /etc/haproxy/haproxy.cfg
# cat haproxy.cfg
global
chroot /usr/local/haproxy
#stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
uid 99
gid 99
daemon
nbproc 1
pidfile /usr/local/haproxy/run/haproxy.pid
log 127.0.0.1 local3 info
defaults
option http-keep-alive
option forwardfor
mode http
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms
listen stats
mode http
bind 0.0.0.0:9999
stats enable
log global
stats uri /haproxy-status
stats auth haadmin:123456
listen web_port_80
bind 0.0.0.0:80
mode http
log global
balance roundrobin
server web1 service-nginx-m44:80 check inter 3000 fall 2 rise 5
listen web_port_443
bind 0.0.0.0:443
mode http
log global
balance roundrobin
server web1 service-nginx-m44:443 check inter 3000 fall 2 rise 5
修改docker-compose.yml文件
[root@ubuntu-1804 case1]# cat docker-compose.yml
version: '3.8'
services:
service-haproxy-m44:
image: harbor.longxuan.vip/m44/haproxy-2.1.7:v1
container_name: haproxy-m44
networks:
- front
expose:
- 80
ports:
- "80:80"
links:
- service-nginx-m44
service-nginx-m44:
image: harbor.longxuan.vip/m44/nginx-18:v1
container_name: nginx-m44-app1
networks:
- front
links:
- service-tomcat-app1
service-tomcat-app1:
image: harbor.longxuan.vip/m44/tomcat-web:app1
container_name: tomcat-app1-container1
volumes:
- /data/tomcat/webapps/m44:/data/tomcat/webapps/m44
- /data/tomcat/logs:/apps/tomcat/logs
networks:
- front
networks:
front:
driver: bridge
backend:
driver: bridge
default:
external:
name: bridge
启动
[root@ubuntu-1804 case2]# docker-compose up -d
WARNING: Some networks were defined but are not used by any service: backend
Creating tomcat-app1-container1 ... done
Creating nginx-m44-app1 ... done
Creating haproxy-m44 ... done
总结
由于nginx容器的location没有添加,就没有办法调用Tomcat,解决方法:
第一种:直接进入容器添加location重启容器里面的nginx服务
第二种:重新制作dockerfile文件,并重新打镜像上传到harbor仓库,因为docker-compose.yml文件中如果没有就会根据镜像去相应的镜像路径去拉取镜像,如果拉取不到,说明没有上传到harbor仓库,重新打标签上传即可
范例:如下启动docker-compose报错
[root@ubuntu-1804 docker-comp]# docker-compose up -d
WARNING: Some networks were defined but are not used by any service: backend
Creating network "docker-comp_front" with driver "bridge"
Pulling service-haproxy-m44 (harbor.longxuan.vip/m44/haproxy-2.1.7:v2)...
ERROR: unknown: artifact m44/haproxy-2.1.7:v2 not found