为Docker容器配置外部IP的工具,通过设置容器的EXTERNAL_IP环境变量,自动配置iptables路由容器流量至指定外部IP,需主机已分配该外部IP。
tozd/external-ip
DEPRECATED: Use com.docker.network.host_ipv4 bridge option since Docker v25. See
https://github.com/docker-mailserver/docker-mailserver/pull/4330/files.
https://gitlab.com/tozd/docker/external-ip
Available as:
- https://hub.docker.com/r/tozd/external-ip
- https://gitlab.com/tozd/docker/external-ip/container_registry
Image inheritance
https://gitlab.com/tozd/docker/base ← https://gitlab.com/tozd/docker/dinit ← tozd/external-ip
Tags
ubuntu-bionicubuntu-focalubuntu-jammyubuntu-noblealpine-38alpine-310alpine-312alpine-314alpine-316alpine-318alpine-320alpine-322
Volumes
/var/log/dockergen: Log files whenLOG_TO_STDOUTis not set to1.
Variables
DOCKER_HOST: Where to connect to access Docker daemon to monitor for new containers. Default is/var/run/docker.sockinside the container.LOG_TO_STDOUT: If set to1output logs to stdout (retrievable usingdocker logs) instead of log volumes.
Description
Run:
$ docker run --detach \ --net=host --cap-add=NET_ADMIN --cap-add=NET_RAW \ --volume /var/run/docker.sock:/var/run/docker.sock \ --volume /run/xtables.lock:/run/xtables.lock \ tozd/external-ip:ubuntu-bionic
After that, if any other Docker container has an environment variable EXTERNAL_IP set, with an IP address to use for
containers external IP, iptables will be configured to route container's traffic from that external IP.
Connections to other containers on the same Docker network keep the container's own address, except connections
which Docker redirects from a published port back into a container.
The external IP must be assigned on the host.
A chain named EXTERNAL_IP is created in the nat table into which all the rules are added.
The chain is managed fully by the container: rules for containers which are not running anymore are removed,
as are any other rules added to it. And one more empty chain is created after this one for any additional custom
rules you might want to add, named AFTER_EXTERNAL_IP.
Please make sure /run/xtables.lock exists on the host before starting the container.
This file ensures iptables locking is consistent between the host and the container,
preventing race conditions that can cause containers to fail to start.
If this file does not exist, Docker will incorrectly create it as a directory, which may cause issues both on the host and with the container.
When LOG_TO_STDOUT is set to 1, Docker image logs output to stdout and stderr. All stdout output is JSON.
docker-compose example
version: '3' services: nat_manager: image: tozd/external-ip:ubuntu-bionic volumes: - /var/run/docker.sock:/var/run/docker.sock - /run/xtables.lock:/run/xtables.lock network_mode: host cap_add: - NET_ADMIN - NET_RAW a: image: byrnedo/alpine-curl command: "-s http://ifconfig.me" environment: EXTERNAL_IP: XX.XX.XX.XX b: image: byrnedo/alpine-curl command: "-s http://ifconfig.me" environment: EXTERNAL_IP: YY.YY.YY.YY
Set XX.XX.XX.XX and YY.YY.YY.YY to your external IP address.
GitHub mirror
There is also a https://github.com/tozd/docker-external-ip, if you need to fork the project there.
tozd 命名空间的其他可用镜像:
镜像拉取方式
您可以使用以下命令拉取该镜像。请将 <标签> 替换为具体的标签版本。如需查看所有可用标签版本,请访问 标签列表页面。
轩辕镜像加速拉取命令点我查看更多 external-ip 镜像标签
DockerHub 原生拉取命令
用户好评
来自真实用户的反馈,见证轩辕镜像的优质服务