238

In my docker-compose.yml file, I have the following. However the container does not pick up the hostname value. Any ideas?

dns:
  image: phensley/docker-dns
  hostname: affy
  domainname: affy.com
  volumes:
    - /var/run/docker.sock:/docker.sock

When I check the hostname in the container it does not pick up affy.

3

6 Answers 6

156

As of Docker Compose file version 3.0 and later, you can just use the hostname key:

version: "3.0"
services:
  yourservicename:
    hostname: your-name
2
  • Hmm, docker-compose is not to v3.0 at this time. V2.x is current. Also note that the version key is no longer needed in docker-compose.yaml at all. (The version key was not the docker-compose version when it still existed, but the compose spec version.)
    – rfay
    Sep 30, 2022 at 16:32
  • 3
    There is no use for the version: key any more. docs.docker.com/compose/compose-file/04-version-and-name "Top-level version property is defined by the specification for backward compatibility but is only informative."
    – rfay
    Apr 30 at 3:01
93

I found that the hostname was not visible to other containers when using docker run. This turns out to be a known issue (perhaps more a known feature), with part of the discussion being:

We should probably add a warning to the docs about using hostname. I think it is rarely useful.

The correct way of assigning a hostname - in terms of container networking - is to define an alias like so:

services:
  some-service:
    networks:
      some-network:
        aliases:
          - alias1
          - alias2

Unfortunately this still doesn't work with docker run. The workaround is to assign the container a name:

docker-compose run --name alias1 some-service

And alias1 can then be pinged from the other containers.

UPDATE: As @grilix points out, you should use docker-compose run --use-aliases to make the defined aliases available.

3
  • 21
    I'm stuck with that problem that I can't access container by hosname from other containers. And you're the only one in whole internet who stated this problem.. been googling for 20 hours already
    – holms
    Apr 9, 2018 at 3:47
  • 11
    Same here! It's insane that millions of developers seem to use Docker, but nobody knows how to give a box a simple name, not even the offiicial docs. Thank you so much :)
    – Sliq
    Jun 6, 2019 at 14:41
  • 1
    Just in case, now we can use --use-aliases with docker-compose run. This will avoid hardcoding the alias on the run command
    – grilix
    May 31, 2020 at 23:03
64

This seems to work correctly. If I put your config into a file:

$ cat > compose.yml <<EOF
dns:
  image: phensley/docker-dns
  hostname: affy
  domainname: affy.com
  volumes:
    - /var/run/docker.sock:/docker.sock
EOF

And then bring things up:

$ docker-compose -f compose.yml up
Creating tmp_dns_1...
Attaching to tmp_dns_1
dns_1 | 2015-04-28T17:47:45.423387 [dockerdns] table.add tmp_dns_1.docker -> 172.17.0.5

And then check the hostname inside the container, everything seems to be fine:

$ docker exec -it stack_dns_1 hostname
affy.affy.com
5
  • 1
    What version of Compose are you using?
    – kojiro
    Apr 28, 2015 at 17:52
  • This format doesn't work in the latest version of docker-compose. YML files are finicky things; are you sure this is the right format for dns? Nov 24, 2015 at 3:08
  • 3
    YML files aren't really all that finicky. What does "doesn't work" mean? According to the docs, both hostname and domainname are valid docker-compose.yml options. Update: just tested, still seems to work just fine (docker-compose version 1.4.2, docker version 1.8.2).
    – larsks
    Nov 24, 2015 at 12:45
  • Is there any way to expose this hostname outside of the docker environment? It would be nice if the host could access the docker containers by their dns name.
    – Paul Praet
    Apr 6, 2016 at 8:21
  • 1
    That is possible, and probably worth it's own question. If you search for "docker dns" you will find several relevant results; take a look at what's out there first.
    – larsks
    Apr 6, 2016 at 11:27
50

Based on docker documentation: https://docs.docker.com/compose/compose-file/#/command

I simply put hostname: <string> in my docker-compose file.

E.g.:

[...]

lb01:
  hostname: at-lb01
  image: at-client-base:v1

[...]

and container lb01 picks up at-lb01 as hostname.

3
  • if you have multiple containers in the docker-compose file, would you set the hostname for each container? that seems very inefficient?
    – vgoklani
    Sep 19, 2017 at 10:13
  • 8
    If you need well-known names for containers that's a viable solution. Oct 2, 2017 at 11:48
  • This is exactly what I needed: You can find the documentation here: docs.docker.com/compose/compose-file/…
    – thejinx0r
    Oct 25, 2020 at 3:20
14

The simplest way I have found is to just set the container name in the docker-compose.yml See container_name documentation. It is applicable to docker-compose v1+. It works for container to container, not from the host machine to container.

services:
  dns:
    image: phensley/docker-dns
    container_name: affy

Now you should be able to access affy from other containers using the container name. I had to do this for multiple redis servers in a development environment.

NOTE The solution works so long as you don't need to scale. Such as consistant individual developer environments.

4
  • 1
    In Compose 3.7 the only way I got the database service hostname to be seen from within another service container of the same stack was to name the service as the hostname. Using the hostname: property failed. Using the environment HOSTNAME= construct failed too.
    – Stephane
    Nov 15, 2019 at 13:15
  • Another solution that also worked, was to use the aliases property as described in the foz solution.
    – Stephane
    Nov 15, 2019 at 13:20
  • 1
    This works great but is a bit messy - I've now taken to setting container_name and hostname to the same value to achieve what I need
    – Oly Dungey
    Mar 11, 2020 at 11:21
  • One problem with container_name is that it's global to the local host system so you run into conflicts when trying to run multiple stacks and two different services have the same container_name. Apr 16, 2021 at 22:45
3

I needed to spin freeipa container to have a working kdc and had to give it a hostname otherwise it wouldn't run. What eventually did work for me is setting the HOSTNAME env variable in compose:

version: 2
services:
  freeipa:
    environment:
      - HOSTNAME=ipa.example.test

Now its working:

docker exec -it freeipa_freeipa_1 hostname
ipa.example.test
3
  • 4
    For me, using slim, that results in a HOSTNAME environment variable that has no effect at all on the actual hostname of the container
    – Oly Dungey
    Oct 18, 2018 at 13:52
  • 3
    While this may have worked with the container you were using, the environment directive only exposes variables to the container. It's still up to the container to do something with the value that's set. In many cases it will likely do nothing.
    – MrJohnBBQ
    Feb 25, 2019 at 7:21
  • exposing an env var has nothing to do with the hostname of the container, they are two separate realms. Jun 12, 2020 at 11:00

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.