It’s not your Dockerfile that’s broken, it’s the environment it’s running in.

Here’s a Jenkins pipeline failing during a docker build step, and what’s actually going wrong.

pipeline {
    agent any
    stages {
        stage('Build Docker Image') {
            steps {
                script {
                    sh 'docker build -t myapp:latest .'
                }
            }
        }
    }
}

Common Causes for docker build Failures in Jenkins

1. Docker Daemon Not Running or Unreachable

  • Diagnosis: On the Jenkins agent node (where the docker build command is executed), run sudo systemctl status docker.
  • Fix: If it’s not active, start it with sudo systemctl start docker and enable it to start on boot with sudo systemctl enable docker.
  • Why it works: The docker build command is a client that talks to the Docker daemon. If the daemon isn’t running, there’s no server to process the build instructions, leading to connection errors.

2. Insufficient Disk Space on the Agent Node

  • Diagnosis: Run df -h on the Jenkins agent node. Look for the filesystem where Docker stores its images and build cache (often /var/lib/docker). If it’s over 90% full, you’ve found your culprit.
  • Fix:
    • Prune unused Docker objects: docker system prune -a --volumes. This removes stopped containers, dangling images, unused networks, and build cache.
    • If pruning isn’t enough, you’ll need to free up space on the underlying filesystem by deleting other large files or moving Docker’s data directory to a larger partition (requires stopping Docker and reconfiguring its daemon).
  • Why it works: Docker images and their layers can consume significant disk space. A full disk prevents new layers from being written during the build process.

3. Incorrect Dockerfile Syntax or Missing Build Context

  • Diagnosis: Carefully examine the docker build output for specific syntax errors reported by Docker (e.g., "invalid instruction," "unknown flag"). Also, ensure the . at the end of docker build -t myapp:latest . correctly points to the directory containing your Dockerfile and any files needed for the build.
  • Fix:
    • Correct the syntax in your Dockerfile. For example, ensure RUN commands are properly formatted and arguments are quoted if necessary.
    • If the error is related to missing files (e.g., COPY failed: file not found), ensure the file is present in the directory specified by the build context (.) and that your Dockerfile is referencing it correctly.
  • Why it works: The docker build command interprets instructions from the Dockerfile. Any deviation from the expected syntax or a failure to find files it needs to COPY or ADD will halt the build.

4. Network Issues or Firewall Blocking Access

  • Diagnosis: If your Dockerfile uses RUN commands to apt-get update, yum install, or curl external resources, test connectivity from the Jenkins agent node. Run curl https://registry-1.docker.io/v2/ or ping google.com. If these fail, check firewalls.
  • Fix: Ensure that the Jenkins agent node has outbound access to Docker Hub (or your private registry) and any other external resources required by your Dockerfile. This might involve configuring ufw, firewalld, or cloud provider security groups to allow traffic on ports 443 (HTTPS) and 80 (HTTP).
  • Why it works: Docker downloads base images and packages specified in RUN instructions. Network restrictions prevent these downloads, causing the build to fail.

5. Insufficient Memory or CPU on the Jenkins Agent

  • Diagnosis: Monitor the agent node’s resource usage during the build using tools like top or htop. If you see high CPU utilization (consistently near 100%) or the system is swapping heavily (indicated by high swpd or swap usage in top), the agent is likely resource-starved.
  • Fix:
    • Allocate more RAM and CPU to the Jenkins agent VM or container.
    • Consider using a more powerful agent node.
    • Optimize your Dockerfile to reduce resource consumption (e.g., multi-stage builds to keep final images smaller, chaining RUN commands to reduce layers).
  • Why it works: Docker builds, especially complex ones involving compilation or large package installations, can be resource-intensive. Insufficient memory can lead to the build process being killed by the OS (OOM killer), and insufficient CPU can cause it to take an excessively long time, potentially hitting Jenkins timeouts.

6. Docker Daemon Configuration Issues (e.g., Storage Driver)

  • Diagnosis: Check the Docker daemon logs for errors related to storage drivers. On systemd-based systems, this is typically sudo journalctl -u docker.service. Look for messages about incompatible storage drivers or errors during image layer operations.
  • Fix: Ensure you’re using a supported and appropriate storage driver for your OS. The default is usually fine, but if it’s been manually changed, it might be misconfigured. Sometimes, simply restarting the Docker daemon (sudo systemctl restart docker) can resolve transient issues with the storage driver. In more severe cases, you might need to reconfigure Docker’s daemon.json (e.g., /etc/docker/daemon.json) to specify a different storage driver, but this is an advanced step.
  • Why it works: The storage driver is responsible for managing image layers and container filesystems. A misconfigured or incompatible driver can lead to fundamental failures in reading or writing image data during the build.

7. Jenkins User Lacks Permissions to Access Docker Socket

  • Diagnosis: If the docker build command itself fails with a permission denied error related to /var/run/docker.sock, this is the issue. Run ls -l /var/run/docker.sock to see its ownership and permissions.
  • Fix: Add the jenkins user to the docker group: sudo usermod -aG docker jenkins. You will need to restart the Jenkins service or restart the agent process for this change to take effect.
  • Why it works: The docker command-line client communicates with the Docker daemon via a Unix socket (/var/run/docker.sock). The jenkins user needs to be part of the docker group to have the necessary read/write permissions on this socket.

You’ll likely hit "unknown flag: --no-cache" next if you try to add a flag without checking the Docker version supported by the daemon.

Want structured learning?

Take the full Jenkins course →