0
0
DockerHow-ToBeginner · 4 min read

How to Use Docker in Jenkins: Simple Guide for Beginners

To use Docker in Jenkins, install the Docker plugin or run Jenkins agents with Docker installed, then configure your pipeline to execute Docker commands using sh steps. This allows Jenkins to build, run, and manage Docker containers as part of your CI/CD process.
📐

Syntax

In Jenkins pipelines, Docker commands are run inside sh steps for Linux agents or bat steps for Windows agents. You can also use the Jenkins Docker plugin to simplify Docker container management.

Basic syntax to run Docker commands in a pipeline:

  • sh 'docker build -t image-name .' - builds a Docker image
  • sh 'docker run image-name' - runs a Docker container
  • docker.withRegistry() - Jenkins pipeline step to interact with Docker registries
groovy
pipeline {
  agent any
  stages {
    stage('Build Docker Image') {
      steps {
        sh 'docker build -t my-image:latest .'
      }
    }
    stage('Run Docker Container') {
      steps {
        sh 'docker run --rm my-image:latest'
      }
    }
  }
}
💻

Example

This example Jenkins pipeline builds a Docker image from a Dockerfile in the workspace and then runs a container from that image. It shows how to use sh steps to execute Docker commands.

groovy
pipeline {
  agent any
  stages {
    stage('Build Image') {
      steps {
        sh 'docker build -t hello-world:latest .'
      }
    }
    stage('Run Container') {
      steps {
        sh 'docker run --rm hello-world:latest'
      }
    }
  }
}
Output
Sending build context to Docker daemon 2.048kB Step 1/2 : FROM alpine ---> a24bb4013296 Step 2/2 : CMD ["echo", "Hello from Docker in Jenkins!"] ---> Running in 3c1f9e5a7f3a Removing intermediate container 3c1f9e5a7f3a Successfully built 7f3a9d2c1b4e Hello from Docker in Jenkins!
⚠️

Common Pitfalls

Common mistakes when using Docker in Jenkins include:

  • Running Jenkins on an agent without Docker installed or without proper permissions.
  • Not adding the Jenkins user to the Docker group, causing permission denied errors.
  • Using docker commands without proper environment setup in pipeline scripts.
  • Forgetting to clean up Docker images and containers, leading to disk space issues.

Always ensure Docker is installed and accessible by Jenkins, and use docker ps or docker info in a test step to verify.

groovy
pipeline {
  agent any
  stages {
    stage('Test Docker Access') {
      steps {
        sh 'docker info'
      }
    }
  }
}
Output
Client: Debug Mode: false Server: Containers: 3 Running: 1 Paused: 0 Stopped: 2 Images: 5 Server Version: 20.10.7 ...
📊

Quick Reference

Tips for using Docker in Jenkins:

  • Install Docker on Jenkins agents or use Docker-in-Docker containers.
  • Add Jenkins user to the Docker group to avoid permission issues.
  • Use sh steps to run Docker CLI commands in pipelines.
  • Use Jenkins Docker plugin for advanced container management.
  • Clean up unused images and containers regularly.

Key Takeaways

Ensure Docker is installed and accessible on Jenkins agents before running Docker commands.
Use sh steps in Jenkins pipelines to execute Docker CLI commands.
Add Jenkins user to the Docker group to prevent permission denied errors.
Test Docker access with simple commands like docker info in your pipeline.
Clean up Docker resources regularly to avoid disk space issues.