Exposing Docker Container Ports

274 views Asked by At

I understand that to expose ports in a docker container, you can use the -p flag (e.g. -p 1-100:1-100). But is there a nice way to expose a large percentage of possible ports from the container to the host machine? For instance if I am running a router of sorts in a container that lives in a VM, and I would like to expose all ports in the container from 32768 upwards to 65535, is there a nice way to do this? As it stands I've tried using the -p flag and it complains about memory allocation errors.

2

There are 2 answers

0
Erik Nguyen On BEST ANSWER

Nvm. I figured out my misunderstanding. -P is what I want, and I want to expose and not explicitly map ports.

0
Thomasleveil On

tl;dr

docker run --net=host ...

Docker offers different networking modes for containers. By default the networking mode is bridge which implies the need to expose ports.

If you run a container with networking mode host then you won't need to expose/forward ports as both the docker host and the container will share the very same network interface.

In the container, localhost will refer to the docker host. Any port opened in the container is in fact opened on the docker host network interface.