237

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.

Zeitounator
  • 38,476
  • 7
  • 53
  • 66
David Medinets
  • 5,160
  • 3
  • 29
  • 42
  • 1
    What version of Compose are you using? – kojiro Apr 28 '15 at 17:51
  • 1
    It may be worth noting that if `version:` isn't specified then docker-compose assumes the file format is v1; see https://docs.docker.com/compose/compose-file/compose-versioning/#versioning – Neil C. Obremski Apr 16 '21 at 22:49
  • This issue is still open here: https://github.com/docker/compose/issues/2925 You can set hostname but it is not reachable from other containers. So it is mostly useless. – bayeslearner Nov 08 '20 at 14:43

6 Answers6

155

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
Adrian W
  • 4,563
  • 11
  • 38
  • 52
improbable
  • 2,308
  • 2
  • 15
  • 28
  • 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 '22 at 16:32
  • @rfay Your comment is factually wrong. – improbable Apr 28 '23 at 19:08
  • 2
    There is no use for the `version:` key any more. https://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 '23 at 03: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.

foz
  • 3,121
  • 1
  • 27
  • 21
  • 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 09 '18 at 03: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 06 '19 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 '20 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
larsks
  • 277,717
  • 41
  • 399
  • 399
  • 1
    What version of Compose are you using? – kojiro Apr 28 '15 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? – George Stocker Nov 24 '15 at 03:08
  • 3
    YML files aren't really all that finicky. What does "doesn't work" mean? According to [the docs](https://docs.docker.com/compose/compose-file/), 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 '15 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 06 '16 at 08: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 06 '16 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.

Marcello Romani
  • 2,967
  • 31
  • 40
  • 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 '17 at 10:13
  • 8
    If you need well-known names for containers that's a viable solution. – Marcello Romani Oct 02 '17 at 11:48
  • This is exactly what I needed: You can find the documentation here: https://docs.docker.com/compose/compose-file/#domainname-hostname-ipc-mac_address-privileged-read_only-shm_size-stdin_open-tty-user-working_dir – thejinx0r Oct 25 '20 at 03: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.

K.J.
  • 921
  • 7
  • 7
  • 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 '19 at 13:15
  • Another solution that also worked, was to use the `aliases` property as described in the `foz` solution. – Stephane Nov 15 '19 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 '20 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`. – Neil C. Obremski Apr 16 '21 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
Roy Golan
  • 113
  • 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 '18 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 '19 at 07:21
  • exposing an env var has nothing to do with the hostname of the container, they are two separate realms. – Marcello Romani Jun 12 '20 at 11:00