FOSS_Development (CT207 - CT213H) - College of ICT - Can Tho University
LAB 5
CI/CD PIPELINE USING JENKINS, GITHUB AND DOCKER
Fullname: Lê Hữu Lâm Thư
Student ID: B2206018
- Note: screenshots need to be clear and good-looking; submissions must be in PDF format.
1. Manually dockerize a Flask project
1.1. Deploy a Flask application
- Create a sample Flask application:
$mkdir cicd_tutorial ; cd cicd_tutorial
$nano flask_docker.py
flask_docker.py
from flask import Flask
app = Flask(__name__)
@[Link]('/')
def hello_world():
return 'Hello FOSS'
if __name__ == '__main__':
[Link](debug=True,host='[Link]')
- Install pip (package installer for Python), and then the Flask framework
$sudo apt install python3-pip -y
$pip3 install flask
- We can test it out by running:
$python3 flask_docker.py
* Running on [Link] (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 135-043-124
- Access the application from a browser ([Link] (take a screenshot)
FOSS_Development (CT207 - CT213H) - College of ICT - Can Tho University
1.2. Dockerize a Flask application using Dockerfile
- Update the apt package index and install Docker
$sudo apt update
$sudo apt install [Link] -y
- Add current user to the docker group:
$sudo usermod -aG docker ${USER}
$su - ${USER}
- Check whether you can access and download images from Docker Hub
$docker run hello-world
The output will indicate that Docker is working correctly:
Hello from Docker!
FOSS_Development (CT207 - CT213H) - College of ICT - Can Tho University
This message shows that your installation appears
to be working correctly.
- Create a [Link] file
$nano [Link]
[Link]
Flask==2.2.2
- Create a Dockerfile file
$nano Dockerfile
Dockerfile
FROM ubuntu:latest
MAINTAINER Tuan Thai "tuanthai@[Link]"
RUN apt update -y
RUN apt install -y python3-pip python3-dev build-essential
FOSS_Development (CT207 - CT213H) - College of ICT - Can Tho University
ADD . /flask_app
WORKDIR /flask_app
RUN pip3 install -r [Link]
ENTRYPOINT ["python3"]
CMD ["flask_docker.py"]
- Create a Docker image whose name is "my-flask-image:latest", using the
Dockerfile
$docker build -t my-flask-image:latest .
- Then see if your image is in Docker (take a screenshot)
$docker images
- Run your image (take a screenshot)
$docker run -d -p 5000:5000 my-flask-image
$docker ps
- Access the application from a browser ([Link]
2. Automatically dockerize a Flask project using Jenkins
FOSS_Development (CT207 - CT213H) - College of ICT - Can Tho University
2.1. Push your code to a Github repository
- Create an account (or login) to GitHub at [Link]
- Create a new repository, name it as "cicd_tutorial". Get the repository URL (for
example: [Link]
- Install and setup git on your computer (remember to set your name/email)
$sudo apt update ; sudo apt install git -y
$git config --global [Link] "Firstname Lastname"
$git config --global [Link] "example@.[Link]"
- Initialize git, commit and push your flask project files to Github
$mv ~/cicd_tutorial
$git init
$git add .
$git commit -m "first commit"
$git remote add origin <your repository URL>
$git push -u origin master
2.2. Install and configure Jenkins
- Install Java and Jenkins
FOSS_Development (CT207 - CT213H) - College of ICT - Can Tho University
$sudo apt install openjdk-11-jdk –y
$wget -q -O - [Link]
stable/[Link] | sudo apt-key add -
$sudo sh -c 'echo deb [Link]
binary/ > /etc/apt/[Link].d/[Link]'
$sudo apt update ; sudo apt install jenkins -y
- Launch Jenkins
$sudo usermod -aG docker jenkins
$sudo systemctl restart [Link]
- Access Jenkins using a web browser ([Link] Unlock Jenkins,
install suggested plugins, create the first admin user.
2.3. Using Jenkins to automatically dockerize your application
- On Jenkins dashboard, cick "Create a new job", then choose "Freestyle project".
Name your project as "my_flask_project"
- Under "Source Code Management" choose "Git", fill in your GitHub repository
URL
- Under "Build Triggers" select "Build periodically", fill in "* * * * *" (build your project
every minute)
*****
FOSS_Development (CT207 - CT213H) - College of ICT - Can Tho University
- Under "Build" we will "Add build step", and select "Execute shell". Then fill in
"docker build -t my-flask-image:latest ."
docker build -t my-flask-image:latest .
- Save your project. Then look at "Build history" to see that your project is built
every minute.
- Then see if your image is in Docker (take a screenshot)
$docker images
- Modify your Flask application:
$nano flask_docker.py
FOSS_Development (CT207 - CT213H) - College of ICT - Can Tho University
from flask import Flask
app = Flask(__name__)
@[Link]('/')
def hello_world():
return 'Hello FOSS, Hello CI/CD using Jenkins'
if __name__ == '__main__':
[Link](debug=True,host='[Link]')
- Commit and push your project files to GitHub
$git add .
$git commit -m "second commit"
$git push origin master
- Wait 1 minute, then run your image
$docker run -d -p 5000:5000 my-flask-image
$docker ps
FOSS_Development (CT207 - CT213H) - College of ICT - Can Tho University
- Access the application from a browser ([Link] (take a screenshot)
- On your Jenkins project configure, under "Build Triggers", do not forget to
deselect "Build periodically"
---END---