0
0
JenkinsHow-ToBeginner · 4 min read

How to Deploy Docker Containers Using Jenkins CI/CD

To deploy Docker using Jenkins, create a Jenkins pipeline that builds your Docker image, pushes it to a registry, and then deploys the container on your server. Use docker commands inside Jenkins pipeline steps to automate the build and deployment process.
📐

Syntax

A typical Jenkins pipeline for Docker deployment includes these steps:

  • Checkout code: Get your application source from version control.
  • Build Docker image: Use docker build to create an image.
  • Push image: Push the image to a Docker registry like Docker Hub.
  • Deploy container: Run the Docker container on your target server.

Each step is a stage in the Jenkinsfile pipeline script.

groovy
pipeline {
  agent any
  stages {
    stage('Checkout') {
      steps {
        checkout scm
      }
    }
    stage('Build Docker Image') {
      steps {
        script {
          dockerImage = docker.build("myapp:${env.BUILD_ID}")
        }
      }
    }
    stage('Push Image') {
      steps {
        script {
          docker.withRegistry('https://registry.hub.docker.com', 'dockerhub-credentials') {
            dockerImage.push()
          }
        }
      }
    }
    stage('Deploy Container') {
      steps {
        sh "docker run -d -p 8080:8080 myapp:${env.BUILD_ID}"
      }
    }
  }
}
💻

Example

This example Jenkinsfile builds a Docker image from your app, pushes it to Docker Hub, and runs it on the Jenkins agent machine.

groovy
pipeline {
  agent any
  environment {
    DOCKER_IMAGE = "yourdockerhubusername/myapp"
  }
  stages {
    stage('Checkout') {
      steps {
        checkout scm
      }
    }
    stage('Build Image') {
      steps {
        script {
          dockerImage = docker.build("${DOCKER_IMAGE}:${env.BUILD_ID}")
        }
      }
    }
    stage('Push Image') {
      steps {
        script {
          docker.withRegistry('https://registry.hub.docker.com', 'dockerhub-credentials') {
            dockerImage.push()
          }
        }
      }
    }
    stage('Deploy') {
      steps {
        sh "docker stop myapp || true"
        sh "docker rm myapp || true"
        sh "docker run -d --name myapp -p 8080:8080 ${DOCKER_IMAGE}:${env.BUILD_ID}"
      }
    }
  }
}
Output
Sending build context to Docker daemon 8.192kB Step 1/5 : FROM openjdk:11-jre-slim ---> 123abc456def ... Successfully built abcdef123456 Successfully tagged yourdockerhubusername/myapp:25 The push refers to repository [docker.io/yourdockerhubusername/myapp] ... latest: digest: sha256:abcdef123456 size: 1234 myapp
⚠️

Common Pitfalls

Common mistakes when deploying Docker with Jenkins include:

  • Not configuring Docker credentials in Jenkins, causing push failures.
  • Running Docker commands without proper permissions on the Jenkins agent.
  • Forgetting to stop and remove old containers before deploying new ones, leading to port conflicts.
  • Using latest tag instead of unique tags, causing deployment confusion.

Always use unique tags and manage credentials securely.

groovy
/* Wrong: No credentials and no container cleanup */
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        sh 'docker build -t myapp .'
      }
    }
    stage('Push') {
      steps {
        sh 'docker push myapp'
      }
    }
    stage('Deploy') {
      steps {
        sh 'docker run -d -p 8080:8080 myapp'
      }
    }
  }
}

/* Right: Use credentials and stop old container */
pipeline {
  agent any
  stages {
    stage('Push') {
      steps {
        script {
          docker.withRegistry('https://registry.hub.docker.com', 'dockerhub-credentials') {
            dockerImage.push()
          }
        }
      }
    }
    stage('Deploy') {
      steps {
        sh 'docker stop myapp || true'
        sh 'docker rm myapp || true'
        sh 'docker run -d --name myapp -p 8080:8080 myapp'
      }
    }
  }
}
📊

Quick Reference

Tips for deploying Docker with Jenkins:

  • Use Jenkins credentials to store Docker registry login securely.
  • Tag images uniquely using build numbers or commit hashes.
  • Stop and remove old containers before running new ones to avoid conflicts.
  • Run Jenkins agents with Docker permissions or use Docker-in-Docker setup.
  • Test Docker commands manually on the Jenkins agent before automating.

Key Takeaways

Use Jenkins pipeline stages to build, push, and deploy Docker images automatically.
Store Docker registry credentials securely in Jenkins and use them in your pipeline.
Always tag Docker images uniquely to avoid deployment confusion.
Stop and remove old containers before deploying new ones to prevent port conflicts.
Ensure Jenkins agents have proper Docker permissions to run commands successfully.