Skip to content

Instantly share code, notes, and snippets.

@lithdew
Created April 4, 2024 20:31
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save lithdew/f77509ce8c116fc22312d9beebfa3d26 to your computer and use it in GitHub Desktop.
Save lithdew/f77509ce8c116fc22312d9beebfa3d26 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"]
@deadcoder0904
Copy link

deadcoder0904 commented Apr 5, 2024

# 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

is this fly specific? why do you need it?

also, does next/image through error about sharp? i faced lots of issues while getting sharp to work next/image but finally found a working solution using pnpm by installing it separately.

@lithdew
Copy link
Author

lithdew commented Apr 10, 2024

@deadcoder0904 It's used for building dependencies that depend on node-gyp. It may be removed if you do not have such dependencies.

I'm not certain if this Dockerfile works with sharp as I generally manually implement image optimization in my Next.js projects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment