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

Create Dockerfile to match Next.js example closely, use "standalone" output, and with 40% smaller image size. #355

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 5 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.git
.github
.env.example
.env.example
**/.github
**/node_modules
**/.next
Comment on lines +3 to +5
Copy link
Member

Choose a reason for hiding this comment

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

Why are we doing **/<folder> here? Wouldn't simply <folder> do the same thing?
Ref

Copy link
Author

Choose a reason for hiding this comment

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

I don't think that's true. Dockerignore works differently to gitignore and is a bit unintuitive because of that:

https://shisho.dev/blog/posts/how-to-use-dockerignore/#:~:text=Syntax%20difference%20between%20.dockerignore%20and%20.gitignore

All paths are relative to the root

Copy link
Member

Choose a reason for hiding this comment

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

I see. Surprising that this isn't mentioned in the official docker docs. Thank you for the reference.

calcom/.yarn/cache
61 changes: 0 additions & 61 deletions .env.example

This file was deleted.

14 changes: 0 additions & 14 deletions .github/workflows/scarf-data-export.yml

This file was deleted.

101 changes: 53 additions & 48 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,76 +1,81 @@
FROM node:18 as builder
FROM node:18-alpine AS base

WORKDIR /calcom
FROM base AS builder
RUN apk add --no-cache libc6-compat
RUN apk update
# Set working directory
WORKDIR /app
RUN yarn global add turbo
COPY calcom/. .
RUN turbo prune @calcom/web --docker

# Add lockfile and package.json's of isolated subworkspace
FROM base AS installer

ARG NEXT_PUBLIC_LICENSE_CONSENT
ARG CALCOM_TELEMETRY_DISABLED
ARG DATABASE_URL
ARG NEXTAUTH_SECRET=secret
ARG NEXT_PUBLIC_API_V2_URL
ARG CALENDSO_ENCRYPTION_KEY=secret
ARG MAX_OLD_SPACE_SIZE=4096
ARG MAX_OLD_SPACE_SIZE=8192
ARG NEXT_PUBLIC_WEBAPP_URL
ARG NEXT_PUBLIC_SENTRY_DSN

ENV NEXT_PUBLIC_WEBAPP_URL=http://NEXT_PUBLIC_WEBAPP_URL_PLACEHOLDER \
ENV NEXT_PUBLIC_WEBAPP_URL=$NEXT_PUBLIC_WEBAPP_URL \
NEXT_PUBLIC_LICENSE_CONSENT=$NEXT_PUBLIC_LICENSE_CONSENT \
CALCOM_TELEMETRY_DISABLED=$CALCOM_TELEMETRY_DISABLED \
DATABASE_URL=$DATABASE_URL \
DATABASE_DIRECT_URL=$DATABASE_URL \
NEXTAUTH_SECRET=${NEXTAUTH_SECRET} \
NEXT_PUBLIC_API_V2_URL=${NEXT_PUBLIC_API_V2_URL} \
CALENDSO_ENCRYPTION_KEY=${CALENDSO_ENCRYPTION_KEY} \
NODE_OPTIONS=--max-old-space-size=${MAX_OLD_SPACE_SIZE}

COPY calcom/package.json calcom/yarn.lock calcom/.yarnrc.yml calcom/playwright.config.ts calcom/turbo.json calcom/git-init.sh calcom/git-setup.sh ./
COPY calcom/.yarn ./.yarn
COPY calcom/apps/web ./apps/web
COPY calcom/packages ./packages
COPY calcom/tests ./tests
NODE_OPTIONS=--max-old-space-size=${MAX_OLD_SPACE_SIZE} \
NEXT_PUBLIC_SENTRY_DSN=${NEXT_PUBLIC_SENTRY_DSN}

RUN yarn config set httpTimeout 1200000 && \
npx turbo prune --scope=@calcom/web --docker && \
yarn install && \
yarn db-deploy && \
yarn --cwd packages/prisma seed-app-store
RUN apk add --no-cache libc6-compat
RUN apk update
WORKDIR /app

