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
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.
version: '3'
services:
app:
networks:
- some-network
networks:
some-network:
Server container configured as expected.
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.