Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Docker Tip #20: Managing Docker Without sudo on Linux

blog/cards/docker-tips-and-tricks.jpg

Being able to access the Docker daemon as a non-root user is a quality of life enhancement. Here's how to do it on Linux.

By default when you install Docker on Linux, you can only access the Docker daemon as the root user, or by using sudo. It’s really easy to forget to add sudo when running Docker commands and you really shouldn’t be logged in as root all the time on a Linux system.

Add a docker group and then add your user to it:
sudo groupadd docker
sudo usermod -aG docker $USER

If you’re on a Linux server you’ll want to log out and then log back in for the changes to take effect. If you’re using Linux on the desktop, log out of your desktop session and then log back in.

I personally do this on my own Linux development box and my production servers.

This works because the Docker daemon binds to a Unix socket which is owned by the root user. With the above change, it makes the socket accessible by whatever user you happen to be logged in as (that’s what $USER refers to).

If you want to read up on the security implications of doing this then check out Docker’s documentation related to attack surface.

Free Intro to Docker Email Course

Over 5 days you'll get 1 email per day that includes video and text from the premium Dive Into Docker course. By the end of the 5 days you'll have hands on experience using Docker to serve a website.


Comments