Docker Community Forums

Share and learn in the Docker community.

Container available by IP but not by name

I ran 2 containers:

1.docker run -p 5432:5432 -e POSTGRES_PASSWORD=qwe -n postgres postgres

2.docker run -e PGADMIN_DEFAULT_EMAIL=qwe@qwe.qw -e PGADMIN_DEFAULT_PASSWORD=123 -e PGADMIN_LISTEN_PORT=4422 --expose 4422 -p 4422:4422 dpage/pgadmin4

When I tried to connect to postgres, I was surprised that I can connect from pgadmin to postgres by internal IP address, but can’t connect by container name postgres.

There is a ping screenshot:

IP address is correct:
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' postgres

Could someone explain, please, why the container available by IP but not available by name?

Create a new bridge network other than docker0, run your containers inside it and you can reference the containers inside that network by their names.

Docker daemon runs an embedded DNS server to provide automatic service discovery for containers connected to user-defined networks. Name resolution requests from the containers are handled first by the embedded DNS server.

Try this:

docker network create
docker run --net --name test busybox nc -l
docker run --net busybox ping test
First, we create a new network. Then, we run a busybox container named test listening on port 7000 (just to keep it running). Finally, we ping the test container by its name and it should work.

1 Like

I launched the containers via docker compose (which means, as I understand it, they all run on a shared separate network) and everything is fine: the call by name works. I used to think it worked out of the box with no extra effort. I was wrong.

Thank you for the answer!

As a rule of thumb if you have two containers that rely on each other launch them using a docker-compose file and they can reach each other by their service names