Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add entry to host /etc/hosts file when a docker container is started

Tags:

docker

I want to be able to add hostnames to my laptop /etc/hosts that maps to a docker container. Since container ips are not static, each site I start/restart a container I would need to update the /etc/hosts file manually which is not very practical.

I am looking for a simple way to solve this. I could do some sort of script, which listens to docker events, checks for container ip and updates /etc/hosts but I dont want to reinvent the wheel. (something like this: https://github.com/discordianfish/docker-spotter) but I couldnt really understand how it works.

Anybody has a suggestion?. Thank you.

like image 787
brpaz Avatar asked Oct 19 '22 15:10

brpaz


1 Answers

You can do that using in your docker-compose.yml using extra_hosts. For instance:

version: '2'
services:
  daemon:
    build: ./
    extra_hosts:
      - "example.com:127.0.0.1"
like image 133
Édouard Lopez Avatar answered Oct 23 '22 01:10

Édouard Lopez