We are going to build an Docker image and push the image to Dockerhub using Jenkins pipeline.
We have already installed the Jenkins and done the required settings.
- step 1: Add your user to the docker group This command adds your current user to the docker group, which grants permission to access the Docker socket
sudo usermod -aG docker jenkins
- step 2: Now restart the Jenkins using
sudo systemctl restart jenkins
step 3:
Install docker related plugins in jenkins
docker
docker commons
docker API
docker pipeline
docker-build-step
CloudBees Docker Build and Publishstep 4:
Login to Dockerhub and create a personal access token.
- step 5: Add the created personal access token to jenkins credentials
- step 6: Created a Dockerfile on the Github project Repository
kannan@kannan-PC:~/dockerhub_jenkins$ vi Dockerfile
kannan@kannan-PC:~/dockerhub_jenkins$ cat Dockerfile
FROM ubuntu:22.04
ENV DEBIAN_FRONTEND=noninteractive
RUN apt update
RUN apt install apache2 -y
RUN apt install apache2-utils -y
RUN apt clean
COPY index.html /var/www/html
EXPOSE 80
CMD ["apache2ctl","-D","FOREGROUND"]
kannan@kannan-PC:~/dockerhub_jenkins$ vi index.html
kannan@kannan-PC:~/dockerhub_jenkins$ cat index.html
<h1>Image created and push to Dockerhub</h1>
- step 7: Create a jenkinsfile and add to the Gitghub project Repository
kannan@kannan-PC:~/dockerhub_jenkins$ vim Jenkinsfile
kannan@kannan-PC:~/dockerhub_jenkins$ cat Jenkinsfile
pipeline {
agent any
options {
buildDiscarder(logRotator(numToKeepStr: '5'))
}
environment {
DOCKERHUB_CREDENTIALS = credentials('kannanb95-dockerhub')
}
stages {
stage('Build') {
steps {
sh 'docker build -t kannanb95/kaniyam:latest .'
}
}
stage('Login') {
steps {
sh 'echo $DOCKERHUB_CREDENTIALS_PSW | docker login -u $DOCKERHUB_CREDENTIALS_USR --password-stdin'
}
}
stage('Push') {
steps {
sh 'docker push kannanb95/kaniyam:latest'
}
}
}
post {
always {
sh 'docker logout'
}
}
}
kannan@kannan-PC:~/dockerhub_jenkins$ ls
Dockerfile index.html Jenkinsfile README.md
Now add,commit and push to the git main
kannan@kannan-PC:~/dockerhub_jenkins$ git add .
kannan@kannan-PC:~/dockerhub_jenkins$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: Dockerfile
new file: Jenkinsfile
new file: index.html
kannan@kannan-PC:~/dockerhub_jenkins$ git commit -m "commit dockerhub_jenkins"
[main 5b006c5] commit dockerhub_jenkins
3 files changed, 41 insertions(+)
create mode 100644 Dockerfile
create mode 100644 Jenkinsfile
create mode 100644 index.html
kannan@kannan-PC:~/dockerhub_jenkins$ git push origin main
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 8 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 884 bytes | 884.00 KiB/s, done.
Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:kannanb95/dockerhub_jenkins.git
397a453..5b006c5 main -> main
- step 8: Configure the pipeline definition as "pipeline script from SCM" and add the "Github project Repository URL" and branch as "main"
Build the Jenkins pipeline
Successfully completed the build verify the build history "Console output"
Top comments (0)