Compose: Adding new link does not update /etc/hosts with name specified in docker-compose.yml

Created on 14 Sep 2015  路  11Comments  路  Source: docker/compose

Starting with a docker-compose file with an apache container specified:

apache:
  image: apache
  ports:
    - "80:80"

After I initially docker-compose up on this file, if I go back in and make the docker-compose file look like this:

apache:
  image: apache
  ports:
    - "80:80"
  links:
    - "reviewservice"
  volumes:
    - ./hosts:/etc/apache2/sites-enabled

reviewservice:
  image: reviewservice
  ports:
    - "3000:3000"

If I exec into the apache container after stopping apache then docker-compose up, I do not see an entry for reviewservice in the /etc/hosts, only entries for docker compose generated names.

172.17.0.46 5dabc584af18
127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.44 cosmos_reviewservice_1 0e0e53f405a2
172.17.0.46 cosmos_apache_1
172.17.0.46 cosmos_apache_1.bridge
172.17.0.44 cosmos_reviewservice_1
172.17.0.44 cosmos_reviewservice_1.bridge

There is no entry for just reviewservice, which is how my code in the apache container is expecting to reference my review service.
However, if I docker-compose rm apache, then docker-compose up and check the /etc/host, the entry for reviewservice is there and everything works.

172.17.0.46 5dabc584af18
127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.44 reviewservice_1 0e0e53f405a2 cosmos_reviewservice_1
172.17.0.44 cosmos_reviewservice_1 0e0e53f405a2
172.17.0.44 reviewservice 0e0e53f405a2 cosmos_reviewservice_1
172.17.0.46 cosmos_apache_1
172.17.0.46 cosmos_apache_1.bridge
172.17.0.44 cosmos_reviewservice_1
172.17.0.44 cosmos_reviewservice_1.bridge

I'm assuming that i should not have to rm the apache image everytime I update a new link in the yml file.

kinbug

Most helpful comment

@juanpastas, @xhs, @adyach:

If you're using a version 2 Compose file, no /etc/hosts entries will be created. This is not a bug. Version 2 files make use of Docker networking, which uses DNS instead of /etc/hosts to resolve hostnames.

All 11 comments

Duplicate of #1757 (it reads a bit different, but the issue is the same)
Fixed by #1960

Will be in the 1.4.1 release (which should be out soon), but you can always try master if you'd like.

A workaround is to docker-compose stop && docker-compose rm after making a change like this, or using up --force-recreate

If you're able to reproduce this issue with the 1.4.1 release or master, please do re-open this issue and I can look into it further

I am having a similar issue on 1.7.1.

My yml:

version: '2'
services:
  db:
    image: postgres
    container_name: db
  redis:
    image: redis
    container_name: redis
  web:
    build: .
    container_name: web
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    # command: bundle exec rails c
    # volumes:
    #   - .:/home/rails/webapp
    ports:
      - "3000:3000"
    links:
      - db
      - redis
    # environment:
    #   - CACHE_URL=redis://redis:6379

If I run with docker run --name redis redis I get a redis host:

docker run --link redis web cat /etc/hosts
127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2  redis 0819c02bcc89
172.17.0.3  977d7884aa80

Here is what I get after docker-compose up:

docker exec web cat /etc/hosts
127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.18.0.4  ba6f6900c0e2

I tried deleting all containers docker rm -f $(docker ps -a -q) and then docker-compose up --force-recreate and then query hosts again. Same.

Tried with docker-compose stop && docker-compose rm and docker-compose up --force-recreate. Then docker exec web cat /etc/hosts yields the same.

Same problem here. No entry is created in hosts on 1.7.1.

version: '2'
services:
    thumbnail_service:
        image: zhaozhao/thumbnail_service:1
        restart: always
        links:
            - pilbox_service
            - object_service
        environment:
            - PILBOX_BASE=http://pilbox_service:80
            - OBJECT_BASE=http://object_service:80
            - SERVICE_NAME=thumbnails
            - SERVICE_TAGS=standby-v1-exposed
        volumes:
            - /tmp:/cache_store
        ports:
            - 80
    pilbox_service:
        image: zhaozhao/pilbox:1
        restart: always
    object_service:
        image: nginx:latest
        restart: always
        volumes:
            - /tmp:/usr/share/nginx/html

Post my compose file here.

I have the exact same problem, using:

  • Docker: Docker version 1.11.2, build b9f10c9
  • Docker Compose: docker-compose version 1.6.2, build 4d72027

There are no entries in /etc/hosts file for linked services!

File docker-compose.yml:

version: '2'
services:
  database:
    image: imageblah1
    restart: unless-stopped
    environment:
     - MYSQL_ROOT_PASSWORD=123456
    ports:
     - "3306:3306"
  giacoop:
    image: imageblah2
    restart: unless-stopped
    ports:
     - "8080:8080"
    links:
     - database

the same for docker-compose version 1.8.0-rc1, build 9bf6bc6

@juanpastas, @xhs, @adyach:

If you're using a version 2 Compose file, no /etc/hosts entries will be created. This is not a bug. Version 2 files make use of Docker networking, which uses DNS instead of /etc/hosts to resolve hostnames.

@aanand Glad to hear that. So how to use the DNS feature? The service names (pilbox_service and object_service in my post) still are unable to be resolved. Do I have to use some DNS library?

@xhs that's strange - they should resolve. Are all containers definitely running? Underscores aren't valid in hostnames, so you might want to rename your services to use dashes instead.

@aanand yes, all of them are running. I think it's the underscore thing. Thank you. I will try that.

Was this page helpful?
0 / 5 - 0 ratings