Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Giving a docker container a routable ip address

Tags:

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!

like image 533
robert Avatar asked Oct 24 '14 00:10

robert


People also ask

Can you assign an IP to a Docker container?

However, you can connect a running container to multiple networks using docker network connect . When you start a container using the --network flag, you can specify the IP address assigned to the container on that network using the --ip or --ip6 flags.

How do I expose a Docker container IP?

You can easily get the IP address of any container if you have the name or ID of the container. You can get the container names using the "Docker ps -a" command. This will list all the existing containers.

Can a Docker container use a VPN?

VPN PassthroughDocker Desktop networking can work when attached to a VPN. To do this, Docker Desktop intercepts traffic from the containers and injects it into the host as if it originated from the Docker application.


1 Answers

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/

like image 180
nucatus Avatar answered Oct 05 '22 10:10

nucatus