DEV Community

Nokuphila Simelane
Nokuphila Simelane

Posted on

How to Dockerize a Flask Application

Introduction

Docker is an open-source tool that ships your application with all the required project dependencies as a single package. Docker can be used to bundle your application with all the application libraries you'll need to run your application.
A container is built from an image that specifies its precise contents.

In this blog article post, we will learn “How to dockerize a Flask Web Application”.

But Why Dockerize ?

“Dockerize” means to configure an application or service to run within a Docker container. Therefore, when you “Dockerize” an application, you are essentially preparing it for containerization using Docker, a containerization platform.
Dockerizing an application involves firstly creating a Dockerfile, building a Docker image, then running the application as a Docker container.

How to start with Dockerizing ?

Step 1: Installing Docker
You can find installation instructions for many operating systems on the official docker website. Open the provided link to find the different Docker downloads for your preferred operating system.
Docker website: [ https://www.docker.com/products/docker-desktop/)

Step 2: Create a Flask hello world application “helloworld.py”

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
  return 'Hello to World!'

if __name__ == "__main__":
   app.run()
Enter fullscreen mode Exit fullscreen mode

Step 3: Create a “requirements.txt” file

This should be the flask version that should be installed along with your necessary application dependencies. Let's Assume you have already installed Flask. in your local environment. Should you struggle visit this page to learn steps to install and run a flask application.

Flask==3.0.0
Enter fullscreen mode Exit fullscreen mode

After steps 1 and 2, we should have a skeleton structure like below

HelloDocker-project
├─── requirements.txt
├─── app.py

Step 4: Create a Dockerfile
In your project’s directory, create a file named Dockerfile do not add a file extension. This docker file contains instructions for building a Docker image. Customize it to suit your application's unique requirements. Dockerfile is a script that provides a set of instructions for building a Docker image.
This file specifies
the base image,
copying files,
and setting up the environment,
as well as defining how your application should run within the container.
These commands you'll typically find in a Dockerfile what they do:

1. FROM: This is the first and most crucial command in a Dockerfile. It specifies the base image upon which your Docker image will be built. The base image typically includes an operating system and a runtime environment (e.g., Python, Node.js, Java). We use “Python” in our example.

2. COPY and ADD: These commands copy files and directories from your local filesystem into the Docker image. They are often used to include your application code, configuration files, and dependencies.

3. RUN: The RUN command executes commands within the container during the image-building process. It is typically used to install packages, libraries, or dependencies that your application needs.

4. WORKDIR: It sets the working directory for any subsequent RUN, CMD, ENTRYPOINT, COPY, and ADD instructions. This is where your application will be placed in the image, and it's where the container will start by default.

5. EXPOSE: This command informs Docker that the container listens on specific network ports. It does not actually publish the port; it is merely documentation for users.

6. CMD and ENTRYPOINT: These commands specify the command that should be executed when the container is started. You generally use one or the other, not both. CMD provides default arguments for an executing container, while ENTRYPOINT specifies the main command.

All these steps below:

# Use an official Python runtime as the base image
FROM python:3-alpine

# Set the working directory in the container
WORKDIR /app

# Copy your project files into the container
COPY requirements.txt ./

# Install any project-specific dependencies
RUN pip install -r requirements.txt

# Bundle app source
COPY . .

EXPOSE 5000

# Specify the command to run your project
CMD [ "flask", "run","--host","0.0.0.0","--port","5000"]
Enter fullscreen mode Exit fullscreen mode

A docker image is created from the instructions in the DockerFile.
HelloDocker-project
├─── requirements.txt
├─── app.py
├─── dockerFile

Step 5: Build the Docker Image
Open a terminal or command prompt in your project directory and use the docker build command to build a Docker image based on your Dockerfile. Be sure to specify a name and optionally a tag for your image.

Docker command to build container image
I've named my container image hello-docker

docker build --tag hello-docker
Enter fullscreen mode Exit fullscreen mode

Hopefully the image was created without any errors! Run the following command to list your built images

docker images
Enter fullscreen mode Exit fullscreen mode

If your image was created successfully, you should get an output similar to the one below

REPOSITORY     TAG       IMAGE ID       CREATED         SIZE
hello-docker   latest    0895121661e0   3 minutes ago   57.1MB
Enter fullscreen mode Exit fullscreen mode

Now that the image is created, I can proceed to run the container with the following command

docker run -it -p 3333:3333 -d hello-docker
Enter fullscreen mode Exit fullscreen mode

I ran my docker image on Github Codespaces, a free online editor similar to VScode.
This is the example output on my browser

Hello Flask app

That's it! Hope you have learned and gained valuable skill in dockerizing a python application.

Top comments (0)