2016-12-07 18 views
2

私はmongodbコンテナに接続したいアプリケーションコンテナをいくつか持っています。 external_linksで試しましたが、mongodbに接続できません。Docker-compose external_linksが接続できない

私はexternal_linksが働いて取得するには、同じネットワークにコンテナを追加する必要がありますか

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

を取得しますか?

の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 

答えて

7

ドキュメント

docker network create -d bridge custom

ドッカ - コン・1.yml

version: '2' 

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

    networks: 
     custom: 
     external: true 

ドッカ - コン・2.yml新しいドッキングウィンドウのネットワークを作成します:は

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は言います

+0

こんにちはとバージョン3の変動を貼り付けました。それらを同じネットワークに接続する方法の例を教えてください。 – Chris

+0

私の問題を解決しました。驚くばかりの回答@yuva。バージョン3でも動作します。 – EnchanterIO

1

バージョン2のための上記のYuvaの答えは、バージョン3でもよくなります。

external_linksのドキュメントが十分に明確ではありません。より明確にするために

は私が注釈

version: '3' 

services: 
    app: 
    image: training/webapp 
    networks: 
     - <<network created by other compose file>> 
    external_links: 
     - postgres:postgres 

networks: 
    <<network created by other compose file>>: 
    external: true 
関連する問題