🔧 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)
-
Check what user Jenkins is running as:
Run:ps aux | grep jenkins
Typically, it’s just the
jenkins
user. -
Add the Jenkins user to the
docker
group:sudo usermod -aG docker jenkins
-
Restart Jenkins and Docker:
sudo systemctl restart jenkins sudo systemctl restart docker
-
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:
-
Edit your Jenkins job’s shell script:
sudo docker rm --force c1
-
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
Post a Comment