Create a Git Repository named "dockerhub_jenkins" with Readme.md
Clone the repo to local machine and add the below required files
Create Dockerfile and image to run
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"]
- Create Jenkins file
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'
}
}
}
Do the Git commit and push to the repo.
Login to Dockerhub >Account settings >Security >Access Token > Create New Access Token.
- Go to Jenkins dashboard > Manage Jenkins >Credentials > systems > add docker hub credentials > at password tab paste the access token created on Docker hub.
At jenkins dashboard > Add items > named as "Dockerhub_jenkins" > Pipeline.
Pipeline > Definition > Pipeline script from SCM > set SCM as "Git"> Copy the Repo "URL" >Set branch as "Main" > save and apply > Build Now.
- Verify the Dockerhub
Top comments (0)