Menu

Post image 1
Post image 2
1 / 2
0

Dockerizing Next.js for production

DEV Community·Mahmoud Mokaddem·about 1 month ago
#jsoV5N8U
#stage#common#nextjs#next#build#copy
Reading 0:00
15s threshold

Most Dockerfiles for Next.js you'll find online ship a 1.2 GB image, leak environment variables at build time, and rebuild every layer on a one-line change. They work on the demo. They don't work in production. This is the Dockerfile I actually run. Multi-stage, ~150 MB final image, build-time and runtime env vars cleanly separated, layer caching that survives a package.json change. I'll walk through every line, explain why each stage exists, and call out the four gotchas that account for most "it worked locally" production failures. The full setup (Dockerfile plus docker-compose, GitHub Actions deploy pipeline, auth, testing) is in a production-grade Next.js + NestJS starter I'm building. Free for email subscribers — subscribe at mahmoud-mokaddem.com . The Dockerfile, up front If you're in a hurry, copy this and skip to Common gotchas . The rest of the post explains every line.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More