Skip to content

Commit 28da43e

Browse files
authored
chore: use non-root user for alpine images (#34162)
1 parent 0cc933e commit 28da43e

File tree

8 files changed

+81
-26
lines changed

8 files changed

+81
-26
lines changed

apps/meteor/.docker/Dockerfile.alpine

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,24 @@ LABEL maintainer="buildmaster@rocket.chat"
44

55
ENV LANG=C.UTF-8
66

7-
RUN apk add --no-cache deno ttf-dejavu
7+
# `nogroup` group is historically reserved for NFS.
8+
# We don't use any NFS related tools in this image.
9+
# For the same reason of NFS using the gid, we can also use it as long as there are no conflicts in terms of running processes with the same egid (which is 1 in our case).
10+
# While 65533 raw gid could be used, renaming nogroup to rocketchat here for maximum compatibility with older debian image.
11+
# More info on nobody/nogroup - https://wiki.ubuntu.com/nobody
12+
# Debian wiki - https://wiki.debian.org/SystemGroups
13+
# """
14+
# daemon: Some unprivileged daemons that need to write to files on disk run as daemon.daemon (e.g., portmap, atd, probably others).
15+
# Daemons that don't need to own any files can run as nobody.nogroup instead,
16+
# and more complex or security conscious daemons run as dedicated users.
17+
# The daemon user is also handy for locally installed daemons.
18+
# """
19+
RUN apk add --no-cache deno ttf-dejavu \
20+
&& apk add --no-cache --virtual deps shadow python3 make g++ py3-setuptools libc6-compat \
21+
&& groupmod -n rocketchat nogroup \
22+
&& useradd -u 65533 -r -g rocketchat rocketchat
823

9-
ADD . /app
24+
COPY --chown=rocketchat:rocketchat . /app
1025

1126
# needs a mongo instance - defaults to container linking with alias 'mongo'
1227
ENV DEPLOY_METHOD=docker \
@@ -17,30 +32,35 @@ ENV DEPLOY_METHOD=docker \
1732
ROOT_URL=http://localhost:3000 \
1833
Accounts_AvatarStorePath=/app/uploads
1934

20-
RUN set -x \
21-
&& apk add --no-cache --virtual .fetch-deps python3 make g++ py3-setuptools libc6-compat \
35+
USER rocketchat
36+
37+
RUN cd /app/bundle/programs/server \
38+
&& npm install --omit=dev \
2239
&& cd /app/bundle/programs/server \
23-
&& npm install --omit=dev --unsafe-perm \
24-
# Start hack for sharp...
2540
&& rm -rf npm/node_modules/sharp \
26-
&& npm install sharp@0.32.6 \
41+
&& npm install sharp@0.32.6 --no-save \
2742
&& mv node_modules/sharp npm/node_modules/sharp \
43+
# End hack for sharp
2844
&& cd /app/bundle/programs/server/npm/node_modules/@vector-im/matrix-bot-sdk \
2945
&& npm install \
30-
# End hack for sharp
3146
# # Start hack for isolated-vm...
3247
# && rm -rf npm/node_modules/isolated-vm \
3348
# && npm install isolated-vm@4.6.0 \
3449
# && mv node_modules/isolated-vm npm/node_modules/isolated-vm \
3550
# # End hack for isolated-vm
3651
&& cd /app/bundle/programs/server/npm \
3752
&& npm rebuild bcrypt --build-from-source \
38-
&& npm cache clear --force \
39-
&& apk del .fetch-deps
53+
&& npm cache clear --force
54+
55+
USER root
56+
57+
RUN apk del deps
58+
59+
USER rocketchat
4060

4161
# TODO: remove hack once upstream builds are fixed
42-
COPY matrix-sdk-crypto.linux-x64-musl.node /app/bundle/programs/server/npm/node_modules/@matrix-org/matrix-sdk-crypto-nodejs
43-
COPY matrix-sdk-crypto.linux-x64-musl.node /app/bundle/programs/server/npm/node_modules/@vector-im/matrix-bot-sdk/node_modules/@matrix-org/matrix-sdk-crypto-nodejs
62+
COPY --chown=rocketchat:rocketchat matrix-sdk-crypto.linux-x64-musl.node /app/bundle/programs/server/npm/node_modules/@matrix-org/matrix-sdk-crypto-nodejs
63+
COPY --chown=rocketchat:rocketchat matrix-sdk-crypto.linux-x64-musl.node /app/bundle/programs/server/npm/node_modules/@vector-im/matrix-bot-sdk/node_modules/@matrix-org/matrix-sdk-crypto-nodejs
4464

4565
VOLUME /app/uploads
4666

ee/apps/account-service/Dockerfile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,15 @@ ENV NODE_ENV=production \
7878
WORKDIR /app/ee/apps/${SERVICE}
7979

8080
RUN apk update && \
81-
apk --no-cache --virtual build-dependencies add g++ python3 make py3-setuptools && \
81+
apk --no-cache --virtual deps add g++ python3 make py3-setuptools shadow && \
8282
yarn workspaces focus --production && \
8383
rm -rf /var/cache/apk/* && \
84-
apk del build-dependencies
84+
groupmod -n rocketchat nogroup && \
85+
useradd -u 65533 -r -g rocketchat rocketchat && \
86+
apk del deps && \
87+
chown -R rocketchat:rocketchat /app
88+
89+
USER rocketchat
8590

8691
EXPOSE 3000 9458
8792

ee/apps/authorization-service/Dockerfile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,15 @@ ENV NODE_ENV=production \
7575
WORKDIR /app/ee/apps/${SERVICE}
7676

7777
RUN apk update && \
78-
apk --no-cache --virtual build-dependencies add g++ python3 make py3-setuptools && \
78+
apk --no-cache --virtual deps add g++ python3 make py3-setuptools shadow && \
7979
yarn workspaces focus --production && \
8080
rm -rf /var/cache/apk/* && \
81-
apk del build-dependencies
81+
groupmod -n rocketchat nogroup && \
82+
useradd -u 65533 -r -g rocketchat rocketchat && \
83+
apk del deps && \
84+
chown -R rocketchat:rocketchat /app
85+
86+
USER rocketchat
8287

8388
EXPOSE 3000 9458
8489

ee/apps/ddp-streamer/Dockerfile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,15 @@ ENV NODE_ENV=production \
8181
WORKDIR /app/ee/apps/${SERVICE}
8282

8383
RUN apk update && \
84-
apk --no-cache --virtual build-dependencies add g++ python3 make py3-setuptools && \
84+
apk --no-cache --virtual deps add g++ python3 make py3-setuptools shadow && \
8585
yarn workspaces focus --production && \
8686
rm -rf /var/cache/apk/* && \
87-
apk del build-dependencies
87+
groupmod -n rocketchat nogroup && \
88+
useradd -u 65533 -r -g rocketchat rocketchat && \
89+
apk del deps && \
90+
chown -R rocketchat:rocketchat /app
91+
92+
USER rocketchat
8893

8994
EXPOSE 3000 9458
9095

ee/apps/omnichannel-transcript/Dockerfile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,15 @@ ENV NODE_ENV=production \
8484
WORKDIR /app/ee/apps/${SERVICE}
8585

8686
RUN apk update && \
87-
apk --no-cache --virtual build-dependencies add g++ python3 make py3-setuptools && \
87+
apk --no-cache --virtual deps add g++ python3 make py3-setuptools shadow && \
8888
yarn workspaces focus --production && \
8989
rm -rf /var/cache/apk/* && \
90-
apk del build-dependencies
90+
groupmod -n rocketchat nogroup && \
91+
useradd -u 65533 -r -g rocketchat rocketchat && \
92+
apk del deps && \
93+
chown -R rocketchat:rocketchat /app
94+
95+
USER rocketchat
9196

9297
EXPOSE 3000 9458
9398

ee/apps/presence-service/Dockerfile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,15 @@ ENV NODE_ENV=production \
7878
WORKDIR /app/ee/apps/${SERVICE}
7979

8080
RUN apk update && \
81-
apk --no-cache --virtual build-dependencies add g++ python3 make py3-setuptools && \
81+
apk --no-cache --virtual deps add g++ python3 make py3-setuptools shadow && \
8282
yarn workspaces focus --production && \
8383
rm -rf /var/cache/apk/* && \
84-
apk del build-dependencies
84+
groupmod -n rocketchat nogroup && \
85+
useradd -u 65533 -r -g rocketchat rocketchat && \
86+
apk del deps && \
87+
chown -R rocketchat:rocketchat /app
88+
89+
USER rocketchat
8590

8691
EXPOSE 3000 9458
8792

ee/apps/queue-worker/Dockerfile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,15 @@ ENV NODE_ENV=production \
8484
WORKDIR /app/ee/apps/${SERVICE}
8585

8686
RUN apk update && \
87-
apk --no-cache --virtual build-dependencies add g++ python3 make py3-setuptools && \
87+
apk --no-cache --virtual deps add g++ python3 make py3-setuptools shadow && \
8888
yarn workspaces focus --production && \
8989
rm -rf /var/cache/apk/* && \
90-
apk del build-dependencies
90+
groupmod -n rocketchat nogroup && \
91+
useradd -u 65533 -r -g rocketchat rocketchat && \
92+
apk del deps && \
93+
chown -R rocketchat:rocketchat /app
94+
95+
USER rocketchat
9196

9297
EXPOSE 3000 9458
9398

ee/apps/stream-hub-service/Dockerfile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,15 @@ ENV NODE_ENV=production \
7575
WORKDIR /app/ee/apps/${SERVICE}
7676

7777
RUN apk update && \
78-
apk --no-cache --virtual build-dependencies add g++ python3 make py3-setuptools && \
78+
apk --no-cache --virtual deps add g++ python3 make py3-setuptools shadow && \
7979
yarn workspaces focus --production && \
8080
rm -rf /var/cache/apk/* && \
81-
apk del build-dependencies
81+
groupmod -n rocketchat nogroup && \
82+
useradd -u 65533 -r -g rocketchat rocketchat && \
83+
apk del deps && \
84+
chown -R rocketchat:rocketchat /app
85+
86+
USER rocketchat
8287

8388
EXPOSE 3000 9458
8489

0 commit comments

Comments
 (0)