I have a docker container running jenkins. As part of the build process, I need to access a web server that is run locally on the host machine. Is there a way the host web server (which can be configured to run on a port) can be exposed to the jenkins container?
EDIT: I'm running docker natively on a Linux machine.
UPDATE:
In addition to @larsks answer below, to get the IP address of the Host IP from the host machine, I do the following:
ip addr show docker0 | grep -Po 'inet \K[\d.]+'
When running Docker natively on Linux, you can access host services using the IP address of the
docker0
interface. From inside the container, this will be your default route.For example, on my system:
And inside a container:
It's fairly easy to extract this IP address using a simple shell script:
You may need to modify the
iptables
rules on your host to permit connections from Docker containers. Something like this will do the trick:This would permit access to any ports on the host from Docker containers. Note that:
iptables rules are ordered, and this rule may or may not do the right thing depending on what other rules come before it.
you will only be able to access host services that are either (a) listening on
INADDR_ANY
(aka 0.0.0.0) or that are explicitly listening on thedocker0
interface.When you have two docker images "already" created and you want to put two containers to communicate with one-another.
For that, you can conveniently run each container with its own --name and use the --link flag to enable communication between them. You do not get this during docker build though.
When you are in a scenario like myself, and it is your
That breaks when you try to
and you get stuck on "curl/wget" returning no "route to host".
The reason is security that is set in place by docker that by default is banning communication from a container towards the host or other containers running on your host. This was quite surprising to me, I must say, you would expect the echosystem of docker machines running on a local machine just flawlessly can access each other without too much hurdle.
The explanation for this is described in detail in the following documentation.
http://www.dedoimedo.com/computers/docker-networking.html
Two quick workarounds are given that help you get moving by lowering down the network security.
Hope this information helps you.