Skip to content

A local-first personal finance app

non-root users for Dockerfiles (#300)

Harrison Kiangd8c7a0a3582f
diff --git a/Dockerfile b/Dockerfile
index 1f9cee4746..ad5aa098ed 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -7,6 +7,14 @@ RUN yarn workspaces focus --all --production
FROM node:18-bullseye-slim as prod
RUN apt-get update && apt-get install tini && apt-get clean -y && rm -rf /var/lib/apt/lists/*
+
+ARG USERNAME=actual
+ARG USER_UID=1001
+ARG USER_GID=$USER_UID
+RUN groupadd --gid $USER_GID $USERNAME \
+ && useradd --uid $USER_UID --gid $USER_GID -m $USERNAME
+RUN mkdir /data && chown -R ${USERNAME}:${USERNAME} /data
+
WORKDIR /app
COPY --from=base /app/node_modules /app/node_modules
ADD package.json app.js ./
diff --git a/docker/edge-alpine.Dockerfile b/docker/edge-alpine.Dockerfile
index 6806d64f68..7c3f57ec03 100644
--- a/docker/edge-alpine.Dockerfile
+++ b/docker/edge-alpine.Dockerfile
@@ -17,6 +17,13 @@ RUN unzip /tmp/desktop-client.zip -d /public
FROM alpine:3.17 as prod
RUN apk add --no-cache nodejs tini
+
+ARG USERNAME=actual
+ARG USER_UID=1001
+ARG USER_GID=$USER_UID
+RUN addgroup -S ${USERNAME} -g ${USER_GID} && adduser -S ${USERNAME} -G ${USERNAME} -u ${USER_UID}
+RUN mkdir /data && chown -R ${USERNAME}:${USERNAME} /data
+
WORKDIR /app
COPY --from=base /app/node_modules /app/node_modules
COPY --from=base /public /public
diff --git a/docker/edge-ubuntu.Dockerfile b/docker/edge-ubuntu.Dockerfile
index b891e2344d..99e4e757ef 100644
--- a/docker/edge-ubuntu.Dockerfile
+++ b/docker/edge-ubuntu.Dockerfile
@@ -16,6 +16,14 @@ RUN unzip /tmp/desktop-client.zip -d /public
FROM node:18-bullseye-slim as prod
RUN apt-get update && apt-get install tini && apt-get clean -y && rm -rf /var/lib/apt/lists/*
+
+ARG USERNAME=actual
+ARG USER_UID=1001
+ARG USER_GID=$USER_UID
+RUN groupadd --gid $USER_GID $USERNAME \
+ && useradd --uid $USER_UID --gid $USER_GID -m $USERNAME
+RUN mkdir /data && chown -R ${USERNAME}:${USERNAME} /data
+
WORKDIR /app
COPY --from=base /app/node_modules /app/node_modules
COPY --from=base /public /public
diff --git a/docker/stable-alpine.Dockerfile b/docker/stable-alpine.Dockerfile
index 56ce5463f8..e6b2165204 100644
--- a/docker/stable-alpine.Dockerfile
+++ b/docker/stable-alpine.Dockerfile
@@ -9,6 +9,13 @@ RUN if [ "$(uname -m)" = "armv7l" ]; then npm install bcrypt better-sqlite3 --bu
FROM alpine:3.17 as prod
RUN apk add --no-cache nodejs tini
+
+ARG USERNAME=actual
+ARG USER_UID=1001
+ARG USER_GID=$USER_UID
+RUN addgroup -S ${USERNAME} -g ${USER_GID} && adduser -S ${USERNAME} -G ${USERNAME} -u ${USER_UID}
+RUN mkdir /data && chown -R ${USERNAME}:${USERNAME} /data
+
WORKDIR /app
COPY --from=base /app/node_modules /app/node_modules
ADD package.json app.js ./
diff --git a/docker/stable-ubuntu.Dockerfile b/docker/stable-ubuntu.Dockerfile
index a8d1c98cf0..eb02aad66f 100644
--- a/docker/stable-ubuntu.Dockerfile
+++ b/docker/stable-ubuntu.Dockerfile
@@ -8,6 +8,14 @@ RUN yarn workspaces focus --all --production
FROM node:18-bullseye-slim as prod
RUN apt-get update && apt-get install tini && apt-get clean -y && rm -rf /var/lib/apt/lists/*
+
+ARG USERNAME=actual
+ARG USER_UID=1001
+ARG USER_GID=$USER_UID
+RUN groupadd --gid $USER_GID $USERNAME \
+ && useradd --uid $USER_UID --gid $USER_GID -m $USERNAME
+RUN mkdir /data && chown -R ${USERNAME}:${USERNAME} /data
+
WORKDIR /app
COPY --from=base /app/node_modules /app/node_modules
ADD package.json app.js ./
diff --git a/upcoming-release-notes/300.md b/upcoming-release-notes/300.md
new file mode 100644
index 0000000000..4256ba4b53
--- /dev/null
+++ b/upcoming-release-notes/300.md
@@ -0,0 +1,6 @@
+---
+category: Maintenance
+authors: [hkiang01]
+---
+
+Optional non-root user for Docker
\ No newline at end of file