polvoazul
polvoazul

Reputation: 2518

How to expose a Docker network to the host machine?

Consider the following docker-compose.yml

version: '2'
services:
    serv1:
        build: .
        ports:
          - "8080:8080"
    links:
      - serv2

    serv2:
        image: redis
        ports:
          - "6379:6379"

I am fowarding the ports to the host in order to manage my services, but the services can access each other simply using the default docker network. For example, a program running on serv1 could access redis:6379 and some DNS magic will make that work. I would like to add my host to this network so that i can access container's ports by their hostname:port.

Upvotes: 29

Views: 51703

Answers (5)

AK_
AK_

Reputation: 2059

there is no magic involved in doing this. Here is how I did it: In my case the docker container had vpn clients connected and I needed to communicate with these clients from within the host

  1. Make sure the docker container is running

    docker-composer up -d

  2. Make sure IP forwarding is enabled, I do this in docker compose file:

     sysctls:
          - net.ipv4.conf.all.src_valid_mark=1
  1. get the IP address of the docker container

    docker inspect <img name> | grep IPAddr

  2. add a route from host to docker container

    ip route add <target ip range>/24 via <container ip>

  3. confirm by pinging the target ip addr.

Upvotes: 0

Yamaneko
Yamaneko

Reputation: 3563

If you need a quick workaround to access a container:

  1. Get the container IP:
$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id

172.19.0.9
  1. If you need to use the container name, add it to your /etc/hosts.
# /etc/hosts

172.19.0.9 container_name

Upvotes: 9

Hunter
Hunter

Reputation: 29

Just modify the hosts file on your host machine to add the container entries. Example: 127.0.0.1 container1 127.0.0.1 container2 127.0.0.1 container3

Assuming that the binding of the ports has been done.

Upvotes: -1

dnephin
dnephin

Reputation: 28040

You can accomplish this by running a dns proxy (like dnsmasq) in a container that is on the same network as the application. Then point your hosts dns at the container ip, and you'll be able to resolve hostnames as if you were in the container on the network.

https://github.com/hiroshi/docker-dns-proxy is one example of this.

Upvotes: 12

DarkLeafyGreen
DarkLeafyGreen

Reputation: 70406

I am not sure if I understand you correctly. You want e.g. your redis server be accessible not only from containers that are in the same network, but also from outside the container using your host ip address?

To accomplish that you have to use the expose command as described here https://docs.docker.com/compose/compose-file/#/expose

expose:
  - "6379"

So

ports:
  - "6379:6379"
expose:
  - "6379"

should do the trick.

The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. EXPOSE does not make the ports of the container accessible to the host. To do that, you must use either the -p flag to publish a range of ports or the -P flag to publish all of the exposed ports. You can expose one port number and publish it externally under another number.

from https://docs.docker.com/engine/reference/builder/#expose

Upvotes: 4

Related Questions