4

I am just learning the ropes with Docker, however one thing seems to of stumped me. I have tried to find answers to my question elsewhere (add entry to host /etc/hosts file when a docker container is started) but none really seemed to solve my question.

I am using docker-compose version 2 and docker-compose.yml looks like this:

version: "2"
services:
  web:
    image: php:5-apache
    extra_hosts:
      - "example.com:127.0.0.1"
    ports:
      - "80"
    command: php -S 0.0.0.0:80 -t /var/www/html
    working_dir: /var/www/html
    restart: always

I want to be able to go to example.com on the host machine and have it point to the docker container. Also, it seems like docker containers IP's can change on restart? Is there a way to keep track of the IP even after restart?

I guess what I am after is similar to: https://github.com/cogitatio/vagrant-hostsupdater

Community
  • 1
  • 1
ellioseven
  • 71
  • 5

2 Answers2

2

The docker engine doesn't manage the /etc/hosts file on the host machine, only inside the containers (but using the DNS service is preferred to that).

You can modify the /etc/hosts file by any other method on your host (by hand or using a configuration management tool).

BMitch
  • 231,797
  • 42
  • 475
  • 450
  • It would be interesting to know if there's an easy way to somehow set up a "small DNS" and make the thing work the way topic starter describes it. – Andrey Agibalov Jul 28 '16 at 01:16
1

You should search for Docker hostupdater on Google. There are many docker containers that make it possible, although most of them only work with a Linux host machine.

For example you can try this project : https://github.com/grachevko/docker-hosts-updater

Kiruahxh
  • 1,276
  • 13
  • 30