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

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


Full error message

docker-compose up db
permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/json?all=1&filters=%7B%22label%22%3A%7B%22com.docker.compose.config-hash%22%3Atrue%2C%22com.docker.compose.project%3Dvrs-backend%22%3Atrue%7D%7D": dial unix /var/run/docker.sock: connect: permission denied

Snapshot


Brief explanation

The error message we're seeing indicates that the user running the docker-compose command does not have the necessary permissions to connect to the Docker daemon, which manages Docker containers.

This typically happens because the user doesn't have the required privileges to interact with Docker.


Context

You are developing a Ruby on Rails web application and want to containerize it using Docker for easier development and deployment. Your application relies on a database, and you plan to use a separate Docker container for the database service.

In my case, I tried using one docker container in a Ruby on Rails application.


Current configuration


Check your user

whoami

or

echo $USER

Add User to the Docker Group

sudo usermod -aG docker $USER

Replace $USER with your actual username.

Since my username is calaca. Take a look at the result:


Add user to docker group

sudo usermod -aG docker $USER

Replace $USER with your actual username.

Since my username is calaca. Take a look at the result:


Check group membership

groups $USER

Replace $USER with your actual username.

Since my username is calaca. Take a look at the result:


Restart docker

sudo systemctl restart docker

Check docker socket permission

ls -l /var/run/docker.sock


Restart the Docker service again

sudo systemctl restart docker

Restart the OS


Run the test


Celebrate

You've made it!

The Office GIF - The Office Happy - Discover & Share GIFs


Let's become friends


Final thoughts

I hope this article has been helpful to you. Please feel free to reach out if you have any questions. Your thoughts, suggestions, and corrections are more than welcome.

By the way, don't hesitate to drop your suggestions for new blog articles.

I look forward to seeing you next time.