Daniel F Daniel F - 5 months ago 18
Linux Question

Docker: user change in terminal prompt

I was just looking at the Docker linode/lamp container documentation, and encountered the following warning:

sudo docker run -p 80:80 -t -i linode/lamp /bin/bash
Caution: This command also changes the terminal prompt to the root user within the new container.

Assuming I have the following Dockerfile

FROM alpine:3.3

RUN apk --update add \
build-base python-dev \
ca-certificates python &&\
adduser -D -u 1001 not_root

USER not_root

CMD ["/bin/sh"]


When I run some python code in this container (loaded from a volume and executed via
docker run
), it gets executed as the not_root user.

But when I then later do a
docker exec -it -u root that_container /bin/sh
in order to manually install a package, lets say for testing purposes, does the running container started with
docker start
or
docker run
still run as not_root? Or will that
docker exec
have changed the active user?

Answer

No, doing a docker exec doesn't modify existing processes running in the container:

bash$ docker run -u 1000 -d --name=top busybox top
fba16b1460ed53673002e81d8592f63dd1df1f90809b7046671b453bb59b1668

bash$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
fba16b1460ed        busybox             "top"                    5 seconds ago       Up 2 seconds                                 top

bash$ docker exec -u root -it top /bin/sh
/ # ps -a
PID   USER     TIME   COMMAND
    1 1000       0:00 top
    7 root       0:00 /bin/sh
   13 root       0:00 ps -a