我有几个要连接到mongodb容器的应用程序容器。我尝试使用external_links,但无法连接到mongodb。

我懂了



我是否必须将容器添加到同一网络中才能使external_links工作?

MongoDB:

version: '2'
services:
  mongodb:
    image: mongo:3.4
    restart: always
    ports:
      - "27017:27017"
    volumes:
      - data:/data/db
volumes:
  data:

应用程序:
version: '2'
services:
  app-dev:
    restart: Always
    build: repository/
    ports:
      - "3000:80"
    env_file:
      - ./environment.env
    external_links:
      - mongodb_mongodb_1:mongodb

联播网:
# sudo docker network ls
NETWORK ID          NAME                      DRIVER              SCOPE
29f8bae3e136        bridge                    bridge              local
67d5519cb2e6        dev_default               bridge              local
9e7097c844cf        host                      host                local
481ee4301f7c        mongodb_default           bridge              local
4275508449f6        none                      null                local
873a46298cd9        prod_default              bridge              local

最佳答案

https://docs.docker.com/compose/compose-file/#/externallinks上的文档说

If you’re using the version 2 file format, the externally-created containers must be connected to at least one of the same networks as the service which is linking to them.

前任:

创建一个新的Docker网络
docker network create -d bridge custom
docker-compose-1.yml
    version: '2'

    services:
      postgres:
        image: postgres:latest
        ports:
          - 5432:5432
        networks:
          - custom

    networks:
      custom:
        external: true

docker-compose-2.yml
    version: '2'

    services:
      app:
        image: training/webapp
        networks:
          - custom
        external_links:
          - postgres:postgres

    networks:
      custom:
        external: true

关于docker - Docker-compose external_links无法连接,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41012766/

10-16 22:36