Encountering the error “dial unix /var/run/docker.sock: connect: permission denied” in a Jenkins declarative pipeline using Docker as the agent is a common challenge. This issue arises when the user or process attempting to connect to the Docker daemon lacks the necessary permissions to access the Docker socket file.
Resolution Steps:
1.Verify Docker Group Membership:
Ensure that the user executing Docker commands is a member of the “docker” group. Use the following command to add a user to the docker group:
Command: sudo usermod -aG docker $USER
Note: Changes to group membership may require a system restart or user re-login to take effect.
2.Check Docker Socket Ownership:
Confirm that the Docker socket file (/var/run/docker.sock) is owned by the “docker” group. Execute the following command to adjust ownership:
Command: sudo chown root:docker /var/run/docker.sock
This command ensures that the Docker socket file is owned by the user “root” and the group “docker.”
3.Review Docker Socket Permissions:
Validate the permissions of the Docker socket file (/var/run/docker.sock) using the command:
Command: sudo chmod 664 /var/run/docker.sock
This command sets appropriate permissions for the Docker socket file.
Conclusion:
By following these steps, you should resolve the permission-related issue. Subsequently, when you attempt to run the build in your Jenkins declarative pipeline, the error should no longer persist.