Dockerizing your Flask Application

Dinesh Sonachalam
3 min readOct 28, 2018

In this article, we will see how to dockerize your flask application.

Step 1. Building a minimal Flask application

Let’s start by developing a simple flask application.

app.py

It’s a very basic “Hello World” application. Start a server with the following command.

$ python app.py

You should see a standard message saying that the server is running:

* Serving Flask app “app” (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 217–967–934

Now open localhost:5000 in the browser. You can see Hello world! We are using Flask! message.

Step 2. Creating a requirements file

Now we are going to create requirements.txt that contains the list of installed packages used by your application. Use the following command to create a requirements.txt file.

$ pip freeze > requirements.txt

When you open the requirements.txt file you can see the list of packages that are installed.

Click==7.0
Flask==1.0.2
itsdangerous==1.1.0
Jinja2==2.10
MarkupSafe==1.0
Werkzeug==0.14.1

Step 3. Creating a docker file for your Flask application

Create a file called Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.

Dockerfile

Step 4. Building the docker image

We created our Dockerfile and now its time to build the docker image. The docker build command allows you to build docker image from the Dockerfile.

docker build  -t ImageName:TagName dir

For example:

$ docker build -t simple_flask_app:1.0.0 .

I am creating a docker image called simple_flask_app and its TagName is 1.0.0. Here dot(.) in the end signifies the current working directory.

Sending build context to Docker daemon 25.23MB
Step 1/6 : FROM python:3
— -> 2cc378c061f7
Step 2/6 : COPY . /app
— -> 21f39fecd2f3
Step 3/6 : WORKDIR /app
— -> Running in 542d6593f653
Removing intermediate container 542d6593f653
— -> a3290259dab7
Step 4/6 : RUN pip install -r requirements.txt
— -> Running in 8b0749fe5eda
Collecting Click==7.0 (from -r requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
Collecting Flask==1.0.2 (from -r requirements.txt (line 2))
Downloading https://files.pythonhosted.org/packages/7f/e7/08578774ed4536d3242b14dacb4696386634607af824ea997202cd0edb4b/Flask-1.0.2-py2.py3-none-any.whl (91kB)
Collecting itsdangerous==1.1.0 (from -r requirements.txt (line 3))
Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting Jinja2==2.10 (from -r requirements.txt (line 4))
Downloading https://files.pythonhosted.org/packages/7f/ff/ae64bacdfc95f27a016a7bed8e8686763ba4d277a78ca76f32659220a731/Jinja2-2.10-py2.py3-none-any.whl (126kB)
Collecting MarkupSafe==1.0 (from -r requirements.txt (line 5))
Downloading https://files.pythonhosted.org/packages/4d/de/32d741db316d8fdb7680822dd37001ef7a448255de9699ab4bfcbdf4172b/MarkupSafe-1.0.tar.gz
Collecting Werkzeug==0.14.1 (from -r requirements.txt (line 6))
Downloading https://files.pythonhosted.org/packages/20/c4/12e3e56473e52375aa29c4764e70d1b8f3efa6682bef8d0aae04fe335243/Werkzeug-0.14.1-py2.py3-none-any.whl (322kB)
Building wheels for collected packages: MarkupSafe
Running setup.py bdist_wheel for MarkupSafe: started
Running setup.py bdist_wheel for MarkupSafe: finished with status ‘done’
Stored in directory: /root/.cache/pip/wheels/33/56/20/ebe49a5c612fffe1c5a632146b16596f9e64676768661e4e46
Successfully built MarkupSafe
Installing collected packages: Click, Werkzeug, itsdangerous, MarkupSafe, Jinja2, Flask
Successfully installed Click-7.0 Flask-1.0.2 Jinja2–2.10 MarkupSafe-1.0 Werkzeug-0.14.1 itsdangerous-1.1.0
Removing intermediate container 8b0749fe5eda
— -> 2a371a7b9a66
Step 5/6 : ENV PATH /app:$PATH
— -> Running in ae31f1287ea3
Removing intermediate container ae31f1287ea3
— -> 19447be8cf4c
Step 6/6 : CMD [ “python”,”app.py” ]
— -> Running in 8e19bcb2ea86
Removing intermediate container 8e19bcb2ea86
— -> 9b4eedc4fe5e
Successfully built 9b4eedc4fe5e
Successfully tagged simple_flask_app:1.0.0

Now we created our docker image called simple_flask_app.

$ docker image lists the images created in your system.

dinesh@dinesh-Inspiron-5559:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
simple_flask_app 1.0.0 9b4eedc4fe5e 5 minutes ago 958MB

Step 5. Running the Docker container

$ docker run -p <local_computer_port>:<container_port> -t <container_name/container_id>

Now running the container by using simple_flask_app image id.

$ docker run -p 5000:5000 7dcceeaa748b

Now you can see the application starts.

* Serving Flask app “app” (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 732–829–290

Step 6. To kill the Docker container

In order to stop the container that is running. We need to use docker kill. Lets first see the list of container that is running in our local system.

$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
93ff720163fa 7dcceeaa748b “python app.py” 27 minutes ago Up 27 minutes 0.0.0.0:5000->5000/tcp elegant_saha

Now let's use docker kill to stop the container that is running.

$ docker kill <Container_ID>

--

--