Hi! I'm Francesco Ciulla, and I've used Docker for around 10 years and Next.js for over 5 years.
In this article, I’ll walk you through how to dockerize a Next.js app, and afterward, I’ll share my thoughts on whether it’s really necessary in most cases.
🎥 Prefer video? Watch it here (link)
💻 Code available for free on GitHub (link in video description)
Step 1: Create a Next.js App
Open your terminal, navigate to any folder, and run:
npx create-next-app@latest nextdocker
Follow the setup wizard and select your preferences.
Then open the project folder in your favorite IDE.
To run the app locally, use:
npm run dev
Visit http://localhost:3000 to see your running app.
Step 2: Dockerize the App
Update next.config.js
Replace the file contents with:
/** @type {import('next').NextConfig} */
const nextConfig = {
output: 'standalone'
};
module.exports = nextConfig;
Add a .dockerignore File
Create .dockerignore at the root with this content:
Dockerfile
.dockerignore
node_modules
npm-debug.log
README.md
.next
.git
Create the Dockerfile
At the root level, create a file named Dockerfile and add:
FROM node:18-alpine AS base
FROM base AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
elif [ -f package-lock.json ]; then npm ci; \
elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \
else echo "Lockfile not found." && exit 1; \
fi
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN yarn build
FROM base AS runner
WORKDIR /app
ENV NODE_ENV=production
RUN addgroup --system --gid 1001 nodejs && \
adduser --system --uid 1001 nextjs
COPY --from=builder /app/public ./public
RUN mkdir .next && chown nextjs:nodejs .next
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
EXPOSE 3000
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
CMD ["node", "server.js"]
Add Docker Compose File
Now create a docker-compose.yml:
version: '3.9'
services:
nextapp:
container_name: nextapp
image: nextapp
build: .
ports:
- "3000:3000"
Step 3: Build and Run the Container
First, build the image:
docker compose build
Then run the container:
docker compose up
Now open http://localhost:3000 again. You should see your app running, but this time it’s powered by Docker!
Step 4: Try Livecycle Deployment
As a final test, I used Livecycle to deploy the app. You can install the Livecycle extension directly in Docker Desktop. Once your app is running, you can turn it on and access the preview link in seconds — it’s super efficient.
Final Thoughts and Considerations
Let’s talk about when it makes sense to Dockerize a Next.js app.
👍 Why Docker with Next.js Can Be Useful
✅ Better security and isolation
✅ Easier scaling and deployment when used with other services
✅ Cleaner and reproducible DevOps workflow
✅ Fits well in microservices environments
👎 When It’s Probably Not Needed
❌ If you’re only deploying a single app
❌ If you're using Vercel — the deployment is already optimized
❌ Adds complexity without major benefit in solo projects
Considerations:
- Docker is a technology that can work on ANY application, including Next.js, of course.
- Next.js has a super cool way to deploy applications to make Docker ALMOST useless.
- Having Next.js running in a Docker container provides better security and dependability, faster and easier deployment procedures, and simpler application management.
- If you already have many Docker containers running, adding a new one is easier than using the Next.js deployment procedure (Vercel) to have complete DevOps control.
- If you have ONLY one Next.js application, I would suggest using the Next.js deployment procedure (Vercel).
So, do you need to use Docker with Next.js? Usually NOT, but it starts to make sense if you have multiple services running (and you use a tool like Livecycle).
This makes us think about how Docker is powerful. Docker doesn't care about the technology you use. It just works. With Docker, you can deploy ANY application, which is super powerful.
Kudos to Vercel for making the Next.js app deployment so easy, but I think Docker is still a great tool.
If you have questions or want to share your experience, leave a comment below.
Thanks for reading —
Francesco
Top comments (0)