Skip to content

Instantly share code, notes, and snippets.

@karlhorky
Forked from lithdew/Dockerfile
Created April 5, 2024 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karlhorky/5a97b3a6f5d4e0129745468d304707cc to your computer and use it in GitHub Desktop.
Save karlhorky/5a97b3a6f5d4e0129745468d304707cc to your computer and use it in GitHub Desktop.
Dockerfile for deploying a Next.js standalone bundle on Fly.io with Bun.

Deploy a Next.js app on Fly.io with Bun

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"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment