Docker-compose external_links not able to connect

docker-compose network
docker-compose secrets
docker-compose volumes
docker-compose build args
docker-compose dockerfile
docker-compose create network if not exists
docker-compose bind mount
docker-compose connect to container

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

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

Docker Compose: Link containers outside compose file using , Linking containers without Docker Compose is easy using the –link But how can you link containers not defined in the same docker compose file? Docker Compose offers the “external_links” configuration option which� external_links. Link to containers started outside this docker-compose.yml or even outside of Compose, especially for containers that provide shared or common services. external_links follow semantics similar to the legacy option links when specifying both the container name and the link alias (CONTAINER:ALIAS).

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

Compose file version 3 reference, Set the network containers connect to for the RUN instructions during build. Because Docker container names must be unique, you cannot scale a service common services. external_links follow semantics similar to the legacy option links� docker-compose version 1.6.2, build 4d72027; Problem Description: The docker-compose linking does not work whereas the command-line equivalent works. Reproducible Testcase: I am producing a reproducible test case using linuxconfig/apache image that acts as a docker daemon or as a simple "web" container service listening on port 5000.

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.

Compose file version 2 reference, mc0e changed the title external_links was working, but now not setting then when attempting to perform an external link in docker-compose up I get this error. ERROR: for devenv_reinit_pg_1 Cannot start service reinit_pg:� Docker: Using docker-compose and networking to link a Spring Boot app to an external service dependency In earlier versions of Docker, links were used to connect two containers by enabling network access as well as sharing environment variables.

external_links was working, but not now � Issue #2106 � docker , Hello, I'm trying to use external_links to link to a container outside of the compose network. However it does not work. One solution is to add� We did not specify any network in the postgres container that we created and hence it would use the default docker bridge. If we run containers mentioned in our docker-compose file in this default network they would be able to communicate with each other. Below is a docker-compose file for that. version: '3' services: web: build: .

external_links does not link to bridge � Issue #3634 � docker/compose, Cattle adopts the standard Docker Compose terminology for services and becomes unavailable, a container fails, or is unable to meet a health check. Linking services will link all containers in the service to all containers in the linked service. for DNS resolution, you can use external_links in the docker-compose. yml . Docker networking feature can be accessed by using a --link flag which allows to connect any number of docker containers without the need to expose container's internal ports to the outside world. In this config you will learn how to link two or more docker containers together using a simple docker networking technique.

Adding Services with Rancher Compose, We can utilize Docker Compose in new and interesting (and even some These tools will poll the hosts and ports until TCP connections can be Recall how Docker Compose passes an empty string to any environment variable you do not define. expose , external_links , dns , dns_search , and tmpfs ), Docker Compose� To see a detailed list of changes for past and current releases of Docker Compose, refer to the CHANGELOG. Getting help. Docker Compose is under active development. If you need help, would like to contribute, or simply want to talk about the project with like-minded individuals, we have a number of open channels for communication.

Comments
  • docs.docker.com/install/linux/linux-postinstall - a manual on how to make docker run without sudo.
  • Hi. Can you give me an example on how to connect them to the same network?
  • that solved my issue. Awesome answer @yuva. Works with version 3 as well.
  • Can't realize for what external_links needed at all? If you join both composes 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?
  • @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