Skip to content

Instantly share code, notes, and snippets.

@timohuovinen
Last active March 25, 2022 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timohuovinen/03a564fcfcc039a4aa15626ce246d923 to your computer and use it in GitHub Desktop.
Save timohuovinen/03a564fcfcc039a4aa15626ce246d923 to your computer and use it in GitHub Desktop.
Adding a user in a linux docker container
FROM yourimage
# Setup the user
ENV USER_NAME=yourusername
ENV GROUP_NAME=yourusergroup # can be the same as USER_NAME
ENV HOME=/var/home/$USER_NAME
# Create the home directory for the new user.
RUN mkdir -p $HOME
# Create a user so our program doesn't run as root.
# For debian based linux
RUN groupadd -r $GROUP_NAME &&\
useradd -r -g $GROUP_NAME -d $HOME -s /sbin/nologin -c "Docker image user" $USER_NAME
# For alpine based linux
# ENV USER_ID=65535
# ENV GROUP_ID=65535
# RUN addgroup -g $USER_ID $GROUP_NAME && \
# adduser --shell /sbin/nologin --disabled-password \
# --no-create-home --uid $USER_ID --ingroup $GROUP_NAME $USER_NAME
## SETTING UP THE APP ##
WORKDIR $HOME
# (Optional) Copy in the application code.
# ADD . $HOME
# Chown all the files to the user.
RUN chown -R $USER_NAME:$GROUP_NAME $HOME
# Change to the user.
USER $USER_NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment