Skip to content

Running Docker and Docker Compose commands without sudo

pexels-pixabay-163726.jpg

Running Docker and Docker Compose commands without sudo is a common request, especially for development environments. Note the Security Implications. Here’s how to set it up:

Best answer: Add the user to the docker group

By default, the Docker daemon binds to a Unix socket instead of a TCP port. The Unix socket is owned by the user root and other users can only access it using sudo. The Docker daemon always runs as the root user.

If you don’t want to use sudo it when you use the Docker command, create a Unix group called docker and add users to it. When the Docker daemon starts, it makes the ownership of the Unix socket read/writable by the docker group.

sudo usermod -aG docker $USER

Replace $USER with your username if it doesn’t work directly.

Log out and log back in

This ensures your user is running with the correct permissions.

Test the Docker command without sudo

Run the following:

docker run hello-world

This should download and run the hello-world Docker image, which simply prints a greeting and exits.

Test Docker Compose without sudo

Just try any docker-compose command, for example:

docker-compose --version
or
docker compose version // for v2

This should display the version of Docker Compose installed.

Security Implications

Do note that being a part of the docker group is equivalent to having root access. This is because Docker containers can be used to gain root access to the host system. Ensure you trust the members of the docker group and be cautious about the images and containers you run.

Remember, if you’re using a cloud environment or an environment where others might have access, understand the risks before implementing these changes. Always consider security implications when adjusting permissions.

Visits: 56

Leave a Reply

Your email address will not be published. Required fields are marked *