Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

Giving a docker container a routable ip address

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.) 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.) 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?

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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

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.

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...