Building a Flask Web Application With Docker: A Step-by-Step Guide
Flask is a popular web framework for building web applications in Python. Docker is a platform that allows developers to package and deploy applications in containers.
Join the DZone community and get the full member experience.Join For Free
Flask is a popular web framework for building web applications in Python. Docker is a platform that allows developers to package and deploy applications in containers. In this tutorial, we'll walk through the steps to build a Flask web application using Docker.
Before we begin, you must have Docker installed on your machine. You can download the appropriate version for your operating system from the official Docker website. Additionally, it would help if you had a basic understanding of Flask and Python.
Creating a Flask Application
The first step is to create a Flask application. We'll create a simple "Hello, World!" application for this tutorial. Create a new file called
app.py and add the following code:
from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return 'Hello, World!'
Save the file and navigate to its directory in a terminal.
Creating a Dockerfile
The next step is to create a Dockerfile. A Dockerfile is a script that describes the environment in which the application will run. We'll use the official Python 3.8 image as the base image for our Docker container.
FROM python:3.8-slim-buster: This sets the base image for our Docker container to the official Python 3.8 image.
WORKDIR /app: This sets the working directory inside the container to
COPY requirements.txt .: This copies the
requirements.txtfile from our local machine to the
/appdirectory inside the container.
RUN pip install --no-cache-dir -r requirements.txt: This installs the dependencies listed in
COPY . .: This copies the entire local directory to the
/appdirectory inside the container.
CMD [ "python", "app.py" ]: This sets the command to run when the container starts to
Create a new file called
Dockerfile and add the following code:
FROM python:3.8-slim-buster # Set the working directory WORKDIR /app # Install dependencies COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # Copy the application code COPY . . # Run the application CMD [ "python", "app.py" ]
Save the Dockerfile and navigate to its directory in a terminal.
Building the Docker Image
The next step is to build a Docker image from the Dockerfile. Run the following command to build the image:
docker build -t my-flask-app .
This command builds an image named
my-flask-app from the Dockerfile in the current directory. The
. at the end of the command specifies that the build context is the current directory.
Starting the Docker Container
Now that we have a Docker image, we can start a container from it. Run the following command to start a new container from the
my-flask-app image and map port 5000 on the host to port 5000 in the container:
docker run -p 5000:5000 my-flask-app
This command starts a new container from the
my-flask-app image and maps port 5000 on the host to port 5000 in the container.
Testing the Flask Application
Finally, open your web browser and navigate to
http://localhost:5000. You should see the "Hello, World!" message displayed in your browser, indicating that the Flask application is running inside the docker application.
Customizing the Flask Application
You can customize the Flask application by modifying the
app.py file and rebuilding the Docker image. For example, you could modify the
hello function to return a different message:
@app.route('/') def hello(): return 'Welcome to my Flask application!'
app.py file and rebuild the Docker image using the
docker build command from earlier. Once the image is built, start a new container using the
docker run command from earlier. When you navigate to
http://localhost:5000, you should see the updated message displayed in your browser.
- Docker simplifies the process of building and deploying Flask applications, as it provides a consistent and reproducible environment across different machines and operating systems.
- Docker allows for easy management of dependencies and versions, as everything needed to run the application is contained within the Docker image.
- Docker facilitates scaling and deployment of the Flask application, allowing for the quick and easy creation of new containers.
- Docker adds an additional layer of complexity to the development and deployment process, which may require additional time and effort to learn and configure.
- Docker may not be necessary for small or simple Flask applications, as the benefits may not outweigh the additional overhead and configuration.
- Docker images and containers can take up significant disk space, which may concern applications with large dependencies or machines with limited storage capacity.
In this tutorial, we've walked through the steps to build a Flask web application using Docker. We've created a simple Flask application, written a Dockerfile to describe the environment in which the application will run, built a Docker image from the Dockerfile, started a Docker container from the image, and tested the Flask application inside the container. With Docker, you can easily package and deploy your Flask application in a consistent and reproducible manner, making it easier to manage and scale your application.
Opinions expressed by DZone contributors are their own.
How To Use Pandas and Matplotlib To Perform EDA In Python
Future of Software Development: Generative AI Augmenting Roles and Unlocking Co-Innovation
Observability Architecture: Financial Payments Introduction
Understanding Data Compaction in 3 Minutes