Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker EXPOSE a port only to Host

Tags:

docker

People also ask

Can Docker container access ports on host?

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. This would permit access to any ports on the host from Docker containers.

How do I map a container port to a host port?

To make a port available to services outside of Docker, or to Docker containers which are not connected to the container's network, use the --publish or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host to the outside world.

What is exposing ports in Docker?

The expose keyword in a Dockerfile tells Docker that a container listens for traffic on the specified port. So, for a container running a web server, you might add this to your Dockerfile: EXPOSE 80. This tells Docker your webserver will listen on port 80 for TCP connections since TCP is the default.


Sure, just bind it to localhost, like this:

docker run -p 127.0.0.1:27017:27017

Also: Your host can also talk to each container normally over its IP. Use docker inspect $ID to get a json dump (beside other stuff) containing the network IP.