RUN yarn turbo run build --filter=@calcom/web
# First install the dependencies (as they change less often)
COPY .gitignore .gitignore
COPY --from=builder /app/out/json/ .
COPY --from=builder /app/out/yarn.lock ./yarn.lock

# RUN yarn plugin import workspace-tools && \
# yarn workspaces focus --all --production
RUN rm -rf node_modules/.cache .yarn/cache apps/web/.next/cache

FROM node:18 as builder-two
# app-store packages aren't explicitly required but need to be available
COPY calcom/packages ./packages

WORKDIR /calcom
ARG NEXT_PUBLIC_WEBAPP_URL=http://localhost:3000
RUN yarn install

ENV NODE_ENV production
# Build the project
COPY --from=builder /app/out/full/ .

COPY calcom/package.json calcom/.yarnrc.yml calcom/yarn.lock calcom/turbo.json ./
COPY calcom/.yarn ./.yarn
COPY --from=builder /calcom/node_modules ./node_modules
COPY --from=builder /calcom/packages ./packages
COPY --from=builder /calcom/apps/web ./apps/web
COPY --from=builder /calcom/packages/prisma/schema.prisma ./prisma/schema.prisma
COPY scripts scripts
# Disable linting and type checking in the next build
ENV CI=1

# Save value used during this build stage. If NEXT_PUBLIC_WEBAPP_URL and BUILT_NEXT_PUBLIC_WEBAPP_URL differ at
# run-time, then start.sh will find/replace static values again.
ENV NEXT_PUBLIC_WEBAPP_URL=$NEXT_PUBLIC_WEBAPP_URL \
BUILT_NEXT_PUBLIC_WEBAPP_URL=$NEXT_PUBLIC_WEBAPP_URL
RUN yarn turbo run build --filter=@calcom/web...

RUN scripts/replace-placeholder.sh http://NEXT_PUBLIC_WEBAPP_URL_PLACEHOLDER ${NEXT_PUBLIC_WEBAPP_URL}
FROM base AS runner
WORKDIR /app

FROM node:18 as runner
# Don't run production as root
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
USER nextjs

COPY --from=installer /app/apps/web/next.config.js .
COPY --from=installer /app/apps/web/package.json .

WORKDIR /calcom
COPY --from=builder-two /calcom ./
ARG NEXT_PUBLIC_WEBAPP_URL=http://localhost:3000
ENV NEXT_PUBLIC_WEBAPP_URL=$NEXT_PUBLIC_WEBAPP_URL \
BUILT_NEXT_PUBLIC_WEBAPP_URL=$NEXT_PUBLIC_WEBAPP_URL

ENV NODE_ENV production
EXPOSE 3000
# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/.next/standalone ./
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/.next/static ./apps/web/.next/static
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/public ./apps/web/public

HEALTHCHECK --interval=30s --timeout=30s --retries=5 \
CMD wget --spider http://localhost:3000 || exit 1
RUN yarn global add prisma
COPY calcom/packages/prisma/migrations/. ./prisma/migrations
COPY calcom/packages/prisma/schema.prisma ./prisma/schema.prisma

CMD ["/calcom/scripts/start.sh"]
# TODO: Consider adding seeding script here
CMD ["sh", "-c", "$(yarn global bin)/prisma migrate deploy --schema=prisma/schema.prisma && node apps/web/server.js"]
1 change: 0 additions & 1 deletion Dockerfile.render

This file was deleted.

67 changes: 0 additions & 67 deletions docker-compose.yaml

This file was deleted.

26 changes: 0 additions & 26 deletions render.yaml

This file was deleted.

16 changes: 0 additions & 16 deletions scripts/replace-placeholder.sh

This file was deleted.

11 changes: 0 additions & 11 deletions scripts/start.sh

This file was deleted.

Loading