The only effect it has is in terms of readability or understanding the application. EXPOSE is a way of documenting your application port.EXPOSE does not add additional layers to the resulting docker image.You can add a simple instruction in your Dockerfile to let others know at which port your application will be accepting connections on.Ībout this instruction, what you must know is the following:. Using -expose with docker CLI or expose key in docker-compose.Using the EXPOSE Dockerfile instruction.There are two ways you can expose a port: Why? Because most of the docker images you use in your set up already have a default port exposed in their configuration.įor example, a containerized frontend application can communicate with a MariaDB database by simply specifying the container's IP and whichever port MariaDB is accepting connections on (default 3306).Įxposing helps in a situation where the defaults are not being used, like in this case if MariaDB were to not accept connections on port 3306, an alternative port should be mentioned by exposing it. Published ports are used for communicated with systems outside the container world.įirst of all, exposing a port is not strictly necessary.Exposed ports are used for internal container communication, within the container world.
The exposed ports, on the other hand, cannot be accessed directly from outside the container world. You see, how the SERVER container's port 80 is mapped to the port 80 of the host system? This way, the container is able to communicate to the outside world using the public IP address of the host system. This way, the container is able to communicate with external systems, the real world, the internet. Publishing a port is more like mapping the ports of a container with ports of the host. This is for communicating with other containers, not with the outside world.
There are two ways to handle ports in Docker: exposing the ports and publishing the ports.Įxposing a port simply means letting others know on which port the containerized application is going to be listening on, or accepting connections on.
#How do i know my docker ip how to
I'll go on to the difference between exposing and publishing ports, why they are used and how to use them.
In this tutorial, I'll discuss dealing with ports in Docker. This internal and external communication is handled with exposed and published ports in Docker respectively. In the same setup, some containers also interact with the outside world. In a multi-container setup, the services running in the containers communicate with each other over a common network.