Compos file 版本3以上
Compos file v3官网
只支持单机运行
1 2 3 4 5 6 7 8
| version: "3" services: webapp: build: context: ./dir dockerfile: Dockerfile-alternate args: buildno: 1
|
只支持集群模式运行,只需要在单机上引用,会自动在其他节点创建配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| version: "3.3" services: redis: image: redis:latest deploy: replicas: 1 configs: - my_config - my_other_config configs: my_config: file: ./my_config.txt my_other_config: external: true
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| version: '3' services: nginx: image: harbor.iexxk.dev/library/web-gate-id-v3:S3.2.1 restart: unless-stopped ports: - "88:80"
configs: - source: nginx_config target: /etc/nginx/conf.d/default.conf env_file: - ./config/nginx.env configs: nginx_config: file: ./config/default.conf
|
会覆盖dockerfile里面的命令例如:
1 2
| command: bundle exec thin -p 3000 command: ["bundle", "exec", "thin", "-p", "3000"]
|
deploy(swarm模式)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| version: '3.2' services: redis: image: redis:alpine labels: com.example.description: " containers 上的标签" deploy: mode: global replicas: 6 update_config: parallelism: 2 delay: 10s restart_policy: condition: on-failure endpoint_mode: vip labels: com.example.description: "This label will appear on the web service" placement: constraints: - node.role == manager - engine.labels.operatingsystem == ubuntu 14.04 preferences: - spread: node.labels.zone resources: limits:
cpus: '0.50' memory: 50M reservations: cpus: '0.25' memory: 20M restart_policy: condition: on-failure delay: 5s max_attempts: 3 window: 120s update_config: parallelism: 2 delay: 10s order: stop-first depends_on: - db - redis healthcheck: test: ["CMD", "curl", "-f", "http://localhost"] interval: 1m30s timeout: 10s retries: 3 start_period: 40s logging: driver: syslog options: syslog-address: "tcp://192.168.0.42:123" volumes: - type: volume source: mydata target: /data volume: nocopy: true - type: bind source: ./static target: /opt/app/static ports: - target: 80 published: 8080 protocol: tcp mode: host
|
docker-compose 网络互相访问
让一个docker-compos里面的服务verse-web-admin-novel-1
访问另一个gate-v3_defaul
中的spring-gateway:8080
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| [root@frts-p1 verse]# docker network ls NETWORK ID NAME DRIVER SCOPE e8a9788c1674 bridge bridge local 305ea0a9d372 gate-v3_default bridge local 2770fd13abc4 host host local 0a9e80edc8d3 verse_default bridge local
[root@frts-p1 verse]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES db427829805e web:1 "/docker-entrypoint.…" 2 minute ago Up 2 second 92->80/tcp verse-web-admin-novel-1 69f449d6f2aa gat:1 "sh -c 'java $JAVA_O…" 2 months ago Up 2 months 8080/tcp gate-v3-spring-gateway-1
[root@frts-p1 verse]# docker network connect gate-v3_default verse-web-admin-novel-1
|
docker-compose配置verse文件:
1 2 3 4 5 6 7 8 9
| version: '3' services: web-admin-novel: image: harbor.hcytech.dev/library/web-admin-novel:S1.3.1 restart: unless-stopped ports: - "92:80" volumes: - ./config/web-admin.conf:/etc/nginx/conf.d/default.conf
|
web-admin.conf
配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| upstream api_server { server spring-gateway:8080; } server { listen 80; server_name localhost; underscores_in_headers on; client_max_body_size 256m; location / { root /usr/share/nginx/html; index index.html index.htm; try_files $uri $uri/ /index.html =404; } location /api/ { rewrite ~/api/(.*)$ /$1 break; proxy_pass http://api_server/api/; proxy_set_header Host $host; proxy_pass_request_headers on; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_read_timeout 60000; proxy_send_timeout 60000; proxy_intercept_errors on; error_page 500 = /500handler; error_page 404 = /404handler; } }
|
docker-compose配置gate-v3文件:
1 2 3 4 5 6 7 8 9
| version: '3' services: spring-gateway: image: harbor.hcytech.dev/library/hcytech-gateway:S1.0.3 volumes: - ./config/application.yaml:/home/application.yaml env_file: - ./config/gateway.env - ./config/redis.env
|
问题
错误services.nginx.ports.0 must be a string or number
是因为自定义端口,只支持3.2以上
1 2 3 4 5 6
| version: '3.2' ports: - target: 80 published: 8080 protocol: tcp mode: host
|