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.internal/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
.internal and .test are the only recommended TLDs. The default is .internal:
sudo docker-hosts --tld test
Specify a custom hosts file path:
docker-hosts /tmp/hosts --dry-run
Filter containers by their Docker names using repeatable regular expressions:
sudo docker-hosts --include '^myproject-' --exclude 'worker'
Update the hosts file and output the resulting aliases as JSON:
sudo docker-hosts --include '^myproject-' --json
Combine --json with --dry-run to output JSON without modifying the hosts file.
Logs default to stderr so stdout can be safely piped to another command. Set
PYTHON_LOG_PATH to override the log destination.
When both options are provided, a container must match at least one --include
expression and no --exclude expressions. Without filters, all running containers
are included.
Docker Compose containers with com.docker.compose.service and
com.docker.compose.project labels also receive project-scoped domains in the
form <service>.<project>.<tld> (for example, postgres.my-app.internal).
When multiple Compose projects run simultaneously and share service names like
postgres, bare <service>.<tld> collides and is omitted while
<service>.<project>.<tld> remains available.
If multiple running containers claim the same hostname, the ambiguous hostname is
omitted from all generated entries and a warning identifies the affected containers.
Collisions are detected across all running containers before --include and
--exclude determine which containers are written or reported. Warnings are only
emitted when a collision affects selected containers, but list all claimants.
The tool requires permission to replace the hosts file and will suggest using
sudo or --dry-run when permission is denied. You can test with --dry-run
first to see what it would do.
Inside a container, Docker's mounted /etc/hosts cannot be replaced. If atomic
replacement fails with EBUSY, the tool writes to the existing file in place and
truncates any remaining bytes, preserving its ownership and permissions. Readers
may briefly see incomplete content during this fallback. Other errors propagate.
These mappings apply to the machine or container running docker-hosts. Child
containers need Compose service names or their own explicit hostname configuration;
they do not inherit the runner's hosts entries.
Windows / WSL
You may encounter issues if using Docker Desktop. Running Docker directly on WSL works properly.
Features
- Compose-aware - emits
<service>.<project>.<tld>domains from Docker Compose labels (com.docker.compose.serviceandcom.docker.compose.project) alongside network aliases, container names, and hostnames. - 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) by default, with an in-place fallback for mounted files that return
EBUSY. 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 and writes to Docker's mounted
/etc/hosts - Unit tests for data extraction and file operations
- CLI tests using temporary hosts files in
tmp/hosts
The mounted-hosts test pulls python:3.13-slim and installs the checkout and pytest
inside a disposable container. It requires access to Docker Hub and PyPI.
MIT License
Local CLI development
Run just install_editable to install this checkout globally in editable mode. Source changes are reflected without reinstalling.
This requires uv, just, zsh, and jq. If mise is available, the recipe also updates every installed pipx:docker-hosts environment so mise can use the local checkout. Re-run after mise upgrades, which replace those editable installations.
This project was created from iloveitaly/python-package-template
Metadata
Release files for docker-hosts 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| docker_hosts-0.3.0.tar.gz | 8.6 kB | Without provenance | Details for docker_hosts-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| docker_hosts-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for docker_hosts-0.3.0-py3-none-any.whl |
Total release size: 17.7 kB
Release files / docker_hosts-0.3.0.tar.gz
| Download URL | docker_hosts-0.3.0.tar.gz |
|---|---|
| Size | 8.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c678b18426cd3efd9d3727340228cbc517414c48d5dcb7878a9f96bc89af3982
|
|
BLAKE2b-256 checksum How to use checksums |
cb64ee584202e544fa8bdf1e37b35395919303303f666b72576eea9931feb0c8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","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.3.0-py3-none-any.whl
| Download URL | docker_hosts-0.3.0-py3-none-any.whl |
|---|---|
| Size | 9.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b71f111fc7909b6172c65a12618b3c6f96a6d0eb5449dd4f8c72483a6e9c79b2
|
|
BLAKE2b-256 checksum How to use checksums |
9e1751e14013e418c4c7992439eca538b53117449d16eed22aafe1e389a83488
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for docker-hosts 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.