资源规划
主机名 | 容器IP | 端口 | 暴露端口 |
---|---|---|---|
app-1 | 192.169.0.2 | 8080 | N.A |
app-2 | 192.169.0.3 | 8080 | N.A |
v-nginx | 192.169.0.100 | 80 | 80 |
获取镜像
# 查看可用的稳定版本
sudo docker search java
sudo docker pull java:8
sudo docker image ls |grep java
服务编排
mkdir -p /share/app
vi /share/app/docker-compose.yml
服务编排内容如下:
version: "2"
services:
nginx:
container_name: v-nginx
image: nginx:1.13
restart: always
privileged: true
ports:
- 80:80
volumes:
- ./app:/app
- ./nginx/conf.d:/etc/nginx/conf.d/default.conf
networks:
nginx-lsb:
ipv4_address: ${nginx_lsb}
app-1:
restart: always
build: ./app
working_dir: /app
container_name: app-1
privileged: true
volumes:
- ./app:/app
expose:
- "8080"
networks:
nginx-lsb:
ipv4_address: ${web1_addr}
depends_on:
- nginx
app-2:
restart: always
build: ./app
working_dir: /app
container_name: app-2
privileged: true
volumes:
- ./app:/app
expose:
- "8080"
networks:
nginx-lsb:
ipv4_address: ${web2_addr}
depends_on:
- nginx
networks:
nginx-lsb:
driver: bridge
ipam:
config:
- subnet: 192.169.0.0/16
服务配置
Φ 配置文件概览
以下配置相关文件保持与编排文件相同目录。
d app # app初始化配置
d static # 放置图片资源
- Dockerfile # 镜像制作文件
- app.jar # jar包
d nginx # nginx初始化配置
- conf.d # nginx配置(反向代理、负载策略)
- .env # 环境变量
Φ .env
web1_addr=192.169.0.2
web2_addr=192.169.0.3
nginx_lsb=192.169.0.100
Φ Dockerfile
FROM java:8
MAINTAINER polaris <450733605@qq.com>
VOLUME /tmp
ADD app.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
EXPOSE 8080
Φ conf.d
upstream app {
server app-1:8080 weight=1;
server app-2:8080 weight=1;
}
server {
listen 80;
charset utf-8;
access_log off;
server_name app;
location / {
proxy_set_header Host $host:$server_port;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering off;
proxy_pass http://app;
}
location /static {
access_log off;
expires 30d;
alias /app/static;
}
}
服务构建
cd /share/app
sudo docker-compose -f docker-compose.yml build --no-cache # 不带缓存构建(只创建镜像,不会启动容器)
sudo docker-compose -f docker-compose.yml up -d # 构建后运行
sudo docker-compose -f docker-compose.yml up --build # 跟踪方式构建,可用于调试
验证
cd /share/app
# 查看进程
sudo docker-compose -f docker-compose.yml ps
# URL请求验证
sudo curl -XGET http://192.168.0.101/index.html
# 查看日志
sudo docker logs app-1
sudo docker logs -f -t --tail=50 app-1
# 查看网络
sudo docker network ls
sudo docker inspect app-1
# 停止
sudo docker-compose -f docker-compose.yml stop
# 移除
sudo docker-compose -f docker-compose.yml down