19

Suppose I have a docker compose file with two containers. Both reference each other in their /etc/hosts file. Container A has a reference for container B and vice versa. And all of this happens automatically. Now I want to add one or more hostnames to B in A's hosts file. How can I go about doing this? Is there a special way I can achieve this in Docker Compose?

Example:

172.0.10.166 service-b my-custom-hostname

saada
  • 2,612
  • 3
  • 27
  • 33

3 Answers3

35

Yes. In your compose file, you can specify network aliases.

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

In this example, the db service could be reached by other containers on the default network using db, database, or postgres.

You can also add aliases to running containers using the docker network connect command with the --alias= option.

Pang
  • 9,564
  • 146
  • 81
  • 122
Steve
  • 6,618
  • 3
  • 44
  • 42
  • Are you sure that you can add DNS alias when container is running? The closest option that I have achieved is to still disconnect container from the network and add it again with alias. Commands: `docker network list` , `docker network disconnect dns-alias-network my-centos-container` , `docker network connect --alias my-centos-alias dns-alias-network my-centos-container` . Any options to avoid disconnecting container? - `Error response from daemon: endpoint with name my-centos already exists in network dns-alias-network` – laimison May 15 '19 at 11:31
  • @Laimis Yes, you can alias a container while it is running; disconnecting it from the network doesn't stop the container. I don't know if there is a way to add the alias without disconnecting the container, though. – Steve May 16 '19 at 13:54
  • @Steve yeah, that disconnect & connect trick worked. Would be curious if this is hard network glitch. For instance, the question is whether TCP connections are just delayed for milliseconds/reconnected gracefully or not. – laimison May 16 '19 at 14:22
  • You save my bacon hehehe – Henrique Felix May 26 '23 at 09:10
10

Docker compose has an extra_hosts feature that allows additional entries to be added to the container's host file.

Example

docker-compose.yml

web1:
  image: tomcat:8.0
  ports:
    - 8081:8080
  extra_hosts:
    - "somehost:162.242.195.82"
    - "otherhost:50.31.209.229"
web2:
  image: tomcat:8.0
  ports:
    - 8082:8080
web3:
  image: tomcat:8.0
  ports:
    - 8083:8080

Demonstrate host file entries

Run docker compose with the new docker 1.9 networking feature:

$ docker-compose --x-networking up -d
Starting tmp_web1_1
Starting tmp_web2_1
Starting tmp_web3_1

and look at the hosts file in the first container. Shows the other containers, plus the additional custom entries:

$ docker exec tmp_web1_1 cat /etc/hosts 
..
172.18.0.4  web1
172.18.0.2  tmp_web2_1
172.18.0.3  tmp_web3_1
50.31.209.229   otherhost
162.242.195.82  somehost
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • Close! Thanks for the writeup. What I really want is this: 172.18.0.2 tmp_web2_1 otherhost somehost – saada Nov 13 '15 at 16:06
  • 1
    I want to reference a container not some static IP address – saada Nov 13 '15 at 16:07
  • @saada Ahh, gotcha you want aliases. Don't think that is supported – Mark O'Connor Nov 13 '15 at 16:11
  • @saada You could fudge it by specifying a container name: https://docs.docker.com/compose/compose-file/#container-name – Mark O'Connor Nov 13 '15 at 16:12
  • Ah but using container name limits the scale feature which is a nono for me. I guess this is worth putting on the compose issues – saada Nov 13 '15 at 16:14
  • @saada It's tricky either way. If you do want container scaling, you can't have a fixed name either, explaining why compose appends a number to the generated container name. – Mark O'Connor Nov 13 '15 at 16:20
2

If I understand your question correctly, you can pass a host name referenced in your host's /etc/hosts file via --add-host flag :

$ docker run ... --add-host="droid" 

Your host's /etc/hosts would need the following entry: xx.xx.xx.xx droid

Of course, xx.xx.xx.xx will need to be reachable from inside the container you just started using the 'docker run' command. You can have one or more --add-host="xyz".

More details about --add-host here:

http://docs.docker.com/v1.8/reference/commandline/run/

Kartik Pandya
  • 2,718
  • 2
  • 14
  • 28