compose: Service can't be reached by defined hostname
Given the following docker-compose.yml file:
version: '2'
networks:
mynet:
driver: bridge
services:
master:
image: busybox
command: top
hostname: amasterservice
networks:
- mynet
slave:
image: busybox
command: top
networks:
- mynet
The following ping fails:
$ docker exec -ti test_slave_1 ping amasterservice
ping: bad address 'amasterservice'
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 32
- Comments: 45
Links to this issue
Commits related to this issue
- Fix using a custom hostname for a node in the integrations tests. See https://github.com/docker/compose/issues/2925 — committed to vitlibar/ClickHouse by deleted user 5 years ago
If goal of Docker Compose is to make it easier for users to deploy a composition of services using Docker images and containers, then the file definition should be crystal clear.
When a user defines a hostname to a service, it will expect that hostname to be resolvable from other services, specially when these services participate in the same network (as per my example).
Since a service can have multiple instances, it is just logical that when I define a hostname to a service, that hostname resolves to multiple instances (thus using the new DNS feature in latest version).
If nothing of these can be implemented today, or should not be implemented at all, then it is a matter of clarifying the documentation to state either:
It’s long been an annoyance for me that compose only creates the DNS container name aliases when using
docker-compose up
, and not when usingdocker-compose run
.The
--name
trick solves it, but it feels like this shouldn’t be necessary.Any update on this issue?
As a Docker Composer user, I expect that whenever I set a hostname to a service, that hostname should resolve to any instance of that service. Same should apply for the service name.
If these things should not work the same way, and a user should only rely on service name, then hostname definition should not be allowed in the compose file. It just creates confusion.
What is the last solution to dns resolve for services with hostname or domainname in the same compose file ???
After using docker for years, this docker-compose bug is a surprise.
and as expected the container is resolvable on
somebridgenetwork
bymyhostname
but notmyname
on the networkSurprisingly, in docker-compose, the container would be resolvable by
myname
but notmyhostname
on the network.Side note: These are two orthognal concepts: container names are meant for container management and scoped to a Docker instance, hostnames are meant for network connectivity and scoped to a network.
May I ask what is the best practice to resolve this hostname resolving issue in 2018? Should
hostname
be specified in the compose file?Looking at
docker inspect test_master_1
, I see two aliases:The first comes from the service name, and the second is the container’s short ID. I think that by default Compose doesn’t create aliases based on hostnames.
May I ask what you’re trying to accomplish? Why can’t the hostname also be “master”, and why does the slave need to ping the master by hostname rather than by service name?
What a mess
We should probably add a warning to the docs about using
hostname
. I think it is rarely useful.It’s year 2020 and the issue seems to persist still. I need hostname with domainname to work. This is NOT a feature but a bug!!! Docker-compose version 3.4
Containers are spun up by docker-compose up.
works:
<hostname>.<domainname>
, i.e. ambari.devdoesn’t work:
<hostname>.<domainname>
, i.e. ambari.dev<hostname>
, i.e. ambariA work around is to use
<hostname>.<domainname>
as the service name but this brings out a whole bunch of other issues, e.g. this is not allowed in swarm-kit (https://github.com/docker/swarmkit/issues/2437)@slothkong Also, do not mistake to refer service name not image name (as I did 😦 ):
Mistake: MONGODB_URL=mongodb://mongo:27017 Correct: MONGODB_URL=mongodb://mongodb:27017
This burnt almost 2 days of development on my end (with very cryptic failures).
Do you by chance know if
docker-compose exec
is also affected?This unexpected behavior still exists. The best solution I have found is this SO answer
Still doesn’t work…
@slothkong
I solved this problem. When using
driver: bridge
, I use the--add-host hostname:172.x.x.x
parameter in thedocker run
command.The
docker run
needs to specify the IP (--ip
) and network name (--net
) for the container.Not really. With overlay network, the hostname set in a container is reachable by another container. So I disagree that it works as “expected”. In fact, what users expect (and think about new users), is that when a hostname is set, specially in the case of Docker Compose, that hostname can be used by other containers.
Then the feature should work as expected by users using Compose for the first time, by adding that information to the list of alises.
Could you please be more specific where, although rare, the current form is useful?
I think it should not be allowed at all.
There was some discussion early on about using
hostname
instead ofcontainer_name
as the default name used by docker, but that didn’t happen (I’m not sure why).We add aliases for service name and short id to be backwards compatible and handle the common use cases. We’re also adding support for your own net-aliases in #2829
As far as I know, it’s not all that uncommon for the internal hostname to be unresolvable from the outside.
We could probably make it another alias, but I’m not sure how we’d deal with conflicts with the service names.
Hi @jake-low, I think question is why isn’t hostname defined under aliases. What is the reasoning behind this decision?