In this tutorial, you will learn how to create a basic Flask app with Docker. You will set up your app with a Dockerfile, and manage the images with an automated build process.
In this process, you’ll also learn how to use multiple Python virtual environments and keep your source code organized. If you’re new to Python or Flask, you may want to check out our beginner guide to Python as well as our beginner guide to Flask first. They cover the basics of these frameworks so that you can follow along better in this tutorial. Let’s get started!
What is Flask?
Flask is a lightweight Python framework for building web applications. It is simple, flexible, and pragmatic. It can be easily extended with the use of extensions and plug-ins. Flask can be used to create small websites or large-scale, complex applications.
Flask is used by companies like Pinterest, Twitter, Walmart, and Cisco. One of the common uses of Flask is for REST APIs that are used to interact with other systems. Applications written in Flask can also easily be deployed to a server or a cloud.
Create a Basic Flask Application
Before you can create a Docker image with your application, you have to have a basic app that you can run locally or on a server.
In this section, you will create a basic app with Flask and then run it in a Docker container. You can use your preferred editor to create the app, or you can use the following command in your terminal to create a new app:
- Let’s begin with creating a new directory:
mkdir flask-app && flask-app
- Next, create the Python virtual environment and then activate the environment.
python3 -m venv venv
- Now install the Flask python module under the virtual environment.
pip install Flask
- The below command will create the requirements.txt file with the installed packages under the current environment. This file is useful for installing module at deployments.
pip freeze > requirements.txt
- Now, create a sample Flask application.. You can write your code in a .py file and run it with the python command.
Add the below snippet.
- Your sample Flask application is ready. You can run this script with Python now.
flask run --host 0.0.0.0 --port 5000
Now that you have a basic app, you can run it in a Docker container by creating a file called Dockerfile in the same directory as your app.py file.
Create a Dockerfile for Your Flask Application
A Dockerfile is a file that contains instructions on how to build your image. It describes the entire process of building your image from the installation of your Python app to the creation of your container.
- Let’s create a file named Dockerfile under the project directory. This is the file docker reads instructions to build image.
Add the following code:
Save the file and close it.
- Next, create the Docker image by running the below-mentioned command. Here “flask-app” is the image name.
docker build -t flask-app .
- This image will be created in local image registry. Then you can create a Docker container with the following command.
sudo docker run -it -p 5000:5000 -d flask-app
- Now, verify that container is running on your system.
docker containers ls
- Finally, open a browser and connect to localhost on port 5000 (or use your own defined port). You should see the flask application.
You can use a Dockerfile to automate the building and updating of your image. This is helpful if you’re working with a team and people are adding code to the same application.
In this tutorial, you learned how to create a basic Flask app and build a Docker image with it. You also learned how to create a private registry and automate the building and updating of your image. This is a great way to create reproducible builds and container images that are easy to share and deploy. This can be helpful if you need to set up servers or if you want to run your app on different machines.