Docker - Access Docker containers from the host via container names by synchronizing /etc/hosts

Docker - Access Docker containers from the host via container names by synchronizing /etc/hosts

· json · rss
Subscribe:

When using Docker, containers within the same shared network are able to resolve one another via their hostnames. However, the same functionality is not replicated on the host of the Docker containers.

For personal reasons, I kept Nginx load balancer on the host and did not want to forward ports of containers, but I still wanted to reverse proxy my containers.

Solution

https://hub.docker.com/r/jarylc/docker-hosts-sync

Source: https://gitlab.com/jarylc/docker-hosts-sync

A simple Golang program that I created that updates the host's /etc/hosts file whenever the Docker containers are updated, capturing the private IPs of all the containers each time. Said program is also packaged in a Docker image, so it is easily appended to your own Docker system!

This image automatically mirrors /etc/hosts whenever your Docker containers are changed

Alternatives

jaschweder/hosts

Not multi-architecture. Checks every 5 second intervals (not configurable), instead of on container creation and deletion. Big Docker image even when after compression 35mb vs 2.5mb.

itsziget/hosts-gen

Not multi-architecture. Based off a larger templating project for more use-cases, 8mb vs 2.5mb.