Tutorial 8: Port mapping in Docker

Gaurav Patil
4 min readOct 16, 2022
Photo by Rubaitul Azad on Unsplash

Missed previous tutorial ? : click here.

We will cover following topics in this tutorial :

Port mapping in Docker

Creating a web app using Flask

Creating a Dockerfile

Building a Docker image

Listing the Docker images

Running the Docker container using port mapping

Port mapping in Docker

This means establishing connection between Docker container and your machine (any machine connected to the same server) to run the same application.

Creating a web app using Flask

Following code is written in main.py file for running web application :

from flask import Flaskapp = Flask(__name__)@app.route(‘/’)def home():    return “Hello from app using Docker !!”if __name__ == “__main__”:    app.run(host= '0.0.0.0', port=5000)

Creating a Dockerfile

This Dockerfile is explained in previous tutorial in detail.

FROM pythonRUN pip install flaskRUN mkdir /appWORKDIR /appCOPY ./main.py /appCMD ["python", "main.py"]

Building a Docker image

Run following command to build the Docker image :

docker build -t my_image:v1 .

You can see following output :

It says that image is built successfully.

Listing the Docker images

We can check the list of all the images we have built using following command :

docker images

You can see following output in the command prompt :

Running the Docker container using port mapping

docker run -p <outside_port>:<inside_port> --name <container_name> <image_name>:<image_version>

outside_port is the port number for which app can run on any machine

inside_port is the port number declared in your app (main.py file)

Use of outside_port and inside_port can be understood as go through the entire tutorial.

Run following command :

docker run -p 8000:5000 --name my_container1 my_image:v1

You can see output as :

Now click on the second URL(172.17.0.2:5000) from the output, you will be directed in the browser and you can see the application running as below:

This is the use of inside_port which is 5000 as mentioned in main.py file.

This port number 5000 is mapped with 8000 which is outside port number.

This means same application can be run using 8000 port. First check your Ubuntu machine’s IP address using the command ifconfig or ip a .

Visit URL your_machine_ip:outside_port . My machine’s IP is 172.17.0.1. So I visited 172.17.0.1:8000 and you can see the application running below:

Note : This application can run on any machine connected to the same server.

Thus it is clear that port mapping is used to run the application outside the Docker container on any machine connected to the same server.

Obvious questions arise in mind :

  1. Can we run application without port mapping ? Yes we can !!! You just have to use following command while running the container : docker run --name <container_name> <image_name>:<image_version>
  2. If we didn’t mention port number in application main.py , which port will be used by Docker ? — Docker uses 5000 as default port .

Now that you know how to run web application using Docker. But if we close the Docker then web application will also stop. Have you observed that whenever we ran docker ps -a command we saw all the containers till now in an exited mode ? How can we keep app running or in Up mode ? This will be unfolded in an upcoming tutorial.

Take a deep breath and move to the next tutorial .

--

--

Gaurav Patil

Machine Learning Engineer drawing insights from mathematics.