How to Dockerize a Flask Application š³š
Panchanan Panigrahi
Posted on February 29, 2024
Introduction:
In the ever-evolving world of software development, efficient deployment and scalability are key factors for success. Docker, a containerization platform, has emerged as a powerful solution to streamline the deployment process. This blog will guide you through the steps of dockerizing a Flask application, unraveling the magic behind each line of the Dockerfile. š©āØ
Understanding Docker:
Docker is an open-source platform designed to automate the deployment of applications inside lightweight, portable containers. A container encapsulates an application and its dependencies, ensuring consistency across different environments. This eliminates the infamous "it works on my machine" problem and simplifies deployment, making it an invaluable tool in modern development. šš¦
Why Containerize Your Application?
Containerization provides several benefits, such as:
Isolation: Containers encapsulate applications and dependencies, avoiding conflicts with the host system or other applications. š”ļøš¦
Consistency: Containers ensure that the application runs the same way across various environments, from development to production. šš
Efficiency: Containers are lightweight and share the host OS kernel, leading to faster startup times and efficient resource utilization. āļøšØ
How to Set Up the Project
Basic directory structure
After completing the following steps, our application directory structure will look like this:
Flask-app/
āāā docker-compose.yml
āāā Dockerfile
āāā README.md
āāā requirements.txt
āāā src/
āāā app.py
āāā templates/
āāā error.html
āāā index.html
For your reference, You can checkout the GitHub repo for this project.
[GitHub Link: https://github.com/panchanandevops/Building-Dockerfiles/tree/main/Flask-app]
Dockerfile for Flask application
In this Project, we will use a simple Dockerfile (Single stage) with a docker-compose.yaml file.
Remember: This dockerfile for development and learning purpose only. So we will use bind mount type volume inside docker-compose.yaml.
Here is the Dockerfile For our flask app:
FROM python:3.11-alpine
# Set up environment variables for Python
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Create and set the working directory
WORKDIR /app
# Copy only the requirements file first to leverage Docker caching
COPY requirements.txt .
# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy the entire application code
COPY . .
# Expose the port your application will run on
EXPOSE 8080
# Specify the command to run on container start
CMD ["python", "src/app.py"]
Here is the docker-compose.yaml For our flask app:
version: '3'
services:
python-app:
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:8080"
volumes:
- .:/app:ro
Breaking Down the Dockerfile:
Let's delve into each line of the Dockerfile provided:
FROM python:3.11-alpine
This line sets the base image to Python 3.11 on Alpine Linux, a lightweight distribution known for its small footprint. šļøš
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
These lines set environment variables to enhance Python's performance within the container. āļøš
WORKDIR /app
Sets the working directory inside the container to /app. šš¼
COPY requirements.txt .
Copies the requirements.txt file to the working directory, allowing Docker to cache dependencies installation for efficiency. š¦š
RUN pip install --no-cache-dir -r requirements.txt
Installs the Python dependencies listed in requirements.txt without caching to ensure the latest versions are fetched. šš§
COPY . .
Copies the entire application code into the container's working directory. šš
EXPOSE 8080
Informs Docker that the application inside the container will use port 8080. šš¢
CMD ["python", "src/app.py"]
Specifies the command to run when the container starts, launching the Flask application. šš
How to Build a Docker Image:
Building a Docker image is a crucial step in containerizing your Flask application. It involves packaging your application, along with its dependencies, into a single, portable image. To build the Docker image, open a terminal and navigate to the directory containing your Dockerfile and application code. Execute the following command:
docker build -t panchanandevops/flask-app:v1.0.0 .
Here's what each part of the command does:
- docker build: Initiates the build process. š ļø
- -t panchanandevops/flask-app:v1.0.0: Tags the image with a name and version. Adjust the name and version according to your preferences. š·ļø
- .: Specifies the build context, indicating the location of the Dockerfile and application code. šš
This command compiles your Flask application into a Docker image, making it ready for deployment. š¢š
How to Run an Image as a Container:
Once you have successfully built your Docker image, the next step is to run it as a container. This involves creating an instance of your image, which is a runnable environment for your Flask application. Execute the following command in the terminal:
docker run --name my-flask-server -p 8080:8080 panchanandevops/flask-app:v1.0.0
Breaking down the command:
- docker run: Initiates the container creation process. ššļø
- --name my-flask-server: Assigns a name to the running container for easy identification. š·ļøš¤
- -p 8080:8080: Maps port 8080 of the host to port 8080 of the container, allowing external access. šš¢
- panchanandevops/flask-app:v1.0.0: Specifies the image to use for creating the container. šļøš¼ļø
This command launches your Flask application inside a Docker container, making it accessible on port 8080 of your host machine. šš
Visit http://localhost:8080/ in your browser to see the Flask app. šš
How to Stop and Delete the Container:
At times, you might need to stop and delete the running Docker container. Execute the following commands:
docker stop my-flask-server
docker rm my-flask-server
Breaking down the commands:
- docker stop my-flask-server: Stops the running container named "my-flask-server". ššļø
- docker rm my-flask-server: Deletes the stopped container named "my-flask-server". šļøšļø
What is Docker Compose?
Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you can define a multi-container environment in a single file, making it easier to manage, scale, and deploy applications. It provides a straightforward way to configure your application's services, networks, and volumes. Let's dive into how to leverage Docker Compose for our Flask application. š§©š
Setting Up Docker Compose:
1. Create a docker-compose.yml
File:
Create a new file named docker-compose.yml
in the root of your project. This file will define the services, networks, and volumes required for your application. Here's a basic example:
version: '3'
services:
flask-app:
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:8080"
volumes:
- .:/app:ro
2. Understanding the docker-compose.yml
:
version: '3': Specifies the version of the Docker Compose file format.
services: Defines the services that constitute your application.
flask-app: Name of the service. You can customize this.
build: Configuration for building the Docker image.
context: The build context, same as in the
docker build
command.dockerfile: The Dockerfile to use for building the image.
ports: Maps the container's port to the host machine.
volumes: Specifies a volume for bind mounting, ensuring code changes reflect in real-time.
3. Building and Running with Docker Compose:
Open a terminal and run the following command to build and run your application using Docker Compose:
docker-compose up -d
Breaking down the command:
- docker-compose up: Builds, (re)creates, starts, and attaches to containers for a service.
- -d: Runs the containers in the background.
4. Accessing Your Flask App:
Visit http://localhost:8080/ in your browser, and you should see your Flask app running.
5. Stopping and Removing Containers:
To stop and remove the containers created by Docker Compose, run:
docker-compose down
This command will stop and remove the containers, networks, and volumes defined in your docker-compose.yml
.
Conclusion:
In this Dockerized Flask journey, you've mastered modern software deployment by encapsulating your app in a Docker container. Ensuring consistency and portability, you're now equipped for diverse environments.
As you delve deeper into containerization, explore advanced Docker features, optimize your Dockerfile, and integrate Docker into a robust CI/CD pipeline. Consider environment configuration, implement monitoring, and logging for a production-ready setup.
This guide marks the start of your Docker and Flask exploration. Embrace containerization's versatility for efficient, scalable, and hassle-free deployments. Happy containerizing! šš
Posted on February 29, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.