Skip to content

Commit a28d1a3

Browse files
feat: Use non-root default user for Docker image (#2243)
* 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.
1 parent b654be9 commit a28d1a3

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

.dockerignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.nox
2+
.*cache

docker/Dockerfile

+33-2
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,47 @@ RUN apt-get -qq -y update && \
1616
python -m venv /usr/local/venv && \
1717
cd /code && \
1818
python -m pip --no-cache-dir install --upgrade pip setuptools wheel && \
19-
python -m pip --no-cache-dir install .[xmlio,contrib] && \
19+
python -m pip --no-cache-dir install '.[xmlio,contrib]' && \
2020
python -m pip list
2121

2222
FROM base
23+
24+
USER root
25+
26+
SHELL [ "/bin/bash", "-c" ]
2327
ENV PATH=/usr/local/venv/bin:"${PATH}"
28+
2429
RUN apt-get -qq -y update && \
2530
apt-get -qq -y install --no-install-recommends \
2631
curl && \
2732
apt-get -y autoclean && \
2833
apt-get -y autoremove && \
2934
rm -rf /var/lib/apt/lists/*
30-
COPY --from=builder /usr/local/venv /usr/local/venv
35+
36+
# Create non-root user "moby" with uid 1000
37+
RUN adduser \
38+
--shell /bin/bash \
39+
--gecos "default user" \
40+
--uid 1000 \
41+
--disabled-password \
42+
moby && \
43+
chown -R moby /home/moby && \
44+
mkdir /work && \
45+
chown -R moby /work && \
46+
echo -e "\nexport PATH=/usr/local/venv/bin:${PATH}\n" >> /home/moby/.bashrc
47+
48+
COPY --from=builder --chown=moby /usr/local/venv /usr/local/venv/
49+
50+
USER moby
51+
52+
ENV USER ${USER}
53+
ENV HOME /home/moby
54+
WORKDIR ${HOME}/work
55+
56+
# Use C.UTF-8 locale to avoid issues with ASCII encoding
57+
ENV LC_ALL=C.UTF-8
58+
ENV LANG=C.UTF-8
59+
60+
ENV PATH=${HOME}/.local/bin:${PATH}
61+
3162
ENTRYPOINT ["/usr/local/venv/bin/pyhf"]

0 commit comments

Comments
 (0)