How Do You Run a Docker Container as a Non-root User?

Problem scenario
You run this command: docker run -it ubuntu bash

docker: Got permission denied while trying to connect to the Docker daemon sockes/create: dial unix /var/run/docker.sock: connect: permission denied.

You do not want to run a Docker container as a privileged user (as a recommended practice).  What should you do?

Solution
Background: "To do builds in the cluster, it is essential to run the build without root privileges." This quote was taken from page 222 of Kubernetes Patterns by Bilgin Ibryam and Roland Huß (O'Reilly). Copyright 2019 Bilgin Ibryam and Roland Huß, 978-1-492-05028-5.

Prerequisites

Install Docker.  If you need assistance, see this posting.

Procedures
1.  Run these commands:

sudo newgrp docker
exit
sudo usermod -aG docker jdoe  
# Replace "jdoe" with the user you want to run the Docker container with.  You can use "whoami" manually to find out what user you are. 

2.  If you were logged in as jdoe (the user you just added), log out and log back in.

3.  Now try again (e.g., run docker run -it ubuntu bash).


You may also want to see this external posting for additional assistance.

Leave a comment

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