28

In my docker-compose file there is a need for several containers to know the hostname of a specific container, including this specific container.

Links will not work, since a container can not link to itself.

Basically, what I am looking for is a way to alias localhost in docker-compose.

galusben
  • 5,948
  • 6
  • 33
  • 52
  • Could you make it work with using network aliases(localhost alias)? When i use localhost alias it doesn't work as expected, but another alias works. – adem caglin Jun 10 '17 at 15:42

4 Answers4

26

I think the correct answer is from

Aliases can be defined as part of the network declaration for a service. See aliases in Compose file reference for more details on that. – King Chung Huang Apr 24 '17 at 15:18

here is the example from the doc

version: '2'

services:
  web:
    build: ./web
    networks:
      - new

worker:
  build: ./worker
  networks:
    - legacy

db:
  image: mysql
  networks:
    new:
      aliases:
        - database
    legacy:
      aliases:
        - mysql

networks:
  new:
  legacy:

you can access the db in this docker-compose, also you can use mysql to connect this db

wassgren
  • 18,651
  • 6
  • 63
  • 77
penny chan
  • 769
  • 1
  • 10
  • 15
  • In your example, can do container access itself by the hostname 'database'? If not it does not answer the question. – galusben Mar 23 '18 at 20:58
  • 4
    Worth noting: you don't have to specify a new network if you only need an extra alias. You can just specify `default` as the network instead of e.g. `new` in your example. – user8675309 Jan 22 '19 at 22:48
  • 3
    `localhost` does not seem to work as an alias as it always resolves to 127.0.0.1 – Martynas Jusevičius Oct 30 '19 at 10:23
  • `aliases` with only one `s` I cannot edit myself, the cue is full I reckon by lots of people trying to fix the same typo. – pykiss Aug 11 '21 at 13:09
23

You should avoid using links. Instead, services on the same Docker network can find each other by using service names as DNS names. Use that to reference the specific container you described, including when it references itself.

For example, in the following made up Docker Compose file, if someservice was a web server serving on port 80, anotherservice service would be able to connect to it at http://someservice/, because they're on a common network the_net.

version: '3'

services:
  someservice:
    image: someserviceimage
    networks:
      - the_net

  anotherservice:
    image: anotherserviceimage
    networks:
      - the_net

networks:
  the_net:

someservice can also reach itself at http://someservice/.

NHG
  • 5,807
  • 6
  • 34
  • 45
King Chung Huang
  • 5,026
  • 28
  • 24
  • 8
    Thanks, but what if I want an alias? Isn't a link is just an alias? For example, anotherservice needs to access someservice, but I do not want to use the name another service but the name 'specialservice' instead. Isn't that what links are for? Also, why not using links? what is the problem with them? – galusben Apr 24 '17 at 06:58
  • 4
    Why should you avoid using links? – Eldad Assis Apr 24 '17 at 08:58
  • Links are from the Docker Compose version 1 format, and are superseded by the use of service names over declared networks. See [Link environment variables](https://docs.docker.com/compose/link-env-deprecated/) and [links in Compose file reference](https://docs.docker.com/compose/compose-file/#links) for more details. – King Chung Huang Apr 24 '17 at 15:09
  • Depending on how you define networks, link-based services might not be protected from other containers. And, links are ignored in Docker stack deployments. For both reasons, links should be avoided in current Docker Compose files. – King Chung Huang Apr 24 '17 at 15:16
  • 2
    Aliases can be defined as part of the network declaration for a service. See [aliases in Compose file reference](https://docs.docker.com/compose/compose-file/#aliases) for more details on that. – King Chung Huang Apr 24 '17 at 15:18
  • Thanks for this explanation. However, I am unable to get this to work as expected. I try to link up my db to my web container and it fails but only in the container. It works just fine otherwise... – Chris Aug 09 '17 at 17:33
  • Service name resolution does not work when a compose configuration is deployed to Amazon ECS via `ecs-cli compose up...` – Chris Wolf Apr 30 '20 at 04:28
22

extra_hosts did the trick for me.

extra_hosts:
    - "hostname:127.0.0.1"

From the docker-compose docs:

extra_hosts Add hostname mappings. Use the same values as the docker client --add-host parameter.

extra_hosts: - "somehost:162.242.195.82" - "otherhost:50.31.209.229" An entry with the ip address and hostname will be created in /etc/hosts inside containers for this service, e.g:

162.242.195.82 somehost 50.31.209.229 otherhost

Soviut
  • 88,194
  • 49
  • 192
  • 260
galusben
  • 5,948
  • 6
  • 33
  • 52
5

Here is one more useful parameter external_links that didn't mentioned in this thread:

external_links:
  - redis_1
  - project_db_1:mysql
  - project_db_2:postgresql
  - {CONTAINER}:{ALIAS}

It is close to extra_hosts:

extra_hosts:
  - "somehost:127.0.0.1"
  - "otherhost:127.0.0.2"
  - {HOSTNAME}:{IP}

but allow to set dynamic container IPs

In addition of it aliases may be added at network level. Here is two answers [ 1, 2 ] how to add alias to network, but I want to add extra info:

If you didn't set network in docker file you may use default to add alias to default network:

services:
  db:
    networks:
      default:
        aliases:
          - database
          - postgres

Thanks to this answer

rzlvmp
  • 7,512
  • 5
  • 16
  • 45