Sometimes the easiest way to learn is by doing it. Quick and dirty step-by-step of running a Flask application inside a Docker container — an article I wish someone had taken 10 minutes of their day to write.
Please note this does not cover installing Docker, programming in Python, understanding Flask, and so on. When running publicly rather than in development, you should not use the built-in development server — this is just a blog post of something I had to figure out for a personal project that might be of help for someone on the internet.
Creating our Flask Application
The first step is, of course, creating our Flask application. Here is our demo-dockerflask folder structure:
The requirements.txt has only the following:
The app.py file has the following:
Notice that inside the app.run() params we have host=”0.0.0.0″. This is a necessity as 0.0.0.0 is a wildcard IP address that will match any possible incoming port on the host machine. We require this because port 127.0.0.1 inside a Docker container does not actually get captured and exposed into the host machine.
Flask automatically sets the application port to 5000 but we changed that by setting the variable port inside app.run() to port 80.
Creating our Dockerfile
Creating the Dockerfile is rather quite simple, and I would recommend reading into the Docker documentation if you would like a deeper understanding of it. However, for the sake of this demo project only the following is needed:
- Line 1: Downloads and uses python from DockerHub. The python image is a Dockerfile itself that utilizes AlpineOS as its base, and installs and makes the latest version of Python.
- Line 3: Set our working directory inside this Alpine box to /usr/app . This is the equivalent of cd into the directory.
- Line 4: Copies the local /app folder to the working directory.
- Line 6: Installs all the apps requirements.
- Line 8: When docker run is called, run the Flask application.
With the files above completed, we have successfully created a proper Flask application and Docker configuration. Now, we must build & run it.
Building our Container
First, we must build our container. To this, we do the following:
docker build -t demo-dockerflask:latest .
We are setting our projects tag ( -t ) to demo-dockerflask:latest , and specifying we are using the current directories’ Dockerfile signaled by the . at the end.
Running our Container
After it builds — in which during this period you will see a bit of commotion as the container installs Alpine, Flask, and the such — you are ready to run it. To run, you simply do:
docker run -d -p 80:80 demo-dockerflask
Here we set -d so that the container starts in detached mode (background mode);-p to publish the port of the container to your machine (so that you can access the Flask app); 80:80 to indicate we are publishing the container’s port 80 to our machines port 80(container:local); demo-dockerflask as our already built container name.
After executing the above command, the container will start, and you may utilize docker ps to view its live status.
Viewing our Application
Now that our application is running, we can access it on our local machine by visiting 127.0.0.1 (no port since we set our app to port 80) to see the following:
Simple and easy. Hope this helps some folks looking for a quick and easy tutorial on setting up Docker and Flask without all the fuzz.