dockerdocker-compose

How to fix Docker: Got permission denied issue


I installed Docker on my Ubuntu machine.

When I run

sudo docker run hello-world

all is ok, but I want to remove the sudo command to make the command shorter.

If I write the command without sudo

docker run hello-world

it displays the following:

docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.35/containers/create: dial unix /var/run/docker.sock: connect: permission denied. See 'docker run --help'.

The same happens when I try to run:

docker-compose up

How can I resolve this?


Solution

  • If you want to run Docker as a non-root user, then you need to add your user to the docker group.

    1. Create the docker group if it does not exist:
    $ sudo groupadd docker
    
    1. Add your user to the docker group:
    $ sudo usermod -aG docker $USER
    
    1. Log in to the new docker group (to avoid having to log out and log in again; but if not enough, try to reboot):
    $ newgrp docker
    
    1. Check if Docker can be run without root:
    $ docker run hello-world
    

    Reboot if you still get an error:

    $ reboot
    

    From the official Docker documentation "Manage Docker as a non-root user":

    ⚠️ Warning

    The docker group grants root-level privileges to the user. For details on how this impacts security in your system, see Docker Daemon Attack Surface.