docker compose scale的时候解决端口冲突问题

比如这个情况,pspdfkit希望扩展多几个docker,但是简单scale,5000宿主机端口冲突

version: "3"

services:
  db:
    image: postgres:latest
    environment:
      POSTGRES_USER: pspdfkit
      POSTGRES_PASSWORD: password
      # ... other environment variables
  pspdfkit:
    image: "pspdfkit/pspdfkit:latest"

    environment:
      PGUSER: pspdfkit
      PGPASSWORD: password
      # ... other environment variables
    depends_on:
      - db
    ports:
      - "5000:5000"

方法1: 动态分配端口

最后一行改成 - “5000”
docker-compose up --scale pspdfkit=3 然后ps,此时才能知道3个container的宿主机端口

方法2: 加ng

user  nginx;

events {
    worker_connections   1000;
}
http {
        server {
              listen 4000;
              location / {
                proxy_pass http://pspdfkit:5000;
              }
        }
}
version: "3"

services:
  db:
    image: postgres:latest
    environment:
      POSTGRES_USER: pspdfkit
      POSTGRES_PASSWORD: password
      # ... other environment variables
  pspdfkit:
    image: "pspdfkit/pspdfkit:latest"

    environment:
      PGUSER: pspdfkit
      PGPASSWORD: password
      # ... other environment variables
    depends_on:
      - db
    expose:
      - "5000"
  nginx:
    image: nginx:latest
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
    depends_on:
      - pspdfkit
    ports:
      - "4000:4000"

docker-compose up --scale pspdfkit=5后,ng监听4000,把5000的proxy pass过去.
当然,最新的docker stack replicas不用整这个了,更不要说ku8的pod,本人喜爱简单明了的,方法2思路清晰,扩展到集群,可以写python脚本控制.

你可能感兴趣的:(docker)