How to use ports in Docker?

    We can leverage Docker ports to set out some networking rules and allow containers to interact with the host machine or other containers. For example, if you want to have hosted a web server inside a Docker container and you want to access it in your localhost, you will need to expose a particular port of the container and then publish/connect it with a port in your host machine. This way you can access the website hosted on the server in your container by your browser in your host machine. There are multiple ways using which you can expose or publish ports in Docker containers. We will take a look at all of those in this article, we hands-on example. So, without any further ado, let’s get started.

    Exposing Docker Ports

    EXPOSE is simply a documentation tool that provides configuration information that other commands can use. It tells the commands about which ports are exposed by the container and available for services or information exchange. We can either use an EXPOSE instruction in a Dockerfile or --expose option in the docker command itself. They only make the ports accessible for inter-container interaction. We need to publish them later on to make actual use of them. Let’s say you have a nodeJs server and a Redis server deployed in two different containers. To access the data from the Redis server, the nodeJs server will have to communicate with the Redis server. The Dockerfile of the Redis image has an instruction called EXPOSE which exposes port 6379 by default which means that we can connect to it through this port. The syntax of the EXPOSE instruction in a Dockerfile is - EXPOSE <port>/<protocol> We need to specify the port number that we want to expose along with the protocol through which we want the container to communicate. By default, it’s the tcp protocol. Let’s create a simple Dockerfile with the following structure.

    Here, we have created an Apache server image, copied a simple html file, and exposed the port 8080. Let’s try to build this image using the Docker build command.

    $ docker build -t sample .

    Next, let’s try to run this container in detached mode.

    $ docker run -dit --name=myCont sample

    Now, let’s try to list all the actively running containers.

    $ docker ps

    You can see that the container is listening to port 80 with TCP protocol. Let’s inspect this container.

    $ docker inspect myCont

    We can see the IP address of the container. If you browse this IP address in your local machine, you will see the website running.

    Publishing Docker Ports

    To publish a Docker port means to connect the ports of the container to the host machine. When we run the Docker run command, it does not make the exposed ports of the container accessible to the outside world. Hence, if we want to do so, we can use the --publish option. If we use the -p flag, only the mentioned ports are published, but if we use the -P flag, all the ports are published. Let’s see how to publish port 80 of the container that we created above to port 80 of our local machine.

    $ docker run -itd -p 80:80 --name=myCont sample

    Let’s list all the actively running containers.

    $ docker ps

    Wrapping Up!

    In this article, we have discussed how to leverage ports to allow inter-container communication and expose ports to carry out services. We discussed how to use the EXPOSE instruction and publish options to do so. We certainly hope that through this article, you will be able to get hands-on with Docker ports! Happy Learning!

    People are also reading: