Automatically update the host's /etc/hosts file from docker-compose network and service names.
228
Automatically update the host's hosts file from docker-compose network
and service names. Useful for development.
Copyright (c) 2017 by Gadi Cohen. Released under MIT License.
$ docker run --name host-etc-hosts \
--restart=unless-stopped \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /etc/hosts:/etc/hosts \
gadicc/host-etc-hosts
myapp/docker-compose.yaml:
rversion: '3'
services:
www:
image: nginx
db:
image: mariadb
Now you can access like:
$ curl www.myapp
$ mysql --host db.myapp
myapp/docker-compose.yaml:
rversion: '3'
services:
db:
image: mariadb
networks:
- "internal"
networks:
internal:
and access with:
$ curl www.myapp
$ mysql --host db.internal.myapp
Under the hood this uses hosts-so-easy (by the same author) so /etc/hosts is written safely while preserving formatting and placing hosts in their own section. For the above examples you'd see something like this:
#
# /etc/hosts: static lookup table for host names
#
#<ip-address> <hostname.domain.org> <hostname>
127.0.0.1 localhost.localdomain localhost
::1 localhost.localdomain localhost
# docker host-etc-hosts
172.24.0.2 edf2e4134bd7.myapp www.myapp
172.24.0.3 03768662bdea.myapp db.myapp
172.24.0.4 87f36e483361.myapp db.internal.myapp
# End of file
Content type
Image
Digest
Size
63.5 MB
Last updated
over 8 years ago
docker pull gadicc/host-etc-hosts