Skip to content

Instantly share code, notes, and snippets.

@joglomedia
Forked from avishayp/Dockerfile
Created January 31, 2020 09:05
Show Gist options
  • Save joglomedia/532052f351d53daf979518c5514df1f8 to your computer and use it in GitHub Desktop.
Save joglomedia/532052f351d53daf979518c5514df1f8 to your computer and use it in GitHub Desktop.

Revisions

  1. @avishayp avishayp created this gist Sep 25, 2018.
    28 changes: 28 additions & 0 deletions Dockerfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    # non root user example for alpine
    #
    # usage:
    # $ docker build --build-arg "USER=someuser" --tag test .
    # $ docker run --rm test

    FROM alpine

    ARG USER=default
    ENV HOME /home/$USER

    # install sudo as root
    RUN apk add --update sudo

    # add new user
    RUN adduser -D $USER \
    && echo "$USER ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/$USER \
    && chmod 0440 /etc/sudoers.d/$USER

    USER $USER
    WORKDIR $HOME

    # files in /home/$USER to be owned by $USER
    # docker has --chown flag for COPY, but it does not expand ENV so we fallback to:
    # COPY src src
    # RUN sudo chown -R $USER:$USER $HOME

    CMD echo "User $(whoami) running from $PWD with premissions: $(sudo -l)"