Skip to content

Instantly share code, notes, and snippets.

@ajcastro
Last active August 16, 2017 11:07
Show Gist options
  • Save ajcastro/d06f86fc5a0aca21e66bf755165806e2 to your computer and use it in GitHub Desktop.
Save ajcastro/d06f86fc5a0aca21e66bf755165806e2 to your computer and use it in GitHub Desktop.

Solving the root User Issue

Running containers use root as the logged in user. This is fine, but in times like you run method like npm init and bower init, this will be cumbersome. This commands generate json configuration files for development. And when you try to edit this files in your host machine, you will notice that you may have Permission denied errors and you should run first chown command to give you permission on these created files.

Fortunately, we have a solution for this, by creating our own user inside the docker container.

In this example, let's say we created a container for our calculator angular app by using this command:

docker run -d -it --name calculator \
-v ~/Projects/calculator:/app \
-p 9001:9000 \
ajcastro/angular-dock

Then, we login into the shell by using this command:

docker exec -it calculator /bin/bash

We will see in the terminal that we are currently logged in as root.

We will create our own user by using this command:

adduser --disabled-password --gecos '' ajcastro -u 1000
adduser ajcastro sudo
echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

This command create a user with disabled password, and add into sudo group to have root privileges. Please make sure to set your own user and uid, and should match the ones in your own host machine. In this example, my user is ajcastro and its uid is 1000. Then exit.

You may now access the application's container shell by using this command:

docker exec -it -u ajcastro calculator /bin/bash

Please make sure to include -u flag and its value which is ajcastro in our example to login as that user.

Reference(s): http://www.yegor256.com/2014/08/29/docker-non-root.html http://stackoverflow.com/questions/24308760/running-app-inside-docker-as-non-root-user

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment