Create a Dockerfile for Flask Application

By khoanc, at: 13:26 Ngày 08 tháng 6 năm 2023

Thời gian đọc ước tính: 6 min read

Create a Dockerfile for Flask Application
Create a Dockerfile for Flask Application

How to Create a Dockerfile for a Flask App with Minimal Setup

Flask is a lightweight and powerful web framework for Python that’s widely used for developing web applications and APIs. This is the best for small projects, while Django is for bigger ones.

Docker makes it easy to containerize your Flask app, ensuring consistent behavior across different environments.

In this guide, we’ll show you how to create a minimal Dockerfile for a Flask app, ideal for beginners and those looking for a straightforward setup.

 

What Is Docker, and Why Use It with Flask?

Docker allows developers to package their applications and dependencies into isolated containers. For Flask apps, Docker offers the following benefits:

  • Portability: Ensures your app runs the same way in development, staging, and production environments.
     
  • Dependency Isolation: Avoids conflicts between Python packages.
     
  • Ease of Sharing: Share a single Docker image with your team or deploy it seamlessly.

 

Prerequisites

To follow this guide, you’ll need:

  • A basic Flask application.
     
  • Docker installed on your system (Download Docker here).
     
  • Familiarity with basic Flask and Docker concepts.

 

Creating a Minimal Dockerfile for Your Flask App

A Dockerfile is a set of instructions for building a Docker image. Below is a step-by-step guide to create one for a simple Flask app.


Choose a Base Image

We’ll use the lightweight Python Slim image as the base:

FROM python:3.10-slim


This image includes Python without unnecessary extras, making it efficient for our needs.

 

Set the Working Directory

Specify the directory where your application’s code will reside:

WORKDIR /app


This organizes the container's file system and ensures subsequent commands run in the /app directory.

 

Install Dependencies

Copy the requirements.txt file to the container and install the dependencies:

COPY requirements.txt /app/
RUN pip install --no-cache-dir -r requirements.txt


The --no-cache-dir flag prevents caching files, keeping the image lightweight.


Copy the Application Code

Copy your Flask app’s code into the container:

COPY . /app/


This transfers all files from your local project directory to the container’s /app/ directory.

 

Expose a Port

Expose port 5000, which is Flask’s default development server port:

EXPOSE 5000


This makes the app accessible on port 5000 when the container is run.

 

Set the Command to Run the App

Define the default command to start your Flask application:

CMD ["python", "app.py"]


Replace app.py with the name of your Flask application’s main file. This command starts the Flask development server.

 

Building and Running Your Container


Build the Docker Image

Run the following command in the directory containing your Dockerfile:

docker build -t flask-app .


This creates a Docker image named flask-app.

 

Run the Docker Container

Start the container with:

docker run -p 5000:5000 flask-app


This maps port 5000 on the container to port 5000 on your local machine. Open your browser and navigate to http://localhost:5000 to see your Flask app running.

 

Enhancements for Production

The setup above is suitable for development. For production, consider the following:
 

  • Use gunicorn as the application server instead of the Flask development server.
     
  • Add nginx as a reverse proxy for handling HTTP requests.
     
  • Use docker-compose to manage services like a database.

 

Common Errors and Fixes


Dependencies Missing

If dependencies aren’t installed, ensure the requirements.txt file is in the correct directory and rebuild the container:

docker build --no-cache -t flask-app .

 

App Not Running

Ensure your Flask app works locally before containerizing it. Check container logs for errors:

docker logs <container_id> </container_id>

 

Conclusion

You’ve just created a minimal Dockerfile to containerize your Flask app. This approach provides a solid starting point for leveraging Docker in your development workflow. Experiment with production configurations and explore additional tools like Docker Compose for managing multi-container setups.

For further reading, check out the Docker Documentation and the Flask Documentation

The whole Dockerfile is here

# Use a lightweight Python image as the base
FROM python:3.10-slim

# Set the working directory in the container
WORKDIR /app

# Copy the requirements file into the container
COPY requirements.txt /app/

# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy the application code into the container
COPY . /app/

# Expose port 5000 for the Flask development server
EXPOSE 5000

# Set the command to run the Flask app
CMD ["python", "app.py"]

 


Theo dõi

Theo dõi bản tin của chúng tôi và không bao giờ bỏ lỡ những tin tức mới nhất.