Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize docker image size #91

Merged
merged 13 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 42 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,22 +1,51 @@
FROM node:21-slim as base
FROM node:21-alpine as base

EXPOSE 3000/tcp
WORKDIR /usr/app
COPY ./ ./
COPY ./package.json \
./package-lock.json \
./next.config.js \
./next-env.d.ts \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file does not exist as far as I can tell? So my test build of your branch is failing
Maybe I'm missing something

Copy link
Contributor

@justcallmelarry justcallmelarry Feb 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried creating an empty file with the name just to get further, and it seems like it's gitignored, so I guess this is some sort of local file that I don't have (and which would not be present should a GHA be introduced to build images automatically or similar).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, that seems like a generated file. I'll remove that line 👍

./tsconfig.json \
./tailwind.config.js \
./postcss.config.js ./
COPY ./prisma ./prisma
COPY ./src ./src

RUN apt update && \
apt install openssl -y && \
apt clean && \
apt autoclean && \
apt autoremove && \
RUN apk add --no-cache openssl && \
npm ci --ignore-scripts && \
npm install -g prisma && \
prisma generate
npx prisma generate

# env vars needed for build not to fail
ARG POSTGRES_PRISMA_URL
ARG POSTGRES_URL_NON_POOLING
ENV POSTGRES_PRISMA_URL=http://temporary.build.url
ENV POSTGRES_URL_NON_POOLING=http://temporary.build.url

ENV NEXT_TELEMETRY_DISABLED=1
RUN npm run build

ENTRYPOINT ["/usr/app/scripts/container-entrypoint.sh"]
RUN rm -r .next/cache

FROM node:21-alpine as runtime-deps

WORKDIR /usr/app
RUN apk add --no-cache zstd
COPY --from=base /usr/app/package.json /usr/app/package-lock.json ./
COPY --from=base /usr/app/.next ./.next
COPY --from=base /usr/app/prisma ./prisma
COPY ./public ./public

RUN npm ci --omit=dev --omit=optional --ignore-scripts && \
npx prisma generate

RUN tar cvf - . | zstd -o app.tar.zst

FROM node:21-alpine as runner

WORKDIR /usr/app
RUN apk add --no-cache zstd

COPY ./scripts ./scripts
COPY --from=runtime-deps /usr/app/app.tar.zst ./app.tar.zst

EXPOSE 3000/tcp

ENTRYPOINT ["/bin/sh", "/usr/app/scripts/container-entrypoint.sh"]
83 changes: 34 additions & 49 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
"ts-pattern": "^5.0.6",
"uuid": "^9.0.1",
"vaul": "^0.8.0",
"zod": "^3.22.4"
"zod": "^3.22.4",
"prisma": "^5.7.0"
},
"devDependencies": {
"@total-typescript/ts-reset": "^0.5.1",
Expand All @@ -70,7 +71,6 @@
"postcss": "^8",
"prettier": "^3.0.3",
"prettier-plugin-organize-imports": "^3.2.3",
"prisma": "^5.7.0",
"tailwindcss": "^3",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.3.3"
Expand Down
3 changes: 0 additions & 3 deletions scripts/build-image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ SPLIIT_VERSION=$(node -p -e "require('./package.json').version")

# we need to set dummy data for POSTGRES env vars in order for build not to fail
docker buildx build \
--no-cache \
--build-arg POSTGRES_PRISMA_URL=postgresql://build:@db \
--build-arg POSTGRES_URL_NON_POOLING=postgresql://build:@db \
-t ${SPLIIT_APP_NAME}:${SPLIIT_VERSION} \
-t ${SPLIIT_APP_NAME}:latest \
.
Expand Down
10 changes: 9 additions & 1 deletion scripts/container-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
#!/bin/bash
prisma migrate deploy

set -euxo pipefail

if [[ -f "app.tar.zst" ]]; then
zstd -d app.tar.zst -c | tar xf -
rm app.tar.zst
fi

npx prisma migrate deploy
npm run start