Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 349
polvoazul Avatar asked Aug 22 '16 02:08

polvoazul


People also ask

How do I expose a Docker network?

There are several ways to do this: you can expose a port via the --expose flag at runtime, or include an EXPOSE instruction in the Dockerfile. You can also publish ports by using the -p or -P flags in the Docker run string. There's also container linking via --link .

Can I access Docker network from host?

Accessing the Host With the Default Bridge ModeYou just need to reference it by its Docker network IP, instead of localhost or 127.0. 0.1 . Your host's Docker IP will be shown on the inet line. Connect to this IP address from within your containers to successfully access the services running on your host.

How does Docker communicate with host?

Here's the gist: For containers to communicate with other, they need to be part of the same “network”. Docker creates a virtual network called bridge by default, and connects your containers to it. In the network, containers are assigned an IP address, which they can use to address each other.

How does host networking work with Docker containers?

If you use the host network driver for a container, that container’s network stack is not isolated from the Docker host. For instance, if you run a container which binds to port 80 and you use host networking, the container’s application will be available on port 80 on the host’s IP address.

How do I expose the ports of a docker container?

In addition to Borja's answer, you can expose the ports of Docker containers by adding -p [HOST_PORT]: [CONTAINER_PORT] to your docker run command. E.g. if you want to reach a web server in a Docker container from another machine, you can start it with docker run -d -p 80:80 httpd:alpine.

How do I access a web server in a docker container?

E.g. if you want to reach a web server in a Docker container from another machine, you can start it with docker run -d -p 80:80 httpd:alpine. The container's port 80 is then reachable via the host's port 80.

How do I run a container on a host network?

Use host networking. If you use the host network driver for a container, that container’s network stack is not isolated from the Docker host. For instance, if you run a container which binds to port 80 and you use host networking, the container’s application will be available on port 80 on the host’s IP address.


3 Answers

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.

like image 126
dnephin Avatar answered Oct 27 '22 11:10

dnephin


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
like image 35
Yamaneko Avatar answered Oct 27 '22 09:10

Yamaneko


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

like image 40
UpCat Avatar answered Oct 27 '22 10:10

UpCat