Skip to main content

🔧 Fixing “Permission Denied While Trying to Connect to the Docker Daemon Socket” in Jenkins


🔧 Fixing “Permission Denied While Trying to Connect to the Docker Daemon Socket” in Jenkins

Running Jenkins builds that involve Docker can sometimes lead to frustrating errors. One such common error is:

permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock

If you’ve stumbled upon this error during a Jenkins pipeline or freestyle job, don’t worry — you’re not alone. In this article, we’ll break down what this error means, why it happens, and how to fix it the right way.


🧠 What Does the Error Mean?

When Jenkins tries to run a Docker command (like docker build, docker run, or docker rm), it needs to communicate with the Docker daemon. On Linux systems, Docker exposes a special Unix socket file:

/var/run/docker.sock

This socket file controls access to Docker. Only users with permission to this file (typically, members of the docker group) can interact with Docker.

So, if Jenkins shows this error:

permission denied while trying to connect to the Docker daemon socket

…it means that the user Jenkins is running as doesn't have access to Docker.


🔍 Real-Life Example

Here’s a real error output from Jenkins:

+ docker rm --force c1
permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock:
Delete "http://%2Fvar%2Frun%2Fdocker.sock/v1.47/containers/c1?force=1": 
dial unix /var/run/docker.sock: connect: permission denied
Build step 'Execute shell' marked build as failure
Finished: FAILURE

In this case, Jenkins tried to remove a Docker container but couldn’t connect to Docker due to a lack of permissions.


🛠️ How to Fix It

✅ Option 1: Add Jenkins to the Docker Group (Recommended)

  1. Check what user Jenkins is running as:
    Run:

    ps aux | grep jenkins
    

    Typically, it’s just the jenkins user.

  2. Add the Jenkins user to the docker group:

    sudo usermod -aG docker jenkins
    
  3. Restart Jenkins and Docker:

    sudo systemctl restart jenkins
    sudo systemctl restart docker
    
  4. Log out and log back in (or reboot) to apply group changes.

📝 Note: You might also need to restart your machine or at least re-login for the group membership to fully apply.


✅ Option 2: Use sudo with Docker (Less Secure)

If you're in a controlled environment (like a personal test server), and you’re okay with some security tradeoffs, you can run Docker commands with sudo in your Jenkins shell build steps:

  1. Edit your Jenkins job’s shell script:

    sudo docker rm --force c1
    
  2. Allow the Jenkins user to run Docker without a password:
    Open the sudoers file:

    sudo visudo
    

    Add this line:

    jenkins ALL=(ALL) NOPASSWD: /usr/bin/docker
    

⚠️ Be careful: This can be dangerous in production environments. It gives Jenkins the power to run Docker commands without authentication.


🚫 Option 3: Don’t Run Jenkins as Root

Sometimes people try to fix this by running Jenkins as root. This is not recommended and should be avoided unless absolutely necessary (e.g., in temporary test setups).


✅ Test It Out

Once you've applied one of the fixes:

  • Restart Jenkins

  • Re-run the failing job

  • You should now see Docker commands run without permission issues


🧩 Bonus Tip: Verify Docker Access from Terminal

Before rerunning Jenkins, test Docker manually from the Jenkins user’s context:

sudo su - jenkins
docker ps

If this works without an error, Jenkins should be able to access Docker too.


🎯 Conclusion

This “permission denied” error when connecting to Docker from Jenkins is usually a simple permissions issue. The most reliable and secure solution is to add Jenkins to the docker group and restart your services. Once that's done, your Jenkins pipelines should be able to run Docker commands seamlessly.


💬 Have you faced this error in other CI tools? Let me know in the comments — and share how you fixed it!


Let me know if you want the article formatted for WordPress, Medium, or another platform.



Comments

Popular posts from this blog

Example of Maven project that interacts with a MySQL database and includes testing

Example Maven project that interacts with a MySQL database and includes testing To install Java, MySQL, Maven, and write a Java program to fetch table data, execute, and create a JAR file using Maven on Ubuntu, you can follow these steps: Step 1: Install Java You can install Java using the following commands: sudo apt update sudo apt install default-jre sudo apt install default-jdk Verify the installation by running: java -version Step 2: Install MySQL You can install MySQL using the following commands: sudo apt update sudo apt install mysql-server During the installation, you'll be prompted to set a root password for MySQL or you can set password at latter stage using following steps.  sudo mysql ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; exit Step 3: Install Maven You can install Maven using the following commands: sudo apt update sudo apt install maven Verify the installation by running: mvn -version Step 4: Create ...

DevOps : Lab Manual (Complete)

  Lab Manual  Subject: DevOps Prepared By : Antosh Mahadappa Dyade  INDEX List of Practical Sr. No. Experiment Title Page No. 1 Exploring Git Commands through Collaborative Coding. 2 Implement GitHub Operations 3 Implement GitLab Operations 4 Implement BitBucket Operations 5 Applying CI/CD Principles to Web Development Using Jenkins, Git, and Local HTTP Server 6 Exploring Containerization and Application Deployment with Docker 7 Applying CI/CD Principles to Web Development Using Jenkins, Git, using Docker Containers 8 Demonstrate Maven Build Life Cycle 9 Demonstrate Container Orchestration using Kubernets. 10 Create the GitHub Account to demonstrate CI/CD pipeline using Cloud Platform. 11 (Content Beyond Syllabus) Title: Demonstrating Infrastructure as Code (IaC) with Terraform                                   ...

Maven Create and Build Artifacts

In Maven, you can create and build artifacts using the package phase of the build lifecycle. The package phase is responsible for taking the compiled code and other project resources and packaging them into a distributable format, such as a JAR (Java Archive), WAR (Web Application Archive), or other custom formats. Here are the steps to create and build artifacts using Maven: Configure the Build Output: In your project's pom.xml file, you need to configure the output of the build. This includes specifying the type of artifact you want to create (e.g., JAR, WAR) and any additional resources to include. You do this in the <build> section of your pom.xml: <build>     <finalName>my-artifact</finalName> <!-- Name of the artifact without the extension -->     <plugins>         <!-- Plugin configurations for creating the artifact -->         <!-- For example, maven-jar-plugin or maven-war-p...