A lightweight Go utility that automatically manages /etc/hosts entries for Docker containers. It monitors Docker events and dynamically updates the hosts file with container network information, making it easy to access containers by hostname.
- π Automatic Updates: Monitors Docker container start/stop events in real-time
- π Network Awareness: Supports multiple Docker networks and aliases
- π§Ή Clean Shutdown: Removes all entries on graceful exit
- β‘ Atomic Operations: Uses atomic file writes to prevent corruption
- π³ Bridge & Custom Networks: Handles both default bridge and user-defined networks
Docker Hoster watches for container lifecycle events and automatically:
- Extracts container IP addresses from all connected networks
- Collects container names, hostnames, and network aliases
- Updates
/etc/hostswith mappings in a dedicated section - Cleans up entries when containers stop or the service exits
- Go 1.16 or higher
- Docker running on the system
- Root/sudo access (required to modify
/etc/hosts)
git clone <repository-url>
cd hoster
go mod download
go build -o hoster main.goRun with default settings (requires root):
sudo ./hosterView all available options:
./hoster --helpAvailable flags:
--help- Display help message and exit--hosts <path>- Path to the hosts file (default:/etc/hosts)--socket <path>- Path to the Docker socket (default:/var/run/docker.sock)
Example with custom paths:
sudo ./hoster --hosts /custom/hosts --socket /var/run/docker.sockThe utility runs continuously, monitoring Docker events. It's designed to run as a background service or daemon.
Press Ctrl+C or send SIGTERM to gracefully shut down. The service will automatically clean up all hosts file entries before exiting.
The utility uses the following defaults:
- Hosts File:
/etc/hosts - Docker Socket:
/var/run/docker.sock
You can override these defaults using command line flags:
sudo ./hoster --hosts /custom/hosts --socket /custom/docker.sockAlternatively, modify the constants in main.go to change the defaults:
const (
defaultHostsPath = "/etc/hosts"
defaultSocket = "/var/run/docker.sock"
)Docker Hoster manages entries in a dedicated section:
# Your existing hosts entries
127.0.0.1 localhost
#-----------Docker-Hoster-Domains----------
172.17.0.2 mycontainer webapp abc123
172.18.0.3 database db postgres
#-----Do-not-add-hosts-after-this-line-----
Start a container with network aliases:
docker network create mynetwork
docker run -d --name webapp --network mynetwork --network-alias api nginxDocker Hoster will automatically add entries like:
172.18.0.2 webapp api abc123def456
You can now access the container:
curl http://webapp
curl http://apiFor each running container, Docker Hoster extracts:
- Container name (without leading
/) - Container hostname
- Network-specific IP addresses
- Network aliases from all connected networks
This utility requires:
- Read/Write access to
/etc/hosts - Read access to Docker socket (
/var/run/docker.sock)
Run with sudo or as root to ensure proper permissions.
- SIGINT (Ctrl+C): Graceful shutdown with cleanup
- SIGTERM: Graceful shutdown with cleanup
On shutdown, all Docker Hoster entries are removed from /etc/hosts.
- Docker Client: Uses official Docker Go SDK
- Event Monitoring: Subscribes to Docker events API
- Atomic Writes: Uses auxiliary file + rename for safe updates
- Context Management: Proper cancellation and cleanup
Error: failed to read hosts file: permission denied
Solution: Run with sudo or as root user.
Error: failed to create docker client
Solution: Ensure Docker is running and the socket path is correct.
Check that:
- Containers are actually running (
docker ps) - Containers have network aliases or are on custom networks
- The utility is running with proper permissions
.
βββ main.go # Main application code
βββ go.mod # Go module dependencies
βββ README.md # This file
github.com/docker/docker- Docker Engine API client
go build -o hoster main.goTest with a simple container:
# Terminal 1: Run hoster
sudo ./hoster
# Terminal 2: Start a container
docker run -d --name test nginx
# Check /etc/hosts
grep test /etc/hostsContributions are welcome! Please feel free to submit issues or pull requests.