Sign inSign up

gadicc/host-etc-hosts

By gadicc

•Updated over 8 years ago

Automatically update the host's /etc/hosts file from docker-compose network and service names.

Image
0

228

gadicc/host-etc-hosts repository overview

⁠docker-host-etc-hosts

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.

⁠Usage

$ 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

⁠Examples

⁠Default network

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
⁠Named networks

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

⁠Example output

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

Tag summary

Content type

Image

Digest

Size

63.5 MB

Last updated

over 8 years ago

docker pull gadicc/host-etc-hosts