多个docker容器的端口相同

时间:2018-03-10 00:19:26

标签: docker docker-compose

我有以下容器:

CONTAINER ID        IMAGE                                    COMMAND                  CREATED             STATUS              PORTS                                                                                                                       NAMES
f75e2629d5b5        sameersbn/gitlab:10.2.2                  "/sbin/entrypoint.sh…"   12 minutes ago      Up 11 minutes       80/tcp, 0.0.0.0:22->22/tcp, 443/tcp                                                                                         gitlab_app_1
8fc5b1cec6d5        sameersbn/redis:latest                   "/sbin/entrypoint.sh…"   12 minutes ago      Up 12 minutes       6379/tcp                                                                                                                    gitlab_redis_1
44db2400787d        sameersbn/postgresql:9.6-2               "/sbin/entrypoint.sh"    12 minutes ago      Up 12 minutes       5432/tcp                                                                                                                    gitlab_postgresql_1
31a3423a66c7        nextcloud_web                            "nginx -g 'daemon of…"   37 minutes ago      Up 37 minutes       80/tcp, 443/tcp                                                                                                             nextcloud_web_1
14334d36116a        nextcloud_app                            "/entrypoint.sh php-…"   37 minutes ago      Up 37 minutes       9000/tcp                                                                                                                    nextcloud_app_1
258d317934a7        nextcloud_cron                           "/cron.sh"               37 minutes ago      Up 37 minutes       9000/tcp                                                                                                                    nextcloud_cron_1
c66f31c762d8        mariadb                                  "docker-entrypoint.s…"   37 minutes ago      Up 37 minutes       3306/tcp                                                                                                                    nextcloud_db_1
53e8fa0e5a9f        redis                                    "docker-entrypoint.s…"   37 minutes ago      Up 37 minutes       6379/tcp                                                                                                                    nextcloud_redis_1
e4c147824046        tvial/docker-mailserver:latest           "/bin/sh -c 'supervi…"   About an hour ago   Up 33 minutes       0.0.0.0:25->25/tcp, 110/tcp, 0.0.0.0:143->143/tcp, 0.0.0.0:587->587/tcp, 465/tcp, 995/tcp, 0.0.0.0:993->993/tcp, 4190/tcp   mail_mail_1
4d99cf5a542a        nginx                                    "nginx -g 'daemon of…"   About an hour ago   Up 33 minutes       80/tcp, 443/tcp                                                                                                             mail_ssl_1
a4e9b76b91df        jrcs/letsencrypt-nginx-proxy-companion   "/bin/bash /app/entr…"   About an hour ago   Up About an hour                                                                                                                                main_letsencrypt_1
334d501060b4        jwilder/nginx-proxy                      "/app/docker-entrypo…"   About an hour ago   Up 22 minutes       0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp                                                                                    main_nginx-proxy_1
2ff7189e6272        mattermost_db                            "/entrypoint.sh post…"   About an hour ago   Up About an hour    5432/tcp                                                                                                                    mattermost_db_1
4d99ebc5ec02        mattermost_app                           "/entrypoint.sh plat…"   About an hour ago   Up About an hour    80/tcp                                                                                                                      mattermost_app_1
94007cb05dd3        mattermost_web                           "/entrypoint.sh"         About an hour ago   Up About an hour    80/tcp, 443/tcp                                                                                                             mattermost_web_1

正如您所见,有多个容器具有相同的端口。例如nextcloud_redisgitlab_redis。由于我使用的是nginx-proxy,我必须将所有容器放入同一个网络中。这会导致问题,因为例如gitlab_app尝试使用nextcloud_redis。 在下面的docker-compose。

Nextcloud

version: '3'

services:
  db:
    image: mariadb
    # image: mysql
    restart: always
    volumes:
      - ./db:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=XXX
    env_file:
      - db.env

  redis:
    image: redis
    restart: always

  app:
    build: ./app
    restart: always
    volumes:
      - ./data:/var/www/html
    environment:
      - MYSQL_HOST=db
    env_file:
      - db.env
    depends_on:
      - db
      - redis

  web:
    build: ./web
    restart: always
    expose:
      - "443"
      - "80"
    volumes:
      - ./data:/var/www/html:ro
    environment:
      - VIRTUAL_HOST=cloud.XXX.net
      - LETSENCRYPT_HOST=cloud.XXX.net
      - LETSENCRYPT_EMAIL=admin@XXX.net
    depends_on:
      - app

  cron:
    build: ./app
    restart: always
    volumes:
      - ./data:/var/www/html
    entrypoint: /cron.sh
    depends_on:
      - db
      - redis

networks:
  default:
    external:
      name: reverse-proxy

Gitlab

version: '2'

