Better Programming

Advice for programmers.

Follow publication

How to Run a Simple Flask Application in a Container

David Wainaina
Better Programming
Published in
2 min readAug 3, 2022
Photo by William Hook on Unsplash

You are testing a theory and want to launch a Python application container quickly. Maybe you are just learning how to containerize a simple Python application. Welcome to this article, where we will do just that.

The first thing is to keep our house in order. Install Python on your workstation. I am assuming you are using a Unix-based system.

Create a folder to host the application and create the first main.py file. Since we don’t want to mix our application with the system-wide Python libraries, install pipenv to enable us just install packages for this application on the current directory. Then we will install flask, gunicorn, and requests packages using pipenv. This will create a Pipfile that contains the application dependencies.

$ mkdir singlepageflask $ touch main.py#install pipenv
$ python3 -m pip install pipenv
# install dependencies; flask, gunicorn and requests packages
$ python3 -m pipenv install flask gunicorn requests

We will create an entry script to be used when you want to run this application.

$ touch appentry.sh
$ chmod +x appentry.sh

Add the following content to start a gunicorn wsgi server when the script is run. The gunicorn command simply calls the app variable in the main.py file and listens from all addresses on port 8000.

#!/bin/bash
python3 -m gunicorn main:app -w 2 --threads 2 -b 0.0.0.0:8000

Test this locally by running ./appentry.sh.

Create a Dockerfile after testing that it is running locally.

Note that, in our appentry.sh, we declared that our application should listen from any address, 0.0.0.0. This will enable the container to accept requests coming from outside.

Build the application using Podman and start the container while mapping host port 8000 to container port 8000.

#build the container image
$ podman build -t singlepage .
#start a container using the image and map to host on port 8000
$ podman run -d -p 8000:8000 --name singlepage singlepage
#Test the endpoint;
$ curl http://localhost:8000/jokeme

Test the application from your workstation with the curl command or your favorite API testing application by sending a request to http://localhost:8000/jokeme.

You can find the full code on my git repository on branch singleflask.

For Windows users, you might face problems where the container cannot find the appentry.sh script. This is because windows use CRLF line endings while Unix (container is Unix based) uses LF line endings.

Therefore, when you create the script on windows and try to run it on the container, it cannot interpret the script correctly, leading to that confusing “no such file or directory found” error.

To correct this, convert the line endings from CRLF to LF for the appentry.sh script.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

David Wainaina
David Wainaina

Written by David Wainaina

Just a guy who loves exploring technologies

Write a response