How to Dockerize a Python Flask Application

In this article, we will set up a python Flask application and Dockerize it efficiently in a few minutes. 

Steps to Follow

Let's Create a simple Python Flask Application. Refer to the code here: Docker Flask Repo.

Now, its time to package our application into a docker image. Create a Dockerfile with the following steps:

1. This installs Python3.7 on the image

FROM python:3.7


2. Specify 
DEBIAN_FRONTED=noninteractive.

ARG DEBIAN_FRONTED=noninteractive


3. Install all the system requirements that are required for the docker image

RUN apt-get update && apt-get install -y --no-install-recommends apt-utils > /dev/null

RUN apt-get install -y build-essential tcl

RUN apt-get install -y systemd-sysv

RUN apt-get update  > /dev/null

RUN apt-get install  -y wget > /dev/null

RUN apt-get install  -y zip > /dev/null

RUN apt-get install  -y libaio1 > /dev/null

RUN apt-get update > /dev/null

RUN apt-get install  -y alien > /dev/null


4. Let's jump into the working directory:

WORKDIR /


5. Create a new directory for keeping the Flask application code:

RUN mkdir /code


6. Now change the working directory as shown below:

WORKDIR /code


7. Copy all your workspace files into the image:

COPY . .


8. Now, install all your code dependencies from requirements.txt:

pip install -r requirements.txt


9. Don't forget to upgrade your pip:

RUN pip install --upgrade pip --user


10. Change your working directory where your packaging is done:

WORKDIR /code/k8s

 

11. Let's shine the docker instructions from here:

ENTRYPOINT ["make"]


We are done with creating the docker file that is required for a docker image. Now, run the docker file to create an image:

docker build -f ./packaging/flask-docker/Dockerfile . -t flask-docker


Now, let's create the docker-compose.yml file to run the docker image in a container.

Let's provide the information required for the container to start. We have only one service here, and the image name docker_flask. Run the app.py file with the make command.  

version: '3'

services:

  flask_web:

    image: docker_flask

    command: ["run"]

    working_dir: /code/flask

    ports:

      - "5000:5000"


Your make file has does starts your application:

run:

    python app.py


Now, start your container which starts your Python Flask web app.

docker-compose up -d


Now your simple Python Flask application is up and running in the Docker container. Now check the container id, with:

docker ps -a


This lists all your docker containers. Check your application logs with:

docker logs -f {container_id}


Hurray, we are done running the flask web app on a docker container!

We Provide consulting, implementation, and management services on DevOps, DevSecOps, DataOps, Cloud, Automated Ops, Microservices, Infrastructure, and Security

 

Services offered by us: https://www.zippyops.com/services

Our Products: https://www.zippyops.com/products

Our Solutions: https://www.zippyops.com/solutions

For Demo, videos check out YouTube Playlist: https://www.youtube.com/watch?v=4FYvPooN_Tg&list=PLCJ3JpanNyCfXlHahZhYgJH9-rV6ouPro

 

If this seems interesting, please email us at [email protected] for a call.



Relevant Blogs:






Recent Comments

No comments

Leave a Comment