Docker compose实施例子
公司正好有个小项目,取名partner,基于spring boot的。需进行访问,加上Nginx,计划用docker-compose做。
目录结构如下:
[root@docker-qa docker-compose]#
drwxr-xr-x 2 root root 4096 Sep 12 16:29 b2b-partner
-rw-r--r-- 1 root root 660 Sep 12 15:52 docker-compose.yml
drwxr-xr-x 3 root root 4096 Sep 12 15:04 logs #其中存放所有容器log,容器应用log目录映射
drwxr-xr-x 3 root root 4096 Sep 12 15:28 nginx
[root@docker-qa docker-compose]# pwd
/data/docker/docker-compose
Nginx和partner目录分别是两个应用容器,存放基础配置、jar包、Dockerfile。
#ll b2b-partner/
总用量 61672
-rw-r--r-- 1 root root 63145378 Sep 12 12:05 b2b-partner.jar
-rw-r--r-- 1 root root 457 Sep 12 14:50 Dockerfile
# ll nginx/
总用量 20
drwxr-xr-x 2 root root 4096 Sep 12 16:42 conf.d
-rw-r--r-- 1 root root 349 Sep 12 12:10 Dockerfile
-rw-r--r-- 1 root root 705 Sep 12 11:41 nginx.conf
-rw------- 1 root root 1679 Sep 12 11:26 xmbaby.com.cn.key
-rw------- 1 root root 3973 Sep 12 11:26 xmbaby.com.cn.pem
容器partner的Dockerfile:
# This is b2b-partner
# Version: 1.0
# Author: yuhuanghui
# Base image
FROM registry.cn-hangzhou.aliyuncs.com/xmbaby-pre/ms-jdk8
# Maintainer
MAINTAINER b2b-partner-qa yuhuanghui@alaxiaoyou.com
# Set timezome
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
#EXPOSE 8081
ADD b2b-partner.jar /data/httpd/
WORKDIR /data/httpd/
ENTRYPOINT java -Xmx256m -Xss512k -jar b2b-partner.jar
容器Nginx的Dockerfile:
# This is b2b-nginx
# Version: 1.0
# Author: yuhuanghui
# Base image
FROM nginx:1.12
# Maintainer
MAINTAINER b2b-nginx yuhuanghui@alaxiaoyou.com
# Set timezome
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN mkdir -p /data/certs/
COPY ./xmbaby.com.cn.* /data/certs/ #https访问,需SSL证书
docker-compose.yml的services编写规则,主要包含version、services、network
关键的docker-compose.yml如下:
version: '2.2'
services:
#db:
# image:mysql:5.7 #image 如果本地没有镜像,则会在公共镜像仓库拉取对应镜像
nginx:
build: ./nginx #build 基于指定Dockerfile路径,compose利用它进行构建
container_name: b2b-nginx
#depends_on: #depends_on 容器依赖
#- b2b-partner
#links: # links 链接到其他服务器中的容器,使用alias别名则会在容器的/etc/hosts文件创建
#- db:mysql5.7
#- b2b-partner:b2b-partner
volumes: #volumes挂载目录(Host:Container),ro表示只读
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- ./nginx/conf.d:/etc/nginx/conf.d:ro
- ./logs/nginx:/var/log/nginx/
ports: #ports 映射端口(Host:Container)
- "80:80"
- "443:443"
restart: always
command: nginx -g 'daemon off;' #command 会覆盖容器启动后默认执行的命令。
b2b-partner:
build: ./b2b-partner
container_name: b2b-partner
ports:
- "8091:8081"
restart: always
docker-compose.yml中还有network网络模式,有:
network_mode: "bridge"
network_mode: "host"
network_mode: "none"
network_mode: "service:[service name]"
network_mode: "container:[container name/id]"
这里docker-compose.yml中没有指定network,但进行docker-compose up -d 命令启动时会显示“Creating network "docker-compose_default" with the default driver”,会创建默认的network。
其中nginx配置parner的server虚拟主机主要配置如下:
server {
listen 443;
server_name aaa;
index index.html index.htm index.php;
ssl on;
ssl_certificate /data/certs/aaa;
ssl_certificate_key /data/certs/aaa;
ssl_session_timeout 5m;
access_log /var/log/nginx/aaa-ssl.access.log main;
error_log /var/log/nginx/aaa-ssl.error.log;
location /
{
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_buffer_size 64k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_max_temp_file_size 512m;
proxy_connect_timeout 1800;
proxy_read_timeout 1800;
proxy_send_timeout 1800;
#proxy_pass http://aaa:8091;
proxy_pass http://172.17.0.1:8091;
}
}
上面的反向代理配置刚开始是 “#proxy_pass http://b2b-partner:8091;” 发现不行,后面换成docker0虚拟网桥IP(其他docker容器的网关),即 “proxy_pass http://172.17.0.1:8091;” 测试通过。
第二天突然灵光开窍,测试发现,其实反向代理proxy_pass端口不是映射出去的端口,是容器内的端口,换成“proxy_pass http://b2b-partner:8081;”, 测试通过。Nginx容器反向代理的格式是:“proxy_pass http://容器名称:容器Port;”。