131

BY default when you run

docker run -it [myimage]

OR

docker attach [mycontainer]

you connect to the terminal as root user, but I would like to connect as a different user. Is this possible?

phuclv
  • 37,963
  • 15
  • 156
  • 475
Andy59469
  • 1,674
  • 2
  • 13
  • 18

9 Answers9

137

For docker run:

Simply add the option --user <user> to change to another user when you start the docker container.

docker run -it --user nobody busybox

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.

docker run -it busybox  # CTRL-P/Q to quit
docker attach <container id>  # then you have root user
/ # id
uid=0(root) gid=0(root) groups=10(wheel)

docker run -it --user nobody busybox # CTRL-P/Q to quit
docker attach <container id>  
/ $ id
uid=99(nobody) gid=99(nogroup)

If you really want to attach to the user you want to have, then

  1. start with that user run --user <user> or mention it in your Dockerfile using USER
  2. change the user using `su
Auzias
  • 3,638
  • 14
  • 36
Larry Cai
  • 55,923
  • 34
  • 110
  • 156
  • got s6-rmrf: fatal: unable to remove /var/run/s6/container_environment: Permission denied – Ardhi Jan 14 '22 at 15:26
  • Well for the `docker exec` it is actually possible to override the user with the `--user` argument. You're attaching to the existing container, true, but not to the existing tty, so the user could be different. – salmin Nov 07 '22 at 17:41
124

You can run a shell in a running docker container using a command like:

docker exec -it --user root <container id> /bin/bash
phuclv
  • 37,963
  • 15
  • 156
  • 475
Jason
  • 9,408
  • 5
  • 36
  • 36
  • 2
    *root* is the default user. `--user` option can be omitted when commands have to be run as root I suppose. – Stphane Oct 24 '17 at 10:09
  • 16
    @Stphane that's not always the case since the default user can be customized in **Dockerfile** via the `USER` instruction, see https://docs.docker.com/engine/reference/builder/#user – ryenus May 09 '19 at 08:02
15

As an updated answer from 2020. --user, -u option is Username or UID (format: <name|uid>[:<group|gid>]).

Then, it works for me like this,

docker exec -it -u root:root container /bin/bash

Reference: https://docs.docker.com/engine/reference/commandline/exec/

phuclv
  • 37,963
  • 15
  • 156
  • 475
Nurhun
  • 475
  • 1
  • 9
  • 21
7

You can specify USER in the Dockerfile. All subsequent actions will be performed using that account. You can specify USER one line before the CMD or ENTRYPOINT if you only want to use that user when launching a container (and not when building the image). When you start a container from the resulting image, you will attach as the specified user.

kliew
  • 3,073
  • 1
  • 14
  • 25
2

The only way I am able to make it work is by:

docker run -it -e USER=$USER -v /etc/passwd:/etc/passwd -v `pwd`:/siem mono bash
su - magnus

So I have to both specify $USER environment variable as well a point the /etc/passwd file. In this way, I can compile in /siem folder and retain ownership of files there not as root.

Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
daparic
  • 3,794
  • 2
  • 36
  • 38
1

My solution:

#!/bin/bash
user_cmds="$@"

GID=$(id -g $USER)
UID=$(id -u $USER)
RUN_SCRIPT=$(mktemp -p $(pwd))
(
cat << EOF
addgroup --gid $GID $USER
useradd --no-create-home --home /cmd --gid $GID --uid $UID  $USER
cd /cmd
runuser -l $USER -c "${user_cmds}"
EOF
) > $RUN_SCRIPT

trap "rm -rf $RUN_SCRIPT" EXIT

docker run -v $(pwd):/cmd --rm my-docker-image "bash /cmd/$(basename ${RUN_SCRIPT})"

This allows the user to run arbitrary commands using the tools provides by my-docker-image. Note how the user's current working directory is volume mounted to /cmd inside the container.

I am using this workflow to allow my dev-team to cross-compile C/C++ code for the arm64 target, whose bsp I maintain (the my-docker-image contains the cross-compiler, sysroot, make, cmake, etc). With this a user can simply do something like:

cd /path/to/target_software
cross_compile.sh "mkdir build; cd build; cmake ../; make"

Where cross_compile.sh is the script shown above. The addgroup/useradd machinery allows user-ownership of any files/directories created by the build.

While this works for us. It seems sort of hacky. I'm open to alternative implementations ...

rmccabe3701
  • 1,418
  • 13
  • 31
1

For docker-compose. In the docker-compose.yml:

version: '3'
services:
    app:
        image: ...
        user: ${UID:-0}
...

In .env:

UID=1000
x-yuri
  • 16,722
  • 15
  • 114
  • 161
1

This solved my use case that is: "Compile webpack stuff in nodejs container on Windows running Docker Desktop with WSL2 and have the built assets under your currently logged in user."

docker run -u 1000 -v "$PWD":/build -w /build node:10.23 /bin/sh -c 'npm install && npm run build'

Based on the answer by eigenfield. Thank you!

Also this material helped me understand what is going on.

Josef Sábl
  • 7,538
  • 9
  • 54
  • 66
0

Execute command as www-data user: docker exec -t --user www-data container bash -c "ls -la"

Vasili Pascal
  • 3,102
  • 1
  • 27
  • 21