Resolving WSL Permission Denied When Connecting to Docker Daemon

If you are using Windows Subsystem for Linux (WSL) and trying to run Docker, you might encounter a permission denied error. This error usually happens when your user does not have permission to access the Docker daemon socket. This blog will guide you through fixing this issue.

Scope

This blog is for users who face permission issues when connecting to the Docker daemon in WSL. It covers the steps to add your user to the Docker group and ensure proper permissions.

Purpose

The purpose of this guide is to help Developer and System Administrator on how to:

  • Resolve the permission denied error when trying to connect to Docker in WSL.

By following the steps, you will be able to run Docker commands without encountering permission issues.

Issue

When running the command docker ps in WSL, you might encounter the following issue: ‘Permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock‘.

Prerequisites

Ensure that your distribution is integrated into your WSL settings. To set this up, follow these steps:

  1. Open Docker Desktop settings.
  2. Select Resources.
  3. Click on WSL Integration.
  4. Choose the distribution you want to access Docker from; in this case, I selected Ubuntu 22.04 as my example.
  5. Don’t forget to apply the changes and restart Docker.
Resolving WSL Permission Denied When Connecting to Docker Daemon

Steps to Fix Permission Issues

Step 1. Open WSL: Launch your WSL terminal (Ubuntu or any other distribution).

Step 2. Create a Docker Group:

sudo addgroup --system docker

Step 3. Add Your User to the Docker Group:

sudo adduser $USER docker

Step 4. Activate the New Group:

newgrp docker

Step 5. Set Permissions on the Docker Socket:

sudo chown root:docker /var/run/docker.sock
sudo chmod g+w /var/run/docker.sock

Step 6. Restart WSL: To ensure changes take effect, restart your WSL:

wsl --shutdown

Step 7. Test Docker: Run a simple Docker command to check if everything works:

docker ps

Optional:

docker run hello-world

Problem Solved

Resolving WSL Permission Denied When Connecting to Docker Daemon

Final Thoughts

By following these steps, you should no longer face permission issues when trying to connect to the Docker daemon in WSL. This setup allows you to run Docker commands smoothly and enhances your development workflow. If you encounter any other issues, make sure to check the Docker documentation or seek help from the community.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.