I'm looking how can I store and restore the USER from an inherited docker image in a Dockerfile. My parent image for example specifies a default service and sets USER to serviceuser.
Now I have a depending image that inherits from the parent image, does some modifications as root, but want's to keep the docker image user to serviceuser.
I can do this manually:
parent Dockerfile:
from default
USER serviceuser
ENTRYPOINT ["some-service"]
other Dockerfile:
from parent
USER root
RUN apt-get install -y cool-stuff
USER serviceuser
However, this way when I change the user in parent I have to update all child images. Is there a way to dymaically do this in the child Dockerfile?
Other options I thought about are to use sudo in the child script or running the entrypoint as root (i.e. not setting USER) and doing the service user switch in the start script. But both have security implications.
The best way is to put the Dockerfile inside the empty directory and then add only the application and configuration files required for building the docker image. To increase the build's performance, you can exclude files and directories by adding a . dockerignore file to that directory as well.
Differences between CMD & ENTRYPOINT CMD commands are ignored by Daemon when there are parameters stated within the docker run command while ENTRYPOINT instructions are not ignored but instead are appended as command line parameters by treating those as arguments of the command.
The default user in a Dockerfile is the user of the parent image. For example, if your image is derived from an image that uses a non-root user example: swuser , then RUN commands in your Dockerfile will run as swuser .
The WORKDIR instruction sets a working directory for other Dockerfile instructions, such as RUN , CMD , and also the working directory for running instances of the container image. The WORKDIR instruction's format goes like this: Dockerfile Copy. WORKDIR <path to working directory>
If you are author of parent image you can do this like this:
ENV serviceuser=foo
RUN useradd $serviceuser
USER $serviceuser
child image:
USER root
RUN apt-get install -y cool-stuff
USER $serviceuser
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With