Skip to main content

Automatically manage Docker container hostnames in /etc/hosts for local development and CI

Project description

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.

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=DEBUG to 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

docker_hosts-0.1.0.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

docker_hosts-0.1.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file docker_hosts-0.1.0.tar.gz.

File metadata

  • Download URL: docker_hosts-0.1.0.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.17

File hashes

Hashes for docker_hosts-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c0f8cccf70a9ad30d01220b7f22c8eec1233b80539006c3d097ee3069655d7fe
MD5 cda820abd72cefaec5d69e50d96c0e07
BLAKE2b-256 8c4b4ee074fda533fb63c8bed3a0919b97e0895bf73bf804c059983eefa9f581

See more details on using hashes here.

File details

Details for the file docker_hosts-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for docker_hosts-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d9e4af822f73658fe1ad60ecb2b41d9a477182c816104059250b22399e72ef55
MD5 72a504788e58e97d2ca641122fc86e62
BLAKE2b-256 e7fbd02db10fb44128e9667a1df71655dcff8cd2866f091d9450d166f4b3d899

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page