Monday, February 22, 2021

Automate Docker builds using Jenkins Pipelines | Dockerize PHP App | Upload Docker Image from Jenkins into Nexus Docker Registry

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 docker image into Nexus Docker registry successfully. 

- Automating builds
- Automating Docker image builds
- Automating Docker image upload into Nexus docker registry
- Automating Docker container provisioning
Watch here for YouTube channel:

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

Create an entry in Manage Credentials for connecting to Nexus
Go to Jenkins --> Manage Jenkins--> Click on Manage Credentials.

Enter Nexus user name and password with ID as nexus
Click on Save.

Step # 1 - Create a pipeline in Jenkins, name can be anything

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 = ""
        dockerImage = ''
    stages {
        stage('Code checkout') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '', url: '']]])                   }
    // Building Docker images
    stage('Building image') {
        script {
          dockerImage = imageName

    // Uploading Docker images into Nexus Registry
    stage('Uploading to Nexus') {
         script {
             docker.withRegistry( 'http://'+registry, registryCredentials ) {
    // 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') {
         script {
                sh 'docker run -d -p 80:80 --rm --name myphpcontainer ' + registry + imageName

Step # 3 - Click on Build - Build the pipeline
Once you create the pipeline, click on Build now.

Steps # 4 - Check Docker images are uploaded into Nexus Registry
Login to Nexus, click on your repo, now you should see the image got uploaded.

Steps # 5 - Access PHP App in the browser which is running inside docker container
Once build is successful, go to browser and enter http://public_dns_name
You should see page like below:


  1. Hi, I tired following your steps in the same order but In the last step i.e. "Upload into Nexus" it is throwing an error saying "$ docker login -u admin -p ********
    WARNING! Using --password via the CLI is insecure. Use --password-stdin.
    Error response from daemon: Get "": http: server gave HTTP response to HTTPS client".

    Kindly help!!

    1. Try this


DevOps Bootcamp Aug 2024 Schedule | DevOps & AWS Azure Cloud Coaching by Coach AK | DevOps and Cloud Computing Online Classes

  (Lot of new topics covered like GitHub Actions, Helm and Monitoring..) The DevOps requirements in the IT market space is expected to grow ...