dockerdocker-container

How to access host port from docker container


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?

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.]+'

Solution

  • 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:

    $ ip addr show docker0
    7: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
        link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff
        inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
           valid_lft forever preferred_lft forever
        inet6 fe80::f4d2:49ff:fedd:28a0/64 scope link 
           valid_lft forever preferred_lft forever
    

    And inside a container:

    # ip route show
    default via 172.17.0.1 dev eth0 
    172.17.0.0/16 dev eth0  src 172.17.0.4 
    

    It's fairly easy to extract this IP address using a simple shell script:

    #!/bin/sh
    
    hostip=$(ip route show | awk '/default/ {print $3}')
    echo $hostip
    

    You may need to modify the iptables rules on your host to permit connections from Docker containers. Something like this will do the trick:

    # iptables -A INPUT -i docker0 -j ACCEPT
    

    This would permit access to any ports on the host from Docker containers. Note that:


    If you are using Docker on MacOS or Windows 18.03+, you can connect to the magic hostname host.docker.internal.


    Lastly, under Linux you can run your container in the host network namespace by setting --net=host; in this case localhost on your host is the same as localhost inside the container, so containerized service will act like non-containerized services and will be accessible without any additional configuration.