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

查看:1042
本文介绍了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.

Ex:

创建一个新的码头工人网络

Create a new docker network

docker网络创建-d网桥自定义

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天全站免登陆