Requires Next.js to be configured to output a standalone bundle. See more here.
- Required system packages are cached during the build stage.
- Next.js builds are cached during the build stage.
- Next.js telemetry is disabled by default.
| # syntax = docker/dockerfile:1 | |
| # Adjust BUN_VERSION as desired | |
| ARG BUN_VERSION=1.1.1 | |
| FROM oven/bun:${BUN_VERSION}-slim as base | |
| LABEL fly_launch_runtime="Next.js" | |
| # Next.js app lives here | |
| WORKDIR /app | |
| # Set production environment | |
| ENV NEXT_TELEMETRY_DISABLED="1" \ | |
| NODE_ENV="production" | |
| # Throw-away build stage to reduce size of final image | |
| FROM base as build | |
| # Install packages needed to build node modules | |
| RUN --mount=type=cache,id=cache,target=/var/cache/apt \ | |
| --mount=type=cache,id=lib,target=/var/lib/apt \ | |
| apt-get update -qq && \ | |
| apt-get install --no-install-recommends -y build-essential ca-certificates curl pkg-config python-is-python3 | |
| # Install node modules | |
| COPY --link bun.lockb package.json ./ | |
| RUN --mount=type=cache,id=bun,target=/root/.bun \ | |
| bun install --frozen-lockfile | |
| # Copy application code | |
| COPY --link . . | |
| # Include Next.js cache | |
| RUN mkdir -p /app/.next/cache | |
| # Build application | |
| RUN --mount=type=cache,id=nextjs,target=/app/.next/cache \ | |
| bun run build | |
| # Final stage for app image | |
| FROM base AS run | |
| # Copy built application | |
| COPY --from=build /app/.next/standalone ./ | |
| COPY --from=build /app/.next/static ./.next/static | |
| # Start the server by default, this can be overwritten at runtime | |
| EXPOSE 3000 | |
| CMD ["bun", "run", "server.js"] |