Reputation: 184
I have 2 docker images, the first Dockerfile creates a user using:
RUN useradd -m newuser
then it switches to the user and workspace as follows:
USER newuser
WORKDIR /home/newuser/
The second docker file reads from the first image using the FROM statement.
Should the USER
and WORKDIR
instructions still hold in the second file without having to put them in again?
Upvotes: 3
Views: 5230
Reputation: 3798
From this Dockerfile:
FROM debian:8
ENV HOME /home/user
RUN useradd --create-home --home-dir $HOME user \
&& mkdir -p $HOME \
&& chown -R user:user $HOME
WORKDIR $HOME
USER user
A build and a run later:
$docker build -t deb .
$docker run --rm deb bash -c "pwd && whoami"
/home/user
user
Now from this Dockerfile, based from the previous image:
FROM deb
ENTRYPOINT [ "sh" ]
Build and run:
$docker build -t debb .
$docker run --rm -it debb
[container]$ pwd && whoami
/home/user
user
So, yes the USER
and the WORKDIR
are inherited.
Client:
Version: 1.10.3
API version: 1.22
Go version: go1.5.3
Git commit: 20f81dd
Built: Thu Mar 10 15:38:58 2016
OS/Arch: linux/amd64
Server:
Version: 1.10.3
API version: 1.22
Go version: go1.5.3
Git commit: 20f81dd
Built: Thu Mar 10 15:38:58 2016
OS/Arch: linux/amd64
Upvotes: 9