diff --git a/docs/deployment/docker.md b/docs/deployment/docker.md index 6e0ff77228..ff31a6a81f 100644 --- a/docs/deployment/docker.md +++ b/docs/deployment/docker.md @@ -58,8 +58,6 @@ Once the host build is complete, we are ready to build our image. The following ```Dockerfile FROM node:16-bullseye-slim -WORKDIR /app - # Install sqlite3 dependencies. You can skip this if you don't use sqlite3 in the image, # in which case you should also move better-sqlite3 to "devDependencies" in package.json. RUN apt-get update && \ @@ -67,16 +65,20 @@ RUN apt-get update && \ rm -rf /var/lib/apt/lists/* && \ yarn config set python /usr/bin/python3 +# From here on we use the least-privileged `node` user to run the backend. +USER node +WORKDIR /app + # Copy repo skeleton first, to avoid unnecessary docker cache invalidation. # The skeleton contains the package.json of each package in the monorepo, # and along with yarn.lock and the root package.json, that's enough to run yarn install. -COPY yarn.lock package.json packages/backend/dist/skeleton.tar.gz ./ +COPY --chown=node:node yarn.lock package.json packages/backend/dist/skeleton.tar.gz ./ RUN tar xzf skeleton.tar.gz && rm skeleton.tar.gz RUN yarn install --frozen-lockfile --production --network-timeout 300000 && rm -rf "$(yarn cache dir)" # Then copy the rest of the backend bundle, along with any other files we might want. -COPY packages/backend/dist/bundle.tar.gz app-config.yaml ./ +COPY --chown=node:node packages/backend/dist/bundle.tar.gz app-config*.yaml ./ RUN tar xzf bundle.tar.gz && rm bundle.tar.gz CMD ["node", "packages/backend", "--config", "app-config.yaml"] @@ -177,8 +179,6 @@ RUN yarn --cwd packages/backend build # Stage 3 - Build the actual backend image and install production dependencies FROM node:16-bullseye-slim -WORKDIR /app - # Install sqlite3 dependencies. You can skip this if you don't use sqlite3 in the image, # in which case you should also move better-sqlite3 to "devDependencies" in package.json. RUN apt-get update && \ @@ -186,18 +186,22 @@ RUN apt-get update && \ rm -rf /var/lib/apt/lists/* && \ yarn config set python /usr/bin/python3 +# From here on we use the least-privileged `node` user to run the backend. +USER node +WORKDIR /app + # Copy the install dependencies from the build stage and context -COPY --from=build /app/yarn.lock /app/package.json /app/packages/backend/dist/skeleton.tar.gz ./ +COPY --from=build --chown=node:node /app/yarn.lock /app/package.json /app/packages/backend/dist/skeleton.tar.gz ./ RUN tar xzf skeleton.tar.gz && rm skeleton.tar.gz RUN yarn install --frozen-lockfile --production --network-timeout 600000 && rm -rf "$(yarn cache dir)" # Copy the built packages from the build stage -COPY --from=build /app/packages/backend/dist/bundle.tar.gz . +COPY --from=build --chown=node:node /app/packages/backend/dist/bundle.tar.gz . RUN tar xzf bundle.tar.gz && rm bundle.tar.gz # Copy any other files that we need at runtime -COPY app-config.yaml ./ +COPY --chown=node:node app-config.yaml ./ CMD ["node", "packages/backend", "--config", "app-config.yaml"] ``` diff --git a/packages/backend/Dockerfile b/packages/backend/Dockerfile index fb60b8a669..bda34fa045 100644 --- a/packages/backend/Dockerfile +++ b/packages/backend/Dockerfile @@ -11,8 +11,6 @@ FROM node:16-bullseye-slim -WORKDIR /app - # Install sqlite3 dependencies. You can skip this if you don't use sqlite3 in the image, # in which case you should also move better-sqlite3 to "devDependencies" in package.json. RUN apt-get update && \ @@ -20,16 +18,20 @@ RUN apt-get update && \ rm -rf /var/lib/apt/lists/* && \ yarn config set python /usr/bin/python3 +# From here on we use the least-privileged `node` user to run the backend. +USER node +WORKDIR /app + # Copy repo skeleton first, to avoid unnecessary docker cache invalidation. # The skeleton contains the package.json of each package in the monorepo, # and along with yarn.lock and the root package.json, that's enough to run yarn install. -COPY yarn.lock package.json packages/backend/dist/skeleton.tar.gz ./ +COPY --chown=node:node yarn.lock package.json packages/backend/dist/skeleton.tar.gz ./ RUN tar xzf skeleton.tar.gz && rm skeleton.tar.gz RUN yarn install --frozen-lockfile --production --network-timeout 300000 && rm -rf "$(yarn cache dir)" # Then copy the rest of the backend bundle, along with any other files we might want. -COPY packages/backend/dist/bundle.tar.gz app-config.yaml ./ +COPY --chown=node:node packages/backend/dist/bundle.tar.gz app-config*.yaml ./ RUN tar xzf bundle.tar.gz && rm bundle.tar.gz CMD ["node", "packages/backend", "--config", "app-config.yaml"] diff --git a/packages/create-app/templates/default-app/packages/backend/Dockerfile b/packages/create-app/templates/default-app/packages/backend/Dockerfile index 8836ac7898..c926ba350b 100644 --- a/packages/create-app/templates/default-app/packages/backend/Dockerfile +++ b/packages/create-app/templates/default-app/packages/backend/Dockerfile @@ -11,8 +11,6 @@ FROM node:16-bullseye-slim -WORKDIR /app - # Install sqlite3 dependencies. You can skip this if you don't use sqlite3 in the image, # in which case you should also move better-sqlite3 to "devDependencies" in package.json. RUN apt-get update && \ @@ -20,16 +18,20 @@ RUN apt-get update && \ rm -rf /var/lib/apt/lists/* && \ yarn config set python /usr/bin/python3 +# From here on we use the least-privileged `node` user to run the backend. +USER node +WORKDIR /app + # Copy repo skeleton first, to avoid unnecessary docker cache invalidation. # The skeleton contains the package.json of each package in the monorepo, # and along with yarn.lock and the root package.json, that's enough to run yarn install. -COPY yarn.lock package.json packages/backend/dist/skeleton.tar.gz ./ +COPY --chown=node:node yarn.lock package.json packages/backend/dist/skeleton.tar.gz ./ RUN tar xzf skeleton.tar.gz && rm skeleton.tar.gz RUN yarn install --frozen-lockfile --production --network-timeout 300000 && rm -rf "$(yarn cache dir)" # Then copy the rest of the backend bundle, along with any other files we might want. -COPY packages/backend/dist/bundle.tar.gz app-config*.yaml ./ +COPY --chown=node:node packages/backend/dist/bundle.tar.gz app-config*.yaml ./ RUN tar xzf bundle.tar.gz && rm bundle.tar.gz CMD ["node", "packages/backend", "--config", "app-config.yaml", "--config", "app-config.production.yaml"]