基于 docker-compose 实现对 Nginx+Tomcat Web 服务的单机编排
作者:忙着长大#
- 2022-11-13 北京
本文字数:8080 字
阅读完需:约 27 分钟
1、安装 docker-compose
在 github 搜索 compose,点击 releases,选择版本下载对应系统的包,下载下来是可执行的二进制文件,直接 cp 到某个路径就可以直接使用 docker-compose 命令了。
root@yt:~# ls
docker-compose-linux-x86_64_go-2.11.1
root@yt:~# cp -a docker-compose-linux-x86_64_go-2.11.1 /usr/local/bin/docker-compose
root@yt:~# chmod 777 /usr/local/bin/docker-compose
复制代码
2、使用 docker-compose 编排文件实现对 nginx 服务的单机编排。
一个 docker-compose 文件可以编排多个服务,如下对 nginx 和 redis 同时进行编排。
root@yt:~/case1# cat docker-compose.yaml
version: '3.8'
services:
nginx-server:
image: nginx:1.22.0-alpine
container_name: nginx-web1
expose:
- 80
- 443
ports:
- "80:80"
- "443:443"
#profiles: #基于配置文件指定要操作的目的容器
# - frontend
env_file:
- test.env
deploy:
resources: #资源限制
limits:
cpus: '0.50'
memory: 1024M
reservations:
cpus: '0.5'
memory: 512M
restart: always
healthcheck: #添加服务健康状态检查
test: ["CMD", "curl", "-f", "http://127.0.0.1:80/index.html"]
interval: 5s #健康状态检查的间隔时间,默认为30s
timeout: 5s #单次检查的失败超时时间,默认为30s
retries: 3 #连续失败次数默认3次,当连续失败retries次数后将容器置为unhealthy状态
start_period: 60s #60s后每间隔interval的时间检查一次,连续retries次后才将容器置为unhealthy状态, 但是start_period时间内检查成功就认为是检查成功并装容器置于healthy状态
redis-server:
image: redis:6.2.7-alpine3.16
container_name: redis1
expose:
- 6379
ports:
- "6379:6379"
#profiles:
# - backend
复制代码
docker-compose 可以通过 "-f" 去指定根据哪个编排文件进行部署 "up -d" 标识后台启动
root@yt:~/case1# docker-compose -f docker-compose.yaml up -d
[+] Running 14/14
⠿ redis-server Pulled 21.8s
⠿ ca7dd9ec2225 Pull complete 6.2s
⠿ 83276aa4de36 Pull complete 6.2s
⠿ 731cc432e6da Pull complete 6.4s
⠿ 93b7a4c8b2d7 Pull complete 16.6s
⠿ 55da7c6d9efa Pull complete 16.6s
⠿ ce2599643e43 Pull complete 16.7s
⠿ nginx-server Pulled 34.7s
⠿ 213ec9aee27d Pull complete 22.7s
⠿ 1bfd2b69cf63 Pull complete 29.4s
⠿ a19f4cc2e029 Pull complete 29.4s
⠿ 4ae981811a6d Pull complete 29.5s
⠿ 7a662f439736 Pull complete 29.5s
⠿ a317c3c2c906 Pull complete 29.5s
[+] Running 3/3
⠿ Network case1_default Created 0.0s
⠿ Container redis1 Started 0.4s
⠿ Container nginx-web1 Started 0.5s
复制代码
docker-compose 是根据目录名称去区分不同的 project , 在不同的目录下执行 docker-compose 去执行"docker-compose ps" 会看到不同的 project 的服务信息,如果目录下不存在 docker-compose 编排文件会报错。
root@yt:~/case1# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b5ffaad4c517 nginx:1.22.0-alpine "/docker-entrypoint.…" 8 minutes ago Up 8 minutes (healthy) 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp nginx-web1
3598379a0c63 redis:6.2.7-alpine3.16 "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp redis1
root@yt:~/case1# pwd
/root/case1
复制代码
root@yt:~# docker-compose ps
no configuration file provided: not found
复制代码
执行如下命令可以停止指定文件的所有服务,并删除容器
root@yt:~/case1# docker-compose -f docker-compose.yaml down
[+] Running 3/3
⠿ Container redis1 Removed 0.2s
⠿ Container nginx-web1 Removed 0.2s
⠿ Network case1_default Removed 0.1s
复制代码
3、使用 docker-compose 实现对 nginx + tomcat 服务的单机编排部署。
root@yt:~/case2# cat docker-compose.yaml
version: '3.8'
services:
nginx-server:
image: nginx:1.22.0-alpine
container_name: nginx-web1
# network_mode: bridge #网络1,使用docker安装后的默认网桥
expose:
- 80
- 443
ports:
- "80:80"
- "443:443"
networks: #网络2,使用自定义的网络,如果网络不存在则会自动创建该网络并分配子网,并且容器会有两块网卡
- front
- backend
links:
- tomcat-server
tomcat-server:
#image: tomcat:7.0.93-alpine
image: registry.cn-hangzhou.aliyuncs.com/zhangshijie/tomcat-myapp:v1
container_name: tomcat-app1
##network_mode: bridge #网络1,使用docker安装后的默认网桥
#expose:
# - 8080
#ports:
# - "8080:8080"
networks: #网络2,使用自定义的网络,如果网络不存在则会自动创建该网络并分配子网,并且容器会有一块网卡
- backend
links:
- mysql-server
mysql-server:
image: mysql:5.6.48
container_name: mysql-container
# network_mode: bridge #网络1,使用docker安装后的默认网桥
volumes:
- /data/mysql:/var/lib/mysql
#- /etc/mysql/conf/my.cnf:/etc/my.cnf:ro
environment:
- "MYSQL_ROOT_PASSWORD=12345678"
- "TZ=Asia/Shanghai"
expose:
- 3306
ports:
- "3306:3306"
networks: #网络2,使用自定义的网络,如果网络不存在则会自动创建该网络并分配子网,并且容器会有一块网卡
- backend
networks:
front: #自定义前端服务网络,需要docker-compose创建
driver: bridge
backend: #自定义后端服务的网络,要docker-compose创建
driver: bridge
default: #使用已经存在的docker0默认172.17.0.1/16的网络
external:
name: bridge
复制代码
root@yt:~/case2# docker-compose -f docker-compose.yaml up -d
WARN[0000] network default: network.external.name is deprecated in favor of network.name
[+] Running 27/27
⠿ tomcat-server Pulled 602.0s
⠿ 2d473b07cdd5 Pull complete 272.6s
⠿ 880042ad5e39 Pull complete 272.8s
⠿ 7aa7ec8f962d Pull complete 513.4s
⠿ 8cd93c3763ca Pull complete 600.5s
⠿ d845e6db193e Pull complete 600.5s
⠿ 8e23c1c5d21b Pull complete 600.5s
⠿ c0f600ab4693 Pull complete 600.6s
⠿ 1b56f533d5b9 Pull complete 600.7s
⠿ 2c6fc468c4a5 Pull complete 600.9s
⠿ 3ca7cd0a550d Pull complete 600.9s
⠿ 333255a680d8 Pull complete 600.9s
⠿ b5ef3a8dbd15 Pull complete 600.9s
⠿ b816ffee4fae Pull complete 601.0s
⠿ 0fee8bb38f0b Pull complete 601.1s
⠿ mysql-server Pulled 484.5s
⠿ 7d2977b12acb Pull complete 341.0s
⠿ 5fb8400e7f07 Pull complete 342.7s
⠿ 234877fbb165 Pull complete 350.9s
⠿ 6fe1021f12f3 Pull complete 354.9s
⠿ 7e36fe6b53f0 Pull complete 356.9s
⠿ 996ec709c11b Pull complete 372.5s
⠿ 5198b7523387 Pull complete 374.4s
⠿ cc9bdad4dcc0 Pull complete 376.4s
⠿ 380cd37ad979 Pull complete 476.1s
⠿ d64465acf034 Pull complete 477.1s
⠿ d4ee6606b3ab Pull complete 479.1s
[+] Running 5/5
⠿ Network case2_backend Created 0.0s
⠿ Network case2_front Created 0.0s
⠿ Container mysql-container Started 0.6s
⠿ Container tomcat-app1 Started 0.6s
⠿ Container nginx-web1 Started 1.1s
复制代码
root@yt:~/case2# docker-compose ps
WARN[0000] network default: network.external.name is deprecated in favor of network.name
NAME COMMAND SERVICE STATUS PORTS
mysql-container "docker-entrypoint.s…" mysql-server running 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp
nginx-web1 "/docker-entrypoint.…" nginx-server running 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp
tomcat-app1 "/apps/tomcat/bin/do…" tomcat-server running 8080/tcp, 8443/tcp
复制代码
划线
评论
复制
发布于: 刚刚阅读数: 4
忙着长大#
关注
还未添加个人签名 2022-02-09 加入
还未添加个人简介
评论