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.
This is continuation of part 2 Part 2: Deploy Flask API in production using WSGI gunicorn with nginx reverse proxy
What will you learn?
-
Containerization using Docker
-
What is Docker ?
-
Why should we use it
-
Creating docker image and running docker container
-
-
Setting up CI/CD pipeline using Jenkins
-
What is CI/CD, why is it important?
-
CI/CD using Jenkins
-
-
Cloud deployment
-
Production monitoring
-
Testing Aspects
-
Junit 5
-
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:
-
When we are using docker, we need not to create virtual environment as docker containers create their own environment for application.
-
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.
-
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
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:
-
Selecting
python3.6
as base image -
Copying the project folder
-
Installing the requirements
-
Exposing port of docker container
-
Running the application
FROM python:3.6
COPY . /flask_project
WORKDIR /flask_project
RUN pip install -r requirements.txt
EXPOSE 8000
CMD ["python", "src/main.py"]
To build the docker image:
sudo docker build -t flask-app .
sudo docker run -p 8000:8000 --name flask-app -d flask-app
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.
-
Create New Item from Jenkins home page.
-
Fill in application name and choose pipeline.After that click OK. This will create a pipeline based job on jenkins.
-
Fill in the general details in jenkins like description and github URL.
-
Check GitHub hook trigger for GITScm polling This will take update of project whenever we do changes in project and push the code.
-
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 ('https://github.com/upasana-mittal/flask-dockerized-jenkins.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
-
Getting project from github. Here we are assuming that project is in private repository.
-
Building docker image
-
Running docker container.
After above steps, save the job and trigger the build
Top articles in this category:
- Configure Logging in gunicorn based application in docker container
- Flask Interview Questions
- Part 2: Deploy Flask API in production using WSGI gunicorn with nginx reverse proxy
- Part 1: Creating and testing Flask REST API
- Deploying Keras Model in Production using Flask
- Deploying Keras Model in Production with TensorFlow 2.0
- Connect to MySQL with Python 3.x and get Pandas Dataframe