Skip to content

Commit e887f95

Browse files
committed
Update Dockerfile to run as non-root
1 parent a405213 commit e887f95

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

Dockerfile

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
FROM debian:jessie
22

3-
RUN apt-get update && apt-get install -y build-essential scons libssl-dev
3+
# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
4+
RUN groupadd -r mongodb && useradd -r -g mongodb mongodb
5+
6+
RUN apt-get update && apt-get install -y \
7+
build-essential \
8+
curl \
9+
libssl-dev \
10+
scons
411

512
# "--use-system" deps
613
RUN apt-get update && apt-get install -y \
@@ -16,13 +23,16 @@ RUN apt-get update && apt-get install -y \
1623
libv8-dev
1724
ENV SCONS_OPTS --use-system-snappy --use-system-tcmalloc --use-system-pcre --use-system-boost --use-system-v8 --ssl
1825

26+
RUN curl -o /usr/local/bin/gosu -SL 'https://github.com/tianon/gosu/releases/download/1.0/gosu' \
27+
&& chmod +x /usr/local/bin/gosu
28+
1929
ADD . /usr/src/mongo
2030
WORKDIR /usr/src/mongo
2131

2232
# the unstripped binaries and build artifacts are enormous, so we strip and
2333
# remove them manually to help alleviate that huge layer being an issue
2434
RUN scons -j"$(nproc)" $SCONS_OPTS core tools \
25-
&& find -maxdepth 1 -type f -executable -exec strip '{}' + \
35+
&& find -maxdepth 1 -type f -executable -not -name '*.sh' -exec strip '{}' + \
2636
&& rm -rf build
2737

2838
# the tests compile huge amounts of data, so we skip them for now
@@ -31,9 +41,10 @@ RUN scons -j"$(nproc)" $SCONS_OPTS core tools \
3141
# since we're stripping and removing build artifacts, we get to install
3242
# manually too (since scons rightfully thinks we need to rebuild)
3343
#RUN scons $SCONS_OPTS install
34-
RUN find -maxdepth 1 -type f -executable -exec ln -v '{}' /usr/local/bin/ ';'
44+
RUN find -maxdepth 1 -type f -executable -not -name '*.sh' -exec ln -v '{}' /usr/local/bin/ ';'
3545

3646
VOLUME /data/db
47+
ENTRYPOINT ["/usr/src/mongo/docker-entrypoint.sh"]
3748

38-
EXPOSE 27017 28017
49+
EXPOSE 27017
3950
CMD ["mongod"]

docker-entrypoint.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
set -e
3+
4+
if [ "$1" = 'mongod' ]; then
5+
chown -R mongodb "/data/db"
6+
exec gosu mongodb "$@"
7+
fi
8+
9+
exec "$@"

0 commit comments

Comments
 (0)