7

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.

I get

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

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:

App:

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
Chris
  • 3,581
  • 8
  • 30
  • 51

3 Answers3

17

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.

Ex:

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
YYY
  • 6,107
  • 1
  • 12
  • 14
  • Hi. Can you give me an example on how to connect them to the same network? – Chris Dec 07 '16 at 09:02
  • that solved my issue. Awesome answer @yuva. Works with version 3 as well. – Lukas Lukac Jul 17 '17 at 13:52
  • 3
    Can't realize for what `external_links` needed at all? If you join both `compose`s to same networks, e.g. `docker-compose-1` and `docker-compose-2` both have `networks: {custom: {external: true}}`, then services defined in each other already reachable by they names without `external_links`. Is it just for stating dependencies explicitly? – Anthony Jul 18 '18 at 22:17
  • @tosh Perhaps another benefit is you can define an alias for the external network: this means you can separate the configuration between what makes sense in naming your networks/projects and what the container sees: `external_links: ... -shared_network_service:what_the_container_sees` – Jonathan Crooke Sep 15 '18 at 09:04
6

Yuva's answer above for the version 2 holds good for version 3 as well.

The documentation for the external_links isn't clear enough.

For more clarity I pasted the version 3 variation 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
2

Recently I faced Name resolution failure trying to link 2 containers handled by docker-compose v3 representing gRPC server and client in my case, but failed and with external_links.

I'll probably duplicate some of the info posted here, but will try to summarize as all these helped me solving the issue.

From external_links docs (as mentioned in earlier answer):

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


The following configuration solved the issue.

project-grpc-server/docker-compose.yml

version: '3'
services:
    app:
        networks:
            - some-network
networks:
    some-network:

Server container configured as expected.


project-grpc-client/docker-compose.yml

services:
    app:
        external_links:
            # Assigning easy alias to the target container
            - project-grpc-server_app_1:server
        networks:
            # Mentioning current container as a part of target network
            - project-grpc-server_some-network
networks:
    # Announcing target network (where server resides)
    project-grpc-server_some-network:
        # Telling that announced network already exists (shouldn't be created but used)
        external: true

When using defaults (no container_name configured) the trick with configuring client container is in prefixes. In my case network name had prefix project-grpc-server_ when working with docker-compose and than goes the name itself some-network (project-grpc-server_some-network). So fully qualified network names should be passed when dealing with separate builds.

While container name is obvious as it appears from time to time on the screen the full network name is not easy-to-guess candidate when first facing this section of Docker, unless docker network ls.

I'm not a Docker expert, so please don't judge too strict if all this is obvious and essential in Docker world.

Paul T. Rawkeen
  • 3,994
  • 3
  • 35
  • 51
  • I am still getting error of undefined network which is "project-grpc-server_some-network" in your case – etotientz Sep 08 '20 at 16:10
  • @etotientz Strange it is. Are you sure your "server" network name is the same as you mentioned it in "client" _docker-compose.yml_ (_try `docker network ls` to find this out_)? Also to avoid automatic prefixes which are named after folder name which project is in you can set `COMPOSE_PROJECT_NAME=my-server` e.g. in you `.env` file and you will have fixed prefix for your containers so network names can be more predictable as well. – Paul T. Rawkeen Sep 08 '20 at 18:27
  • use services.app.container_name=app to pin the name of your container. – user528025 Aug 07 '21 at 21:13