Next.js Course
Next.js
/
Advanced

Docker

Definition

A platform used to package an application and all its dependencies into a standardized, isolated container, ensuring it runs exactly the same on any computer or cloud provider.

Explain Like I'm New

The classic developer excuse is: 'It works on my machine!' Docker fixes this. You put your Next.js app, the specific Node.js version, and the environment variables into a 'Container'. You give that container to AWS, Google Cloud, or your coworker, and it is guaranteed to run perfectly.

Real World Example

A company policy forbids using Vercel. You must deploy to AWS ECS. You write a `Dockerfile` that builds the Next.js app, wraps it in a lightweight Linux environment, and deploys it to the AWS cloud.

Common Use Cases

  • •Self-hosting
  • •Cloud-agnostic deployments
  • •Microservices

Terminal Output

bash / terminal
# A basic Dockerfile for Next.js # 1. Use the official Node image FROM node:18-alpine AS runner WORKDIR /app # 2. Copy the standalone build generated by Next.js # (Requires output: 'standalone' in next.config.js) COPY .next/standalone ./ COPY .next/static ./.next/static COPY public ./public # 3. Expose the port EXPOSE 3000 # 4. Start the Node.js server CMD ["node", "server.js"]

Interview Questions

basic

  • What file do you create in the root of your project to define how the Docker container should be built?

intermediate

  • Why is Next.js 'Standalone Output' mode crucial when building Docker containers?

Flash Cards

Question

Which file?

Click to reveal answer
Answer

A `Dockerfile`.

Question

Standalone Output?

Click to reveal answer
Answer

By default, Next.js relies on the massive `node_modules` folder to run. If you set `output: 'standalone'` in `next.config.js`, Next.js traces exactly which files are needed and creates a tiny, self-contained folder. This reduces the Docker image size from 1 Gigabyte down to 100 Megabytes.