services:
    redis:
      restart: always
      image: sameersbn/redis:latest
      command:
      - --loglevel warning
      volumes:
      - ./redis:/var/lib/redis:Z

    postgresql:
      restart: always
      image: sameersbn/postgresql:9.6-2
      volumes:
      - ./postgresql:/var/lib/postgresql:Z
      environment:
      - DB_USER=gitlab
      - DB_PASS=XXX
      - DB_NAME=gitlabhq_production
      - DB_EXTENSION=pg_trgm

    app:
      restart: always
      image: sameersbn/gitlab:10.2.2
      depends_on:
      - redis
      - postgresql
      expose:
       - "80"
      ports:
      - "22:22"
      volumes:
      - ./data:/home/git/data:Z
      environment:
      - VIRTUAL_HOST=gitlab.XXX.net
      - DEBUG=false

      - DB_ADAPTER=postgresql
      - DB_HOST=postgresql
      - DB_PORT=5432
      - DB_USER=gitlab
      - DB_PASS=XXX
      - DB_NAME=gitlabhq_production

      - REDIS_HOST=redis
      - REDIS_PORT=6379

      - TZ=Europe/Berlin
      - GITLAB_TIMEZONE=Berlin

      - GITLAB_HTTPS=true
      - SSL_SELF_SIGNED=true
      [...]    
      - OAUTH_CROWD_SERVER_URL=
      - OAUTH_CROWD_APP_NAME=
      - OAUTH_CROWD_APP_PASSWORD=

      - OAUTH_AUTH0_CLIENT_ID=
      - OAUTH_AUTH0_CLIENT_SECRET=
      - OAUTH_AUTH0_DOMAIN=

      - OAUTH_AZURE_API_KEY=
      - OAUTH_AZURE_API_SECRET=
      - OAUTH_AZURE_TENANT_ID=

networks:
 default:
   external:
     name: reverse-proxy

解决此问题的最佳解决方案是什么?

1 个答案:

答案 0 :(得分:1)

尝试使用networks

(目前我无处可去,所以我希望它有效,或至少帮助你解读这个难题)

version: '3'

services:
  db:
    image: mariadb
    # image: mysql
    restart: always
    volumes:
      - ./db:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=XXX
    env_file:
      - db.env
    networks:
      - net_internal_next

  redis:
    image: redis
    restart: always
    networks:
      - net_internal_next

  app:
    build: ./app
    restart: always
    volumes:
      - ./data:/var/www/html
    environment:
      - MYSQL_HOST=db
    env_file:
      - db.env
    depends_on:
      - db
      - redis
    networks:
      - net_internal_next

  web:
    build: ./web
    restart: always
    expose:
      - "443"
      - "80"
    volumes:
      - ./data:/var/www/html:ro
    environment:
      - VIRTUAL_HOST=cloud.XXX.net
      - LETSENCRYPT_HOST=cloud.XXX.net
      - LETSENCRYPT_EMAIL=admin@XXX.net
    depends_on:
      - app
    networks:
      - net_internal_next
      - net_external_next

  cron:
    build: ./app
    restart: always
    volumes:
      - ./data:/var/www/html
    entrypoint: /cron.sh
    depends_on:
      - db
      - redis
    networks:
      - net_internal_next

networks:
  net_external_next:
    external:
      name: reverse-proxy
  net_internal_next:
    driver: bridge

-

version: '2'

services:
    redis:
      restart: always
      image: sameersbn/redis:latest
      command:
      - --loglevel warning
      volumes:
      - ./redis:/var/lib/redis:Z
      networks:
       - net_internal_git

    postgresql:
      restart: always
      image: sameersbn/postgresql:9.6-2
      volumes:
      - ./postgresql:/var/lib/postgresql:Z
      environment:
      - DB_USER=gitlab
      - DB_PASS=XXX
      - DB_NAME=gitlabhq_production
      - DB_EXTENSION=pg_trgm
      networks:
       - net_internal_git

    app:
      restart: always
      image: sameersbn/gitlab:10.2.2
      depends_on:
      - redis
      - postgresql
      expose:
       - "80"
      ports:
      - "22:22"
      volumes:
      - ./data:/home/git/data:Z
      environment:
      - VIRTUAL_HOST=gitlab.XXX.net
      - DEBUG=false

      - DB_ADAPTER=postgresql
      - DB_HOST=postgresql
      - DB_PORT=5432
      - DB_USER=gitlab
      - DB_PASS=XXX
      - DB_NAME=gitlabhq_production

      - REDIS_HOST=redis
      - REDIS_PORT=6379

      - TZ=Europe/Berlin
      - GITLAB_TIMEZONE=Berlin

      - GITLAB_HTTPS=true
      - SSL_SELF_SIGNED=true
      [...]    
      - OAUTH_CROWD_SERVER_URL=
      - OAUTH_CROWD_APP_NAME=
      - OAUTH_CROWD_APP_PASSWORD=

      - OAUTH_AUTH0_CLIENT_ID=
      - OAUTH_AUTH0_CLIENT_SECRET=
      - OAUTH_AUTH0_DOMAIN=

      - OAUTH_AZURE_API_KEY=
      - OAUTH_AZURE_API_SECRET=
      - OAUTH_AZURE_TENANT_ID=
      networks:
       - net_internal_git
       - net_external_git

networks:
 net_external_git:
   external:
     name: reverse-proxy
 net_internal_git:
   driver: bridge