Bird
Raised Fist0
Jenkinsdevops~7 mins

Docker Pipeline plugin in Jenkins - Commands & Configuration

Choose your learning style10 modes available
Introduction
Building and running Docker containers inside Jenkins pipelines can be complex. The Docker Pipeline plugin simplifies this by providing easy commands to build, run, and manage Docker images and containers directly in your Jenkins pipeline scripts.
When you want to build a Docker image as part of your Jenkins build process.
When you need to run tests inside a Docker container during a Jenkins pipeline.
When you want to push a Docker image to a registry automatically after a successful build.
When you want to clean up Docker containers and images after pipeline execution to save space.
When you want to use Docker containers as build agents inside Jenkins pipelines.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Build Docker Image') {
      steps {
        script {
          docker.build('my-app-image:1.0')
        }
      }
    }
    stage('Run Container') {
      steps {
        script {
          docker.image('my-app-image:1.0').inside {
            sh 'echo Running inside Docker container'
          }
        }
      }
    }
    stage('Push Image') {
      steps {
        script {
          docker.withRegistry('https://registry.hub.docker.com', 'docker-hub-credentials') {
            docker.image('my-app-image:1.0').push()
          }
        }
      }
    }
  }
}

This Jenkinsfile defines a pipeline with three stages:

  • Build Docker Image: Uses docker.build to create a Docker image named my-app-image:1.0.
  • Run Container: Runs commands inside a container created from the built image using docker.image(...).inside.
  • Push Image: Pushes the Docker image to Docker Hub using docker.withRegistry with stored credentials.
Commands
This command builds a Docker image named 'my-app-image' with tag '1.0' from the current directory. It is the manual equivalent of the Jenkins docker.build step.
Terminal
docker build -t my-app-image:1.0 .
Expected OutputExpected
Sending build context to Docker daemon 5.12kB Step 1/3 : FROM alpine:3.14 ---> a24bb4013296 Step 2/3 : RUN echo Hello from Dockerfile ---> Running in 123abc456def Removing intermediate container 123abc456def ---> 789xyz123abc Step 3/3 : CMD ["echo", "Hello World"] ---> Running in 456def789xyz Removing intermediate container 456def789xyz Successfully built 789xyz123abc Successfully tagged my-app-image:1.0
→
-t - Tags the image with a name and version
Runs a container from the 'my-app-image:1.0' image and removes it after it exits. This simulates the Jenkins docker.image(...).inside step.
Terminal
docker run --rm my-app-image:1.0
Expected OutputExpected
Hello World
→
--rm - Automatically removes the container after it exits
Logs into Docker Hub so you can push images. In Jenkins, this is handled by docker.withRegistry with stored credentials.
Terminal
docker login -u myusername -p mypassword
Expected OutputExpected
Login Succeeded
Pushes the 'my-app-image:1.0' image to Docker Hub or another registry.
Terminal
docker push my-app-image:1.0
Expected OutputExpected
The push refers to repository [docker.io/library/my-app-image] 1.0: Pushed
Key Concept

If you remember nothing else from this pattern, remember: the Docker Pipeline plugin lets you build, run, and push Docker images inside Jenkins pipelines with simple commands that wrap Docker CLI operations.

Common Mistakes
Not configuring Docker credentials in Jenkins before pushing images.
Without credentials, the push command will fail with authentication errors.
Store Docker registry credentials in Jenkins and reference them in docker.withRegistry.
Using docker.build without specifying a tag.
The image will be built with the default 'latest' tag, which can cause confusion or overwrite existing images.
Always specify a meaningful tag like 'my-app-image:1.0' to track versions.
Running docker.image(...).inside without ensuring the Docker daemon is accessible to Jenkins agents.
The container will fail to start if Jenkins cannot communicate with Docker.
Make sure Jenkins agents have Docker installed and configured properly.
Summary
Use docker.build in Jenkinsfile to build Docker images during pipeline runs.
Use docker.image(...).inside to run commands inside containers from those images.
Use docker.withRegistry with stored credentials to push images to Docker registries.

Practice

(1/5)
1. What is the primary purpose of the docker.build() method in Jenkins Docker Pipeline plugin?
easy
A. To remove unused Docker images
B. To run a Docker container interactively
C. To build a Docker image from a Dockerfile
D. To push a Docker image to a registry

Solution

  1. Step 1: Understand the role of docker.build()

    This method is used to create a Docker image from a Dockerfile in the workspace.
  2. Step 2: Differentiate from other Docker Pipeline methods

    Running containers uses docker.image().inside { }, pushing uses docker.withRegistry(), and removing images is not handled by docker.build().
  3. Final Answer:

    To build a Docker image from a Dockerfile -> Option C
  4. Quick Check:

    docker.build() builds images [OK]
Hint: Remember: build = create image from Dockerfile [OK]
Common Mistakes:
  • Confusing build with push or run commands
  • Thinking docker.build() runs containers
  • Assuming it handles image removal
2. Which of the following is the correct syntax to run commands inside a Docker container in a Jenkins pipeline?
easy
A. docker.image('myimage').inside { sh 'echo Hello' }
B. docker.build('myimage') { sh 'echo Hello' }
C. docker.run('myimage') { sh 'echo Hello' }
D. docker.container('myimage').exec { sh 'echo Hello' }

Solution

  1. Step 1: Identify the correct method to run commands inside a container

    The docker.image('myimage').inside { } syntax runs commands inside a container created from the image.
  2. Step 2: Check other options for syntax errors or misuse

    docker.run() and docker.container() are not valid Jenkins Pipeline methods; docker.build() builds images but does not run containers.
  3. Final Answer:

    docker.image('myimage').inside { sh 'echo Hello' } -> Option A
  4. Quick Check:

    Use docker.image().inside { } to run commands [OK]
Hint: Use docker.image().inside { } to run commands inside containers [OK]
Common Mistakes:
  • Using docker.build() to run commands
  • Trying to use non-existent methods like docker.run()
  • Confusing image build and container run syntax
3. Given the following Jenkins pipeline snippet, what will be the output of the sh command inside the container?
def img = docker.build('myapp:latest')
img.inside {
  sh 'echo Hello from Docker'
}
medium
A. Error: No such image 'myapp:latest'
B. Hello from Docker
C. Build logs of the Docker image
D. Nothing, the command is not executed

Solution

  1. Step 1: Understand the build and run sequence

    The docker.build('myapp:latest') creates the image, then img.inside { } runs commands inside a container from that image.
  2. Step 2: Analyze the sh command inside the container

    The shell command echo Hello from Docker runs inside the container and outputs the string.
  3. Final Answer:

    Hello from Docker -> Option B
  4. Quick Check:

    Commands inside img.inside { } run and output normally [OK]
Hint: Commands inside img.inside { } run inside container [OK]
Common Mistakes:
  • Assuming build only creates logs, not image
  • Thinking the command runs outside the container
  • Expecting an error if image was just built
4. Identify the error in this Jenkins pipeline snippet using Docker Pipeline plugin:
docker.withRegistry('https://myregistry.com', 'my-credentials') {
  def img = docker.build('myapp')
  img.push()
}
img.inside {
  sh 'echo Running inside container'
}
medium
A. The img.push() should be called before docker.withRegistry
B. The docker.build method is missing the Dockerfile path
C. The credentials ID 'my-credentials' is invalid syntax
D. The img.inside block is outside the docker.withRegistry block and will fail

Solution

  1. Step 1: Check scope of img variable

    The variable img is defined inside the docker.withRegistry block, so it is not accessible outside that block.
  2. Step 2: Identify the error with img.inside usage

    Using img.inside outside the block where img is defined causes a runtime error because img is undefined there.
  3. Final Answer:

    The img.inside block is outside the docker.withRegistry block and will fail -> Option D
  4. Quick Check:

    Variable scope matters in Jenkins pipeline blocks [OK]
Hint: Keep variable usage inside the block where it's defined [OK]
Common Mistakes:
  • Assuming variables are global across blocks
  • Confusing credentials ID syntax
  • Thinking push must be outside withRegistry
5. You want to build a Docker image, run tests inside a container from that image, and then push the image to a private registry using Jenkins Docker Pipeline plugin. Which pipeline snippet correctly implements this workflow?
hard
A. def img = docker.build('myapp:test') img.inside { sh 'run-tests.sh' } docker.withRegistry('https://registry.example.com', 'creds') { img.push() }
B. docker.withRegistry('https://registry.example.com', 'creds') { def img = docker.build('myapp:test') img.push() img.inside { sh 'run-tests.sh' } }
C. docker.withRegistry('https://registry.example.com', 'creds') { img.push() def img = docker.build('myapp:test') img.inside { sh 'run-tests.sh' } }
D. def img = docker.build('myapp:test') docker.withRegistry('https://registry.example.com', 'creds') { img.push() } img.inside { sh 'run-tests.sh' }

Solution

  1. Step 1: Build image and run tests inside container first

    The image must be built before running tests inside it, so docker.build() and img.inside { } come first.
  2. Step 2: Push image inside docker.withRegistry() block after tests

    Pushing requires registry credentials, so img.push() must be inside docker.withRegistry() block after tests pass.
  3. Step 3: Verify variable scope and order

    def img = docker.build('myapp:test') img.inside { sh 'run-tests.sh' } docker.withRegistry('https://registry.example.com', 'creds') { img.push() } correctly builds image, runs tests inside container, then pushes image with credentials in proper order and scope.
  4. Final Answer:

    def img = docker.build('myapp:test') img.inside { sh 'run-tests.sh' } docker.withRegistry('https://registry.example.com', 'creds') { img.push() } -> Option A
  5. Quick Check:

    Build -> Test inside container -> Push with credentials [OK]
Hint: Build first, test inside container, then push inside withRegistry [OK]
Common Mistakes:
  • Pushing image before running tests
  • Using img.inside after pushing outside scope
  • Defining variables inside wrong blocks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard