Docker-compose external_links 无法连接 [英] Docker-compose external_links not able to connect

查看:39
本文介绍了Docker-compose external_links 无法连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a couple of app containers that I want to connect to the mongodb container. I tried with external_links but I can not connect to the mongodb.

我明白

MongoError:第一次连接到服务器 [mongodb:27017] 失败连接

MongoError: failed to connect to server [mongodb:27017] on first connect

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

Do I have to add the containers into the same network to get external_links working?

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网络

Create a new docker network

docker network create -d bridge custom

docker-compose-1.yml

docker-compose-1.yml

    version: '2'

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

    networks:
      custom:
        external: true

docker-compose-2.yml

docker-compose-2.yml

    version: '2'

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

    networks:
      custom:
        external: true

这篇关于Docker-compose external_links 无法连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