From ed7b51eea5fa916c9f2bea6e97417d7e4147ade7 Mon Sep 17 00:00:00 2001 From: Anatoli Nicolae Date: Thu, 21 Mar 2024 14:44:14 +0100 Subject: [PATCH] Move container image in own Dockerfile Signed-off-by: Anatoli Nicolae Fix workflow property Signed-off-by: Anatoli Nicolae --- .github/workflows/build.yml | 1 + Dockerfile | 7 ++--- Dockerfile.image | 53 +++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 Dockerfile.image diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 0570e87e8..3e8b8a7ab 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -62,6 +62,7 @@ jobs: uses: docker/build-push-action@v5.1.0 with: context: . + file: Dockerfile.image platforms: linux/amd64,linux/arm64 push: true # Note: provenance fixes unknown/unknown platform to be generated on build diff --git a/Dockerfile b/Dockerfile index 409b1d61c..f2d3ec6d4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -41,12 +41,13 @@ ENV NODE_ENV="production" COPY --from=production-deps /myapp/node_modules /myapp/node_modules COPY --from=build /myapp/node_modules/.prisma /myapp/node_modules/.prisma +COPY --from=build /myapp/node_modules/prisma /myapp/node_modules/prisma COPY --from=build /myapp/app/database /myapp/app/database COPY --from=build /myapp/build /myapp/build COPY --from=build /myapp/public /myapp/public COPY --from=build /myapp/package.json /myapp/package.json -COPY --from=build /myapp/docker-entrypoint.sh /myapp/docker-entrypoint.sh -RUN chmod +x /myapp/docker-entrypoint.sh +COPY --from=build /myapp/start.sh /myapp/start.sh +RUN chmod +x /myapp/start.sh -ENTRYPOINT [ "/myapp/docker-entrypoint.sh" ] +ENTRYPOINT [ "/myapp/start.sh" ] diff --git a/Dockerfile.image b/Dockerfile.image new file mode 100644 index 000000000..37916e203 --- /dev/null +++ b/Dockerfile.image @@ -0,0 +1,53 @@ +# base node image +FROM node:20-bookworm-slim as base + +# set for base and all layer that inherit from it +ENV NODE_ENV="production" + +WORKDIR /myapp + +# Install openssl for Prisma +RUN apt-get update && apt-get install -y openssl + +# Install all node_modules, including dev dependencies +FROM base as deps + +ADD package.json ./ +RUN npm install --production=false + +# Setup production node_modules +FROM base as production-deps + +COPY --from=deps /myapp/node_modules /myapp/node_modules +ADD package.json ./ +RUN npm prune --production + +# Build the app +FROM base as build + +COPY --from=deps /myapp/node_modules /myapp/node_modules + +ADD /app/database ./app/database +RUN npx prisma generate + +ADD . . +RUN npm run build + +# Finally, build the production image with minimal footprint +FROM base + +ENV PORT="8080" +ENV NODE_ENV="production" + +COPY --from=production-deps /myapp/node_modules /myapp/node_modules +COPY --from=build /myapp/node_modules/.prisma /myapp/node_modules/.prisma +COPY --from=build /myapp/node_modules/prisma /myapp/node_modules/prisma +COPY --from=build /myapp/app/database /myapp/app/database + +COPY --from=build /myapp/build /myapp/build +COPY --from=build /myapp/public /myapp/public +COPY --from=build /myapp/package.json /myapp/package.json +COPY --from=build /myapp/docker-entrypoint.sh /myapp/docker-entrypoint.sh +RUN chmod +x /myapp/docker-entrypoint.sh + +ENTRYPOINT [ "/myapp/docker-entrypoint.sh" ]