Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run docker image as a non-root user?

Tags:

I'm new to docker. When I run a docker images like ubuntu image by using the command,

sudo docker run -i -t ubuntu:14.04

By default, it is entering into the container as root like this. enter image description here

I searched regarding this, but I couldn't get any of how to start a docker image as a non root user as I'm completely a starter for this topic.

It would be great if someone explains with an example of how to run a docker image as a non root user.

like image 258
Stranger Avatar asked Apr 27 '15 08:04

Stranger


People also ask

Can you run Docker as non-root?

Rootless mode allows running the Docker daemon and containers as a non-root user to mitigate potential vulnerabilities in the daemon and the container runtime. Rootless mode does not require root privileges even during the installation of the Docker daemon, as long as the prerequisites are met.

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

For docker run : Simply add the option --user <user> to change to another user when you start the docker container. For docker attach or docker exec : Since the command is used to attach/execute into the existing process, therefore it uses the current user there directly.

How do I run a docker as a root user?

As an alternative, we can also access the Docker container as root. In this case, we'll use the nsenter command to access the Docker container. To use the nsenter command, we must know the PID of the running container. This allows us to access the Docker container as a root user and run any command to access any file.


3 Answers

the docker run command has the -u parameter to allow you to specify a different user. In your case, and assuming you have a user named foo in your docker image, you could run:

sudo docker run -i -t -u foo ubuntu:14.04 /bin/bash

NOTE: The -u parameter is the equivalent of the USER instruction for Dockerfile.

like image 66
Thomasleveil Avatar answered Oct 02 '22 01:10

Thomasleveil


This is admittedly hacky, but good for those quick little containers you start just to test something quickly:

#!/bin/bash

set -eu

NAME=$1
IMG=$2

#UID=$(id -u)
USER=$(id -un)
GID=$(id -g)
GROUP=$(id -gn)

docker run -d -v /tmp:/tmp -v "/home/$USER:/home/$USER" -h "$NAME" --name "$NAME" "$IMG" /bin/bash

docker exec "$NAME" /bin/bash -c "groupadd -g $GID $GROUP && useradd -M -s /bin/bash -g $GID -u $UID $USER"

Full version of the script I use here:

https://github.com/ericcurtin/staging/blob/master/d-run

like image 29
ericcurtin Avatar answered Oct 01 '22 23:10

ericcurtin


udocker is a basic variant of docker which runs in user space:

udocker is a basic user tool to execute simple docker containers in user space without requiring root privileges. Enables download and execution of docker containers by non-privileged users in Linux systems where docker is not available. It can be used to pull and execute docker containers in Linux batch systems and interactive clusters that are managed by other entities such as grid infrastructures or externally managed batch or interactive systems.

like image 40
Erwan Avatar answered Oct 01 '22 23:10

Erwan