Part 3: Dockerize Flask application and build CI/CD pipeline in Jenkins

Upasana | February 09, 2021 | 3 min read | 2,177 views | Flask - Python micro web framework

This concise tutorial will walk you through Flask REST API from development to production.

What will you learn?

  1. Containerization using Docker

    1. What is Docker ?

    2. Why should we use it

    3. Creating docker image and running docker container

  2. Setting up CI/CD pipeline using Jenkins

    1. What is CI/CD, why is it important?

    2. CI/CD using Jenkins

  3. Cloud deployment

  4. Production monitoring

  5. Testing Aspects

    1. Junit 5

    2. Writing a End to End testcase in Java/Kotlin for REST API

Containerization using Docker

What is docker?

Docker is a containerization tool which bundles all the dependencies of given python program into single archive (called image) that can run on Docker. It is an open source tool that allows us to create, manage and deploy multiple instances of single application using containers.

Why should we use docker?

There are many benefits of using docker and we have listed few of them as follows:

  1. When we are using docker, we need not to create virtual environment as docker containers create their own environment for application.

  2. We can package up an application with all the libraries and dependencies and can create its image which can be used in deploying the application on any OS.

  3. With docker, we also get the benefit when we are moving from one cloud service to another. Let’s say we were earlier using AWS and we want to move whole architecture on Google cloud services. If our whole architecture was based on docker, then we need not to worry about setting up environments again.

Creating docker image and running docker container

Process of docker is as such that first we build docker image and then deploying the image as docker container

dockerfile image container

We need to create a docker file with the following configuration:

Just add a new file and name it as Dockerfile in root of project folder. Here we are assuming that project directory’s name is flask_project. In your case, it could be something else too, so replace the project directory in dockerfile as well. Here we are following few steps as follows:

  1. Selecting python3.6 as base image

  2. Copying the project folder

  3. Installing the requirements

  4. Exposing port of docker container

  5. Running the application

FROM python:3.6
COPY .  /flask_project
WORKDIR /flask_project
RUN pip install -r requirements.txt
EXPOSE  8000
CMD ["python", "src/"]

To build the docker image:

Building docker image
sudo docker build -t flask-app .
Running docker container
sudo docker run -p 8000:8000 --name flask-app -d flask-app
Login into Shell for this container
sudo docker exec -it flask-app /bin/bash

where flask-app is the container id.

Setting up CI/CD pipeline using Jenkins

What is CI/CD, why is it important?

CI (Continuous Integration) & CD (Continuous Delivery) is the process where developers follows the process of pushing their code oftenly to code hosting repositories like bitbucket, github, gitlab etc for automated integration testing and also it is one of best practices for devops. This also helps the developer in seeing the changes of application in environment.

CI/CD using Jenkins

We will be suing jenkins for making CI/CD pipeline.

  1. Create New Item from Jenkins home page.

  2. Fill in application name and choose pipeline.After that click OK. This will create a pipeline based job on jenkins.

  3. Fill in the general details in jenkins like description and github URL.

  4. Check GitHub hook trigger for GITScm polling This will take update of project whenever we do changes in project and push the code.

  5. In pipeline section, write down the pipeline code.

node {
   stage('Get Source') {
      // copy source code from local file system and test
      // for a Dockerfile to build the Docker image
      git ('')
      if (!fileExists("Dockerfile")) {
         error('Dockerfile missing.')
   stage('Build Docker') {
       // build the docker image from the source code using the BUILD_ID parameter in image name
         sh "sudo docker build -t flask-app ."
   stage("run docker container"){
        sh "sudo docker run -p 8000:8000 --name flask-app -d flask-app "

In pipeline, we are

  1. Getting project from github. Here we are assuming that project is in private repository.

  2. Building docker image

  3. Running docker container.

After above steps, save the job and trigger the build

Top articles in this category:
  1. Configure Logging in gunicorn based application in docker container
  2. Flask Interview Questions
  3. Part 2: Deploy Flask API in production using WSGI gunicorn with nginx reverse proxy
  4. Part 1: Creating and testing Flask REST API
  5. Deploying Keras Model in Production using Flask
  6. Deploying Keras Model in Production with TensorFlow 2.0
  7. Connect to MySQL with Python 3.x and get Pandas Dataframe

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.