How to run Python flask applications on Docker Engine

Cannot we isolate our apps so that they are independent of each other and run perfectly ? The answer is absolutely “YES”, that correct that’s very much possible with docker and containers. They provide you isolated environment and are your friend for deploying many applications with each taking its own container. You can run as many as containers in docker and are independent of each other. They all share same kernel memory.

In this tutorial we will go through a simple demonstration of a python application which will run on docker engine.

Table of content

  1. What is Python ?
  2. What is docker ?
  3. Prerequisites
  4. Create a Python flask application
  5. Create a Docker file
  6. Build Docker Image
  7. Run the Python flask application Container
  8. Conclusion

What is Python ?

Python is a language from which you create web applications and system scripts. It is a used vastly across the organizations and very easy to learn. Python apps require isolated environment to run its application very well. This is quite possible with Docker and containers which we will use in this tutorial.

If you wish to know more about python please visit our Python’s Page to learn all about Python.

What is docker ?

Docker is an open source tool for developing , shipping and running applications. It has ability to run applications in loosely isolated environment using containers. Docker is an application which helps in management of containers in a very smooth and effective way. In containers you can isolate your applications. Docker is quite similar to virtual machine but it is light weighted and can be ported easily.

Containers are light weighted as they are independent of hypervisors load and configuration. They directly connect with machines ie. hosts kernel.


You may incur a small charge for creating an EC2 instance on Amazon Managed Web Service.

Create a Python flask application

  • Before we create our first program using python flask we need to install python flask and python virtual environment for flask to run.

pip install virtualenv # virtual python environment 
  • Create and activate a virtual environment named virt:
virtualenv venv
source virt/bin/activate
  • Finally install Flask

pip install flask # Install Flask from pip
  • Now create a text file and name it as where we will write our first python flask code as below.
from flask import Flask # Importing the class flask

app = Flask(__name__)   # Creating the Flask class object.

@app.route('/')         # app.route informs flask about the URL to be used by function
def func():             # Creating a function
      return("Iam from")  

if __name__ ==  "__main__":    # Programs starts from here.
  • Create one more file in same directory and name it as requirements.txt where we will define the dependency of flask application
  • Now our python code and requirements.txt are ready for execution. Lets execute our code using below command.
This image has an empty alt attribute; its file name is image-42.png
  • Great, so our python flask application ran successfully on our local machine. Now we need to execute same code on docker . Lets now move to docker part.

Create a docker file

Docker file is used to create a customized docker images on top of basic docker image. It is a text file that contains all the commands to build or assemble a new docker image. Using docker build command we can create new customized docker images . Its basically another layer which sits on top of docker image. Using newly built docker image we can run containers in similar way.

This image has an empty alt attribute; its file name is image-43.png
  • Create a docker file and name it as Docker file . Keep this file also in same directory as and requirements.txt
FROM python:3.8           # Sets the base image 
WORKDIR /code             # Sets the working directory in the container
COPY requirements.txt .   # copy the dependencies file to the working directory
RUN pip install -r requirements.txt  # Install dependencies
COPY src/ .               # Copy the content of the local src directory to the working directory
CMD [ "python", "./" ] # Command to run on container start  
This image has an empty alt attribute; its file name is image-44.png

Build docker Image

  • Now we are ready to build our new image . So lets build our image
docker build -t myimage .
  • You should see the docker images by now.
docker images
This image has an empty alt attribute; its file name is image-45.png

Run the Python flask application Container

  • Now run our first container using same docker image ( myimage)
docker run -d -p 5000:5000 myimage
  • Verify if container is successfully created.
docker ps -a
This image has an empty alt attribute; its file name is image-45.png


In this tutorial we covered what is docker , what is python and using python flask application created a application on docker engine in one of the containers.

Hope this tutorial will helps you in understanding and setting up Python flask and python flask applications on docker engine in ubuntu machine.

Please share with your friends.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s