Skip to content

Commit

Permalink
feat: Use non-root default user for Docker image (#2243)
Browse files Browse the repository at this point in the history
* Add non-root default user 'moby' with uid 1000 that owns the Python virtual environment.
   - Set default working directory to /home/moby/work/.
* Add .dockerignore for local builds.
  • Loading branch information
matthewfeickert authored Jul 5, 2023
1 parent b654be9 commit a28d1a3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.nox
.*cache
35 changes: 33 additions & 2 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,47 @@ RUN apt-get -qq -y update && \
python -m venv /usr/local/venv && \
cd /code && \
python -m pip --no-cache-dir install --upgrade pip setuptools wheel && \
python -m pip --no-cache-dir install .[xmlio,contrib] && \
python -m pip --no-cache-dir install '.[xmlio,contrib]' && \
python -m pip list

FROM base

USER root

SHELL [ "/bin/bash", "-c" ]
ENV PATH=/usr/local/venv/bin:"${PATH}"

RUN apt-get -qq -y update && \
apt-get -qq -y install --no-install-recommends \
curl && \
apt-get -y autoclean && \
apt-get -y autoremove && \
rm -rf /var/lib/apt/lists/*
COPY --from=builder /usr/local/venv /usr/local/venv

# Create non-root user "moby" with uid 1000
RUN adduser \
--shell /bin/bash \
--gecos "default user" \
--uid 1000 \
--disabled-password \
moby && \
chown -R moby /home/moby && \
mkdir /work && \
chown -R moby /work && \
echo -e "\nexport PATH=/usr/local/venv/bin:${PATH}\n" >> /home/moby/.bashrc

COPY --from=builder --chown=moby /usr/local/venv /usr/local/venv/

USER moby

ENV USER ${USER}
ENV HOME /home/moby
WORKDIR ${HOME}/work

# Use C.UTF-8 locale to avoid issues with ASCII encoding
ENV LC_ALL=C.UTF-8
ENV LANG=C.UTF-8

ENV PATH=${HOME}/.local/bin:${PATH}

ENTRYPOINT ["/usr/local/venv/bin/pyhf"]

0 comments on commit a28d1a3

Please sign in to comment.