SoFunction
Updated on 2025-04-08

Frequently Asked Questions and Solutions for Docker Image Construction and Pushing in Jenkins Pipeline

introduction

In modern DevOps practice, Jenkins is widely used in automated build, test and deployment processes as a popular continuous integration and continuous delivery (CI/CD) tool. Docker, as a lightweight containerization technology, can help development teams build, publish and run applications quickly. Using Jenkins with Docker can greatly improve the efficiency and quality of software delivery.

However, various problems may be encountered during the actual process of building and pushing Docker images using Jenkins Pipeline. This article will use a specific case to analyze the common problems of Docker image construction and push in Jenkins Pipeline in detail and provide solutions.

Case background

Here is a snippet of a Jenkins Pipeline script that is used to build and push Docker images to remote repositories:

stage('Build Docker'){
    steps{
        echo "Build Docker Image Stage"
        sh "docker build -t /advertise/${SERVER_NAME}:${build_tag} ."
    }
}

stage('Push'){
    steps{
        echo "4. Push Docker Image Stage"
        withCredentials([usernamePassword(credentialsId: 'tencentcloudcr', passwordVariable: 'PASSWORD', usernameVariable: 'USERNAME')]) {
            sh "docker login  --username ${USERNAME} --password ${PASSWORD} "
            sh "docker push /advertise/${SERVER_NAME}:${build_tag}"
            sh "docker rmi \$(docker images -q)"
        }
    }
}

The script is divided into two stages:

  • Build Docker: Build a Docker image.
  • Push: Log in to the remote Docker repository, push the image, and clean the local image.

In actual operation, the script may encounter the following problems:

Issue 1: Unable to connect to the Docker daemon

error message

ERROR: Cannot connect to the Docker daemon at unix:///var/run/. Is the docker daemon running?

Problem analysis

This error indicates that Jenkins cannot connect to the Docker daemon. Possible reasons include:

  1. The Docker service is not started.
  2. Jenkins users do not have permission to access Docker's Unix socket files/var/run/
  3. Jenkins runs in a container and does not mount the Docker socket.

Solution

  • Start Docker Service
sudo systemctl start docker
  • Grant Jenkins user permissions
sudo usermod -aG docker jenkins
sudo systemctl restart jenkins
  • Mount Docker socket(If Jenkins is running in a container):
docker run -v /var/run/:/var/run/ -p 8080:8080 jenkins/jenkins:lts

Problem 2: The image is occupied by the stopped container

error message

Error response from daemon: conflict: unable to delete 583207f3c64e (must be forced) - image is being used by stopped container 90eceb6d5a9c

Problem analysis

This error indicates that the image583207f3c64eBeing stopped container90eceb6d5a9cOccupancy, resulting in the inability to delete directly.

Solution

  • Delete containers that occupy images
docker rm -f 90eceb6d5a9c
  • Forced delete the mirror
docker rmi -f 583207f3c64e

Question 3: The mirror is referenced by multiple repositories

error message

Error response from daemon: conflict: unable to delete 27b858cdcd8a (must be forced) - image is referenced in multiple repositories

Problem analysis

This error indicates that the image27b858cdcd8aReferenced by multiple repositories, resulting in the inability to delete directly.

Solution

  • Forced delete the mirror
docker rmi -f 27b858cdcd8a
  • Clean up unused images
docker image prune -a -f

Issue 4: Docker login password security issue

error message

Warning: A secret was passed to "sh" using Groovy String interpolation, which is insecure.
WARNING! Using --password via the CLI is insecure. Use --password-stdin.

Problem analysis

This warning indicates that it is not safe to pass passwords directly using Groovy string interpolation in Jenkins Pipeline.

Solution

use--password-stdinPass the password:

echo ${PASSWORD} | docker login --username ${USERNAME} --password-stdin 

Optimized Jenkins Pipeline scripts

Combining the above problems and solutions, here is the optimized Jenkins Pipeline script:

stage('Build Docker'){
    steps{
        echo "Build Docker Image Stage"
        sh "docker build -t /advertise/${SERVER_NAME}:${build_tag} ."
    }
}

stage('Push'){
    steps{
        echo "4. Push Docker Image Stage"
        withCredentials([usernamePassword(credentialsId: 'tencentcloudcr', passwordVariable: 'PASSWORD', usernameVariable: 'USERNAME')]) {
            sh "echo ${PASSWORD} | docker login --username ${USERNAME} --password-stdin "
            sh "docker push /advertise/${SERVER_NAME}:${build_tag}"
            sh "docker rm -f \$(docker ps -aq) || true"  # Force delete all containers            sh "docker rmi -f \$(docker images -q) || true"  # Force delete all images        }
    }
}

Summarize

When building and pushing Docker images in Jenkins Pipeline, you may encounter various problems such as not being able to connect to the Docker daemon, image occupied or referenced, password security issues, etc. By analyzing the root cause of the problem and adopting corresponding solutions (such as starting Docker service, granting permissions, forcibly deleting images, optimizing password delivery methods, etc.), Pipeline can be ensured to be successfully executed.

Additionally, optimizing Jenkins Pipeline scripts and adding error handling logic (such as || true) can avoid failures of the entire Pipeline due to failures in the cleanup step. Through these practices, development teams can more efficiently leverage Jenkins and Docker to achieve the goals of continuous integration and continuous delivery.

I hope that the content of this article can help readers better understand and solve common problems in Docker image construction and push in Jenkins Pipeline. If you have other questions or suggestions, please leave a message to discuss!

The above is the detailed content of common problems and solutions for Docker image construction and push in Jenkins Pipeline. For more information about Jenkins Pipeline Docker image construction and push, please follow my other related articles!