为 docker 容器提供一个可路由的 IP 地址 [英] Giving a docker container a routable ip address

查看:34
本文介绍了为 docker 容器提供一个可路由的 IP 地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ubuntu 14.04 上运行它,并将 docker0 设置为静态 ip,然后通过防火墙从公共 ip 路由到该静态 ip.我正在尝试将后端 API 设置为在 docker 容器中运行,但对一些事情感到困惑.

I am running this on ubuntu 14.04 and have set docker0 to a static ip which is then routed to from the public ip through the firewall. I am trying to set up my backend API to run in a docker container and am confused by a couple things.

1.) 我如何将 docker0 的 ip 映射到容器的 ip,以便 docker0 知道将传入的数据包发送到容器(如果可能,动态地).

1.) How would I map docker0's ip to the container's ip such that docker0 would know to send the incoming packets to the container (dynamically if possible).

2.) 如果还没有以这种方式完成,我怎样才能做到这样我就不必每次重新运行该 docker 容器时都进行设置?

2.) If not already done in such a way, how could I make it so that I don't have to set this up every time I do a fresh run of that docker container?

提前致谢!

推荐答案

我假设您希望在 docker 之外为您的 docker 容器分配一个 IP.

I assume you want to have an IP assigned to your docker container outside of docker.

首先,创建一个新 IP 并将其分配给您主机的接口(我们假设您的接口称为 eth0.

First, create a new IP and assign it to your host's interface (we assume your interface is called eth0.

$> ip addr add 10.0.0.99/8 dev eth0

现在,当您启动容器时,指定该地址并将其链接到您的 docker 容器:

Now, when you fire up the container, specify that address and link it to your docker container:

$> docker run -i -t --rm -p 10.0.0.99:80:8080 base

-p 参数将使 docker 创建一个 iptables NAT 规则,该规则将匹配目标 10.0.0.99:80 的所有流量传输到端口 8080 上的 docker 容器.

The -p argument will make docker create an iptables NAT rule which will nat all the traffic matching the destination 10.0.0.99:80 to your docker container on port 8080.

如果您需要自动化流程并将其扩展,请参阅此资源:https://github.com/jpetazzo/管道

If you need to automate the process and scale it out, consult this resource: https://github.com/jpetazzo/pipework

docker 文档是一个好的开始:https://docker.github.io/engine/用户指南/网络/

The docker documentation is a good start: https://docker.github.io/engine/userguide/networking/

这篇关于为 docker 容器提供一个可路由的 IP 地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