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

Carvia Tech | August 31, 2019 | 3 min read | 1,933 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

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. Flask Interview Questions
  2. Top 100 interview questions on Data Science & Machine Learning
  3. Google Data Scientist interview questions with answers
  4. Python coding challenges for interviews
  5. Configure Logging in gunicorn based application in docker container
  6. Part 2: Deploy Flask API in production using WSGI gunicorn with nginx reverse proxy
  7. Part 1: Creating and testing Flask REST API

Find more on this topic:
Machine Learning image
Machine Learning

Data science, machine learning, python, R, big data, spark, the Jupyter notebook, and much more

Last updated 1 week ago

Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info