Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Docker as non-root user

Tags:

docker

I'm trying to run docker as a non-root user. When I try, I get the following error:

$ docker ps
FATA[0000] Get http:///var/run/docker.sock/v1.18/containers/json: dial unix /var/run/docker.sock: permission denied. Are you trying to connect to a TLS-enabled daemon without TLS? 

I can run docker as root:

$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[...]

I've put this user into a Unix group docker:

$ groups
domain users docker suappprod stashadmin config_mgmt remote server access sudevmail sudevsvn

However, it still appears that this user cannot run most of the docker commands without sudoing as root.

I am on an older version of docker:

$ docker --version
Docker version 1.6.1, build a8a31ef/1.6.1

I know that the latest is 1.10, and it's possible for our company to update all of the docker installs to 1.9.2, but that will take a lot of effort and time.

Is there something else I need to look at? The user has been logged in and out multiple times. I have not rebooted the system yet.

like image 498
David W. Avatar asked Mar 07 '16 16:03

David W.


People also ask

Should Docker run as root or user?

One of the best practices while running Docker Container is to run processes with a non-root user. This is because if a user manages to break out of the application running as root in the container, he may gain root user access on host.

How do I run a docker container as a different user?

To run a command as a different user inside your container, add the --user flag: docker exec --user guest container-name whoami.

How do I stop containers running as root?

Add a non-privileged user and set it as the process owner The second line sets the Dockerfile to switch to the newly created user. There's a point in setting it to the UID rather than the username, and we'll get to it. With just this change, your container will now run as a non-privileged user!


2 Answers

Adding users to the Docker group (since Docker group has full control to the socket)

As root, add the user to the docker group:

  • Cat /etc/group
  • gpasswd -a <username> docker
  • Exit (as root)
  • Log off
  • Log in as the user, and attempt to run "Docker PS" to validate.

This is how I've been able to set it up on my Ubuntu systems time and time again.

like image 160
vXE Avatar answered Sep 21 '22 03:09

vXE


Check what this command gives --> ls -l /var/run/docker.sock You may want to change the permissions of this file using chmod (Ex: sudo chmod 777 /var/run/docker.sock) depending on what permissions you want to give.

like image 35
Achsah Avatar answered Sep 23 '22 03:09

Achsah