Tutorial 9: Detached mode in Docker

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

Missed previous tutorial ? : click here.

We will cover following topics in this tutorial :

Creating a web app using Flask

Creating a Dockerfile

Building a Docker image

Listing the Docker images

Running the Docker container using port mapping

Listing the Up (running) containers

Stopping the container

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 app_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 -d --name <container_name> <image_name>:<image_version>

-d is for running the container in detached mode.

Detached mode means a web application will stop running only when container is stopped.

Run following command :

docker run -d --name app_container1 app_image:v1

You can see the following output :

Listing the Up (running) containers

Run following command :

docker ps 

Check STATUS following output which says “Up 4 seconds”. This means application is running in detached mode.

Now type http://URL(172.17.0.2:5000) in browser and you can see the web app running :

Stopping the container

docker stop <container_name>

Run following command to stop the container :

docker stop app_container1

Now that container is stopped, check the same URL. It will show following output.

Above message clearly shows that web application is not running since container has stopped.

If you want to run the application, run following command :

docker run --name <container_name> <image_name>:<image_version>

Following command can also be used to run the application :

docker run --name app_container2 app_image:v1

You can see following command :

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

Press Ctrl + c to stop the container, then check same URL. As soon as container is stopped, application stops running.

Congratulations !!!

Now we will deploy the song popularity web application on Heroku platform using Docker for which we have already created Flask API. Take a deep breath and move to the next tutorial.

--

--

Gaurav Patil

Machine Learning Engineer drawing insights from mathematics.