# How to fix docker: Got permission denied while trying to connect to the Docker daemon socket

This error may appear when running docker commands and it is caused by
insufficient privilages.

## Solution

To resolve the problem, you need to do the following:

1. Create a docker group

```bash
sudo groupadd docker
```

1. Add your user to the docker group

```bash
sudo usermod -aG docker ${USER}
```

1. You would need to log out and log back in so that your group membership is
   re-evaluated or type the following command:

```bash
su -s ${USER}
```

1. • Verify that you can run docker commands without `sudo`

```bash
docker run hello-world
```

- This command downloads a test image and runs it in a container. When the
  container runs, it prints an informational message and exits.
- If you initially ran Docker CLI commands using `sudo` before adding your user
  to the docker group, you may see the following error, which indicates that
  your `~/.docker/` directory was created with incorrect permissions due to the
  `sudo` commands.

```
WARNING: Error loading config file: /home/user/.docker/config.json -
stat /home/user/.docker/config.json: permission denied
```

• To fix this problem, either remove the `~/.docker/` directory (it is recreated
automatically, but any custom settings are lost), or change its ownership and
permissions using the following commands:

```bash
sudo chown "$USER":"$USER" /home/"$USER"/.docker -R
sudo chmod g+rwx "$HOME/.docker" -R
```
