Automatic Docker Hosts Management
Automatically manage Docker container hostnames in your /etc/hosts file.
I built this to solve a specific problem: using Docker Compose for local development services (postgres, redis, etc.) and sharing the same compose file with CI. I wanted DATABASE_URL=postgres://postgres.localhost/mydb to work both locally and in CI without maintaining separate configs. This tool creates stable domains for Docker Compose services that work everywhere - no manual /etc/hosts edits, no environment-specific configuration. Originally extracted from my python-starter-template.
Installation
Using uv:
uv tool install docker-hosts
Using pip:
pip install docker-hosts
Usage
Update /etc/hosts with all running containers:
sudo docker-hosts
Preview what would be written without making changes:
docker-hosts --dry-run
Use a custom TLD (defaults to .localhost):
sudo docker-hosts --tld dev
Specify a custom hosts file path:
docker-hosts /tmp/hosts --dry-run
The tool requires sudo when writing to /etc/hosts, but you can test with --dry-run first to see what it would do.
Windows / WSL
You may encounter issues if using Docker Desktop. Running Docker directly on WSL works properly.
Features
- Network-aware - picks up all network aliases from Docker networks, not just the default bridge network. If your container is attached to multiple networks, all IPs and aliases get added.
- Safe writes - uses atomic file writes (write to temp, then rename) to avoid corrupting your hosts file. Your existing entries are preserved - the tool only manages the section between
### Start Docker Domains ###and### End Docker Domains ###markers. - Structured logging - built with structlog for clean, parseable logs. Set
LOG_LEVEL=DEBUGto see what's happening under the hood. - Dry-run mode - test what would be written before committing to changes. Great for understanding what the tool does or debugging issues.
Development
Running Tests
Tests require Docker to be running with the postgres and redis containers from docker-compose.yml:
docker compose up -d
pytest -v
Run only unit tests (no Docker required):
pytest -v -m unit
Run only integration tests:
pytest -v -m integration
The test suite includes:
- Integration tests that verify container detection using real Docker containers
- Unit tests for data extraction and file operations
- CLI tests using temporary hosts files in
tmp/hosts
MIT License
Metadata
Release files for docker-hosts 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| docker_hosts-0.1.1.tar.gz | 3.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| docker_hosts-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.5 kB
Release files / docker_hosts-0.1.1.tar.gz
| Download URL | docker_hosts-0.1.1.tar.gz |
|---|---|
| Size | 3.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e3ce8ffb9bfb3c7eb5e1fd30de05e45e9c58365c84dac39b804be0040a3556c6
|
|
BLAKE2b-256 checksum How to use checksums |
f4d1aa3ad8fce03bf4b0a97508ad163592da4d1960f7d9ae264be0780638ae66
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / docker_hosts-0.1.1-py3-none-any.whl
| Download URL | docker_hosts-0.1.1-py3-none-any.whl |
|---|---|
| Size | 4.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a3f323066545dd0f55b60a56322e6ac83699dde49e434b24b787d72a341f06f8
|
|
BLAKE2b-256 checksum How to use checksums |
0c5f63e9d76b335be73198f3c2b4e3487c8a205ab6b4629c0ee0f2b9fb12dcfc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|