In this article, we’re going to run a simple python application on a flask web-server using docker. I’ll assume that you have some familiarity with docker and that you have docker installed and running. If not, visit this link to get set up. I’ll also assume that you have python3 installed on your local machine. If you don’t you can visit this link to get python3 downloaded and installed
Part 1: Setting up the flask app
First, let’s build a simple flask app and get it running locally without using docker. Make a new directory for this project and navigate to it in your terminal, then create the following files in this directory:
Once you’ve done this, populate
requirements.txt with the following:
Since this is just a demo application, we’ll only need to include the flask package. Listing your packages in a
requirements.txt file is a common practice with python applications. As your project grows, you can add more packages to this file along with specific version constraints as needed.
In python, it’s also a common practice to use separate virtual environments for each project to prevent compatibility issues between package versions from one project to the next. We’ll see later that this won’t be necessary when running our application in docker since a docker container is a self-contained environment.
To install our required packages, we’ll first create a virtual environment for this project. If you haven’t created a virtual environment before, run
pip3 install virtualenv to install the virtualenv package.
python3 -m venv simple-flask-env to create a virtual environment called “simple-flask-env” in the current directory. To activate the environment, run
source simple-flask-env/bin/activate. Now, any packages that are installed will live within this specific environment.
We can now install the packages in our
requirements.txt file by running
pip3 install -r requirements.txt.
Now that we have flask installed in our local python environment, populate
main.py with the following code:
To start up the flask webserver, run
python3 main.py in the terminal. Afterwards, you should be able to access your application at http://localhost:5000. 5000 is the default port used for flask applications.
Congratulations, we just got our simple flask app running locally! Before moving on to running this app in a docker container, stop the flask web-server by entering “ctrl+c” in the terminal window. Then, deactivate the virtual environment we created by running the
deactivate command in the terminal.
Part 2: Dockerizing the flask application
Now that we have our hello world app up and running, let’s run it in a docker container. First, we’ll need a Dockerfile. This is essentially a blueprint of instructions for setting up the environment in which our flask application will run. We’ll use this Dockerfile to build a docker image, which we can run as a docker container.
Create a new file called
Dockerfile and populate it with the following code:
Let’s go through this file line by line:
- Line 1 uses the official python docker image as the base image for our dockerfile. This essentially takes care of making sure that we have the latest stable version of python installed in our container (3.9.1 at the time of writing this article).
- Line 2 sets
/appas the working directory inside our container. All commands run inside the container after this line will be run from this directory.
- Line 3 copies the contents of our working directory on our host machine to the
/appdirectory in the container
- Line 4 runs the
pip3 install -r requirements.txtcommand from the working directory in our container (which we set to
/appon line 2). This will use our
requirements.txtfile to install our specified dependencies.
- Line 5 exposes port 5000 in our docker container, so that it can accept traffic to our app on that port.
- Line 6 executes our python code in main.py to start up our flask webserver. This command will execute immediately after our container is built and run.
Now that we have the Dockerfile set up, we can build our image and run our docker container. To build the image, run the following command:
docker build -t simple-flask-app .
This will build the image and tag it with the name “simple-flask-app” using the instructions in the Dockerfile, which it will search for in the current directory (to access a Dockerfile in another directory, specify the path to that file instead of
. in the command above).
Once the image is built, we can run it as a container using the
docker run command:
docker run simple-flask-app
However, if we try to visit our app at http://localhost:5000, we’ll find that it is inaccessible. This is because even though the app is listening at port 5000, this is referring to the port inside the container, not on our host machine. To fix this, we need to set up port-forwarding so that our local machine knows to forward traffic on port 5000 to the same port in docker. We can do this easily by modifying the
docker-run command as follows
docker run -p 5000:5000 simple-flask-app
Now if you visit the app in the browser, you should see that it is working successfully:
Congratulations! You just dockerized a python/flask web application. There’s much more to learn about Docker, but this is already a great start that you can use to apply as a starting point for your own applications. Please feel free to leave any questions in the comments below. Thanks for reading!