I've had db and server container, both running in the same network. Can ping db host by its container id with no problem.
When I set a hostname for db container manually (-h myname
), it had an effect ($ hostname
returns set host), but I can't ping that hostname from another container in the same network. Container id still pingable.
Although it works with no problem in docker compose.
What am I missing?

- 87
- 3
- 9
3 Answers
Hostname is not used by docker's built in DNS service. It's a counterintuitive exception, but since hostnames can change outside of docker's control, it makes some sense. Docker's DNS will resolve:
- the container id
- container name
- any network aliases you define for the container on that network
The easiest of these options is the last one which is automatically configured when running containers with a compose file. The service name itself is a network alias. This lets you scale and perform rolling updates without reconfiguring other containers.
You need to be on a user created network, not something like the default bridge which has DNS disabled. This is done by default when running containers with a compose file.
Avoid using links since they are deprecated. And I'd only recommend adding host entries for external static hosts that are not in any DNS, for container to container, or access to other hosts outside of docker, DNS is preferred.

- 231,797
- 42
- 475
- 450
-
Setting `--network-alias` on the 1st container and referring to this name when trying to connect to it from 2nd container helped in my case. – Bojan Komazec Jul 30 '19 at 15:22
-
I was struggling with `--network-alias`, didn't know it doesn't work with default bridge network. https://docs.docker.com/network/bridge/#differences-between-user-defined-bridges-and-the-default-bridge – nakamume Oct 09 '21 at 08:40
As stated in the docker docs, if you start containers on the default bridge network, adding -h myname
will add this information to
- /etc/hosts
- /etc/resolv.conf
- and the bash prompt
of the container just started.
However, this will not have any effect to other independent containers. (You could use --link
to add this information to /etc/hosts of other containers. However, --link
is deprecated.)
On the other hand, when you create a user-defined bridge network, docker provides an embedded DNS server to make name lookups between containers on that network possible, see Embedded DNS server in user-defined networks. Name resolution takes the container names defined with --name
. (You
will not find another container by using its --hostname
value.)
The reason, why it works with docker-compose is, that docker-compose creates a custom network for you and automatically names the containers.
The situation seems to be a bit different, when you don't specify a name for the container yourself. The run reference says
If you do not assign a container name with the --name option, then the daemon generates a random string name for you. [...] If you specify a name, you can use it when referencing the container within a Docker network.
In agreement with your findings, this should be read as: If you don't specify a custom --name
, you cannot use the auto-generated name to look up other containers on the same network.

- 4,569
- 4
- 31
- 42
-
Thanks for answer. > You could use --link Docker documentation says that its legacy feature (https://docs.docker.com/network/links/), not sure that is it will be correct to use it. > docker-compose creates a custom network for you I've specified same network as external in docker-compose config and use it, so the network should be the same as manually running. Also, I've discovered, that when setting container name manually the name resolves in other containers (in the same net). But auto-generated names doesn't resolves. – Eugene Nov 18 '18 at 22:03
-
Yes, `--link` is deprecated, that's why I put it in parenthesis, maybe I should have said that explicitly. To comment about your specific issues when using external networks, could you post more information, e.g. your actual docker-compose.yml? – sauerburger Nov 18 '18 at 22:14
-
That's easy to show: - create a network: `$ docker network create test`; - run first container: `$ docker run -itd --network test --name database alpine`; - run second container: `$ docker run -it --network test alpine`; - and ping the first one: `$ ping database`. But if we run first without specified name, we can't ping it's name from second one (but can ping it's id). – Eugene Nov 18 '18 at 22:15
-
A general comment about stackoverflow: You should add this information to the initial question, so that others can benefit from the question more easily. Back to the topic: I can't follow your example. If you start the containers without giving them names, what name do you use to ping it? – sauerburger Nov 20 '18 at 10:22
-
I'll take into account. When starting without giving names used default name generated by docker. – Eugene Nov 20 '18 at 18:44
-
I've updated the answer with information about the case when you omit `--name`. – sauerburger Nov 21 '18 at 13:56
-
Well, together with another answer, that explains all unclear moments. I appreciate your help. – Eugene Nov 21 '18 at 22:15
I've found out, that problem can be solved without network using --add-host option. Container's IP can be gain using inspect command.
But when containers in the same network, they are able to access each other via it names.

- 87
- 3
- 9