How to expose a Docker network to the host machine?

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

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.


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