资源规划

主机名 容器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

获取镜像

  1. # 查看可用的稳定版本
  2. sudo docker search java
  3. sudo docker pull java:8
  4. sudo docker image ls |grep java

服务编排

  1. mkdir -p /share/app
  2. vi /share/app/docker-compose.yml

服务编排内容如下:

  1. version: "2"
  2. services:
  3. nginx:
  4. container_name: v-nginx
  5. image: nginx:1.13
  6. restart: always
  7. privileged: true
  8. ports:
  9. - 80:80
  10. volumes:
  11. - ./app:/app
  12. - ./nginx/conf.d:/etc/nginx/conf.d/default.conf
  13. networks:
  14. nginx-lsb:
  15. ipv4_address: ${nginx_lsb}
  16. app-1:
  17. restart: always
  18. build: ./app
  19. working_dir: /app
  20. container_name: app-1
  21. privileged: true
  22. volumes:
  23. - ./app:/app
  24. expose:
  25. - "8080"
  26. networks:
  27. nginx-lsb:
  28. ipv4_address: ${web1_addr}
  29. depends_on:
  30. - nginx
  31. app-2:
  32. restart: always
  33. build: ./app
  34. working_dir: /app
  35. container_name: app-2
  36. privileged: true
  37. volumes:
  38. - ./app:/app
  39. expose:
  40. - "8080"
  41. networks:
  42. nginx-lsb:
  43. ipv4_address: ${web2_addr}
  44. depends_on:
  45. - nginx
  46. networks:
  47. nginx-lsb:
  48. driver: bridge
  49. ipam:
  50. config:
  51. - 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

附件

docker-compose.app.zip