From 235803f68e685aae1b7d67bedb5517b137d907d6 Mon Sep 17 00:00:00 2001 From: Giovanni Ravalico <15946771+suddenlyGiovanni@users.noreply.github.com> Date: Tue, 5 Mar 2024 14:25:44 +0100 Subject: [PATCH] Build/dockerize web app (#576) Signed-off-by: Giovanni Ravalico <15946771+suddenlyGiovanni@users.noreply.github.com> --- .dockerignore | 3 ++- .github/workflows/deply.yml | 2 +- apps/web/Dockerfile | 50 ++++++++++++++++++++----------------- docker-compose.yml | 2 +- 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/.dockerignore b/.dockerignore index 8318e51db..41c7bd690 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,7 +1,8 @@ .idea/ .git/ +.github/ node_modules/ .DS_Store .dockerignore +docker-compose.yml Dockerfile -.github/ diff --git a/.github/workflows/deply.yml b/.github/workflows/deply.yml index 62fdc94b3..1ac1102bb 100644 --- a/.github/workflows/deply.yml +++ b/.github/workflows/deply.yml @@ -17,6 +17,6 @@ jobs: uses: superfly/flyctl-actions/setup-flyctl@master - name: 🚀Deploy to Production - run: flyctl deploy . --config ./apps/web/fly.toml --dockerfile ./apps/web/Dockerfile --remote-only + run: flyctl deploy . --config ./apps/web/fly.toml --dockerfile ./apps/web/Dockerfile --build-target production --remote-only env: FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} diff --git a/apps/web/Dockerfile b/apps/web/Dockerfile index 7d33f2e3e..b226c7d2a 100644 --- a/apps/web/Dockerfile +++ b/apps/web/Dockerfile @@ -1,56 +1,60 @@ -# base node image -FROM node:21-alpine3.19@sha256:d3271e4bd89eec4d97087060fd4db0c238d9d22fcfad090a73fa9b5128699888 AS base +FROM node:21-alpine3.19 AS base LABEL authors="suddenlyGiovanni" -# set for base and all layer that inherit from it ENV PNPM_HOME="/pnpm" ENV PATH="$PNPM_HOME:$PATH" -RUN apk add --no-cache libc6-compat +RUN corepack enable -RUN apk update +WORKDIR /home/node/ -RUN corepack enable +FROM base AS builder -COPY . /app +WORKDIR /home/node/builder -WORKDIR /app +RUN pnpm add --global turbo -#ENTRYPOINT ["sh"] +COPY . . +RUN turbo prune --docker @suddenly-giovanni/web -FROM base AS prod-deps +ENTRYPOINT ["sh"] -RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile +FROM base AS build -#ENTRYPOINT ["sh"] +WORKDIR /home/node/build +COPY .gitignore .gitignore -FROM base AS build +COPY --from=builder /home/node/builder/out/json/ . -RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile +RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile --prod=false + +# Build the project +COPY --from=builder /home/node/builder/out/full/ . ENV NODE_ENV production -RUN pnpm run build +RUN pnpm turbo build --filter=@suddenly-giovanni/web -#ENTRYPOINT ["sh"] +RUN pnpm --filter @suddenly-giovanni/web --prod deploy pruned -FROM base AS runner +FROM base AS production ENV NODE_ENV production -COPY --from=prod-deps /app/node_modules /app/node_modules -COPY --from=prod-deps /app/apps/web/node_modules /app/apps/web/node_modules -COPY --from=build /app/apps/web/build /app/apps/web/build +WORKDIR /home/node/code + +RUN apk add --no-cache dumb-init + +COPY --from=build --chown=node:node /home/node/build/pruned . +COPY --from=build --chown=node:node /home/node/build/apps/web/build ./build USER node EXPOSE 3000 -WORKDIR /app/apps/web - -CMD ["pnpm", "run", "start"] +CMD ["dumb-init", "pnpm", "run", "start"] diff --git a/docker-compose.yml b/docker-compose.yml index 9f45d87e6..05bc63737 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,7 +4,7 @@ services: build: context: . dockerfile: apps/web/Dockerfile - target: runner + target: production ports: - "3000:3000" init: true