Adding Flask App to Docker Container

Just a quick how to for taking a flask app and putting it inside a docker container

Creating the Docker File

The Docker file is created in a parent directory of the flask app. This is where the Python version, the libraries used and the commands to run the app.

Minor Changes to app.py

We do need to amend the app.py file before we do continue. That is to remove the debugging.

Build Docker Container

Quite a few commands here to build and test the Docker container…

Build the container. This will download python and all the libraries needed that are in the requirements.txt file.

Test the newly built docker container

Save the newly built docker container

Tag the container with a version tag. Blank is for latest

Now we can see both docker containers. Different names, but both the same. The repo named one will be uploaded.

This is to upload the container to a repo. This is not required to run the container. Just something to do if there is a central store of Docker containers. I will be using a server to pull the container down to and run it.

Once we are on a server that has access to the Docker repo we can download the image

Now it’s on the server. We can look at running the container.
Before this though as it is Flask it will run on port 5000 by default. We need to see if anything is running on that port or anything else we may want to use.

Run the container on port 8080.

Now we can check to see it is actually running, then test. That’s all the container should accept connections and function just like it did when testing the Flask app in VScode or other IDE.

Now if we ever need to stop the container we can use the command below. Be sure to use the container ID and not the name of the image.

Leave a Comment

Your email address will not be published. Required fields are marked *