Container to add to Docker Compose to get host.docker.internal
on Linux.
This is the source repo for docker.io/benizi/host.docker.internal.
Its only purpose is to provide a Docker container (for use in Docker Compose) that forwards all its network traffic to a single IP (the host running Docker Compose).
It uses the same iptables
+ capabilities approach as qoomon/docker-host, but its use case is intentionally much more limited in scope.
Add to Docker Compose docker-compose.yml
(or docker-compose.override.yml
):
version: "3.8"
services:
host:
image: benizi/host.docker.internal
cap_add: [NET_ADMIN, NET_RAW]
networks: { default: { aliases: [host.docker.internal] } }
restart: on-failure
- The capabilities in
cap_add
are required foriptables
- The
networks
setting is optional, but that's the main reason this exists - The
restart
setting is probably overly cautious (shouldn't be any reason it would fail, but it doesn't hurt)
Configuration can be done through environment variables, e.g., in the docker-compose.yml
service:
# ...
services:
host:
## From above:
image: benizi/host.docker.internal
cap_add: [NET_ADMIN, NET_RAW]
networks: { default: { aliases: [host.docker.internal] } }
restart: on-failure
## Configuration
environment:
## Set the destination ...
# ... by IP
IP: '10.1.2.1'
# ... or hostname (resolved in the container)
HOST: 'service.network.internal'
# Forward these port ranges: (default: `0:65535`)
PORTS: 80:100,8800:8900
# Optionally, route all traffic to a single port:
# TARGET_PORT: 8080 (or `DPORT` (D = destination) or `TPORT` (T = target))
## Print `iptables` stats this frequently (default: `1h`)
# Should accept anything Alpine Linux `sleep` accepts
STATS: 1m
# Don't print any stats:
# STATS: '0'
## Debugging options
# Enable debugging (any non-empty value)
DEBUG: 'yup'
# ... or just for printing environment variables beforehand:
DEBUG_ENV: 'yup'
# ... or just for the `/entrypoint` script (mainly for my own use):
DEBUG_SCRIPT: 'yup'
- Determine IP for forwarding:
- Specify forwarding IP via
$IP
(directly) - Specify forwarding IP via
$HOST
(resolved in the container) - Use default gateway IP otherwise
- Specify forwarding IP via
- Reroute all traffic to a single port:
- Specify target port via
$TARGET_PORT
- Specify target port via
Copyright © 2020–2021 Benjamin R. Haskell
Distributed under the MIT License (included in file: LICENSE).