We will learn how to automate Docker builds using Jenkins pipeline. We will use PHP based application. I have already created a repo with source code + Dockerfile. We will see how to create Docker image and upload into Nexus Docker registry successfully.
- Automating Docker image builds
- Automating Docker image upload into Nexus docker registry
1. Jenkins is up and running
2. Docker installed on Jenkins instance. Click here to for integrating Docker and Jenkins
3. Docker and Docker pipelines plug-in are installed
4. Nexus is up and running and docker registry is configured. Click here to know how to do that.
5. port 80 is opened up in firewall rules to access phpApp running inside Docker container
Step # 2 - Copy the pipeline code from below
Make sure you change red highlighted values below:
Your account_d should be updated and repo should be updated.
pipeline {
agent any
environment {
imageName = "myphpapp"
registryCredentials = "nexus"
registry = "ec2-13-58-223-172.us-east-2.compute.amazonaws.com:8085/"
dockerImage = ''
}
stages {
stage('Code checkout') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '', url: 'https://bitbucket.org/ananthkannan/phprepo/']]]) }
}
// Building Docker images
stage('Building image') {
steps{
script {
dockerImage = docker.build imageName
}
}
}
// Uploading Docker images into Nexus Registry
stage('Uploading to Nexus') {
steps{
script {
docker.withRegistry( 'http://'+registry, registryCredentials ) {
dockerImage.push('latest')
}
}
}
}
// Stopping Docker containers for cleaner Docker run
stage('stop previous containers') {
steps {
sh 'docker ps -f name=myphpcontainer -q | xargs --no-run-if-empty docker container stop'
sh 'docker container ls -a -fname=myphpcontainer -q | xargs -r docker container rm'
}
}
stage('Docker Run') {
steps{
script {
sh 'docker run -d -p 80:80 --rm --name myphpcontainer ' + registry + imageName
}
}
}
}
}







































