Docker-compose external_links cannot connect

I have several application containers that I want to connect to the mongodb container. I tried with external_links, but I cannot connect to mongodb.

I get

MongoError: Could not connect to server [mongodb: 27017] on first connection

Do I need to add containers to the same network to work with external_references?

MongoDB:

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

applications:

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

Networks:

 # 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 
+3
source share
2 answers

The documentation at https://docs.docker.com/compose/compose-file/#/externallinks says

 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. 

Example:

Create a new docker network

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 
+8
source

Yuva's answer above for version 2 is also suitable for version 3.

The documentation for external_inclusions is not clear enough.

For clarity, I inserted a version 3 option with annotation

 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 
+1
source

Source: https://habr.com/ru/post/1274727/


All Articles