DEV Community

Cover image for Next.js app deployed with Docker - does it make sense?

Next.js app deployed with Docker - does it make sense?

Francesco Ciulla on November 30, 2023

Hi, I am Francesco Ciulla, and I used Docker for ~10 years and Next.js for 5+ years. In this article, I want to show you how you can dockerize a N...
Collapse
 
mjoycemilburn profile image
MartinJ

Thanks for this. I've used Vercel 's deployment procedure myself and echo your thoughts on its excellence. My problem is that I've a big investment in Google Cloud hosting and don't want to switch. So, just now, if I wanted to use Next.js (which I do), I'd be a customer of the Docker intricacies that you describe so well.

But recently I've been looking at Svelte where to my amazement that I can use an "adapter" to build and deploy a Docker container to Google Cloud Run with just two commands.

I'm left wondering why Next.js have made things so difficult in this area.

Collapse
 
brianmcbride profile image
Brian McBride

You are wondering why a company that sells hosting services has made an open source platform that they manage easier to deploy on their platform, yet harder on others?

How about the features that only work on Vercel deployments?

I think it is pretty easy to see the "why" here. But just in case, I can represent it with one character: $

Collapse
 
francescoxx profile image
Francesco Ciulla

This is not a surprise to me, they are here for the money and it's fine. but for sure it's not the only way to deploy a frontend js app.

Collapse
 
mjoycemilburn profile image
MartinJ • Edited

Yes, I had the same thought - just didn't like to say it! But if this is really the case, who can one blame them? As Elon Musk would say - "I've got bills to pay"

Collapse
 
preciousnwaoha profile image
Precious Nwaoha

Not sure about this - compare yourself.
youtu.be/KNeJI61QVqg?si=UZvJwAhYid...

Collapse
 
royra profile image
Roy Razon

To me the Docker approach makes more sense if you have more than a single service, or some persistence engine like a DB or Redis.

Collapse
 
francescoxx profile image
Francesco Ciulla

same here

Collapse
 
brianmcbride profile image
Brian McBride

My experience using Astro over the last few months is a breath of fresh air compared to Vercel's Next.js.

Part of my motivation to move away from Next.js was due to features that felt like a Vercel vendor-lockin. Financially, Vercel is fine for the individual, but their pricing scales poorly in enterprise. Most larger companies also have their cloud provider in place and don't want to take on another. While you can containerize Next.js like any other app, some features won't work like they do on Vercel.

Beyond that, there are serious concerns with Next.js. In the past, I've had Next.js building modify my tsconfig files. I find it a bit shocking to have a framework modify setup/config files without asking first. I've also discovered that the React installed isn't the same as that runs in the Next.js runtime. Moreso, it was a beta version of React.

I feel strongly that libs shouldn't adjust config files and should run the versions we pin in our package.json files.

That drove me to Astro where, once past the learning curve, I found myself enjoying front-end dev a lot more.

Collapse
 
francescoxx profile image
Francesco Ciulla

maybe I should make a video on Astro with Docker, too

Collapse
 
kubilayozisik profile image
Kubilay Özışık

Thank you for the article. I have a middleware in my app and it looks like this image doesn't support it. I am doing some redirections and rewrites. Any idea how to solve it?

Failed to proxy http://localhost:3000/site Error: socket hang up
at connResetException (node:internal/errors:720:14)
at Socket.socketCloseListener (node:_http_client:474:25)
at Socket.emit (node:events:529:35)
at TCP.<anonymous> (node:net:350:12)
at TCP.callbackTrampoline (node:internal/async_hooks:128:17) {
code: 'ECONNRESET'
}

Collapse
 
pradumnasaraf profile image
Pradumna Saraf

Awesome blog, @francescoxx 👏👏

Collapse
 
francescoxx profile image
Francesco Ciulla

let's go @pradumnasaraf

Collapse
 
mhm13dev profile image
Mubashir Hassan

Has anyone tried to deploy Next.js 14 Docker Image on AWS ECS? Do you face any errors after deployment?

Collapse
 
francescoxx profile image
Francesco Ciulla

not yet, but it should be fine.

Collapse
 
mhm13dev profile image
Mubashir Hassan • Edited

After the deployment is complete, I get error cannot resolve hostname

Only happens on versions > Next 13.4

For older it works fine.

Collapse
 
cb2023 profile image
Charles Brown

Hello Francesco Ciulla,

Thank you for the comprehensive guide on deploying Next.js with Docker. The code snippets are valuable, but providing explanations for each segment and offering insights into when to use specific configurations would enhance the tutorial's clarity. Including considerations on when this approach is most suitable would be especially helpful for readers seeking guidance on decision-making.

Thank you

Collapse
 
francescoxx profile image
Francesco Ciulla

a video about dockerfile is coming on youtube this week.

Collapse
 
shricodev profile image
Shrijal Acharya

Well explained. Great blog, @francescoxx ! 🎉🎉

Collapse
 
francescoxx profile image
Francesco Ciulla

you are welcome

Collapse
 
ajackti profile image
DangTrong

Your guide is so helpful. But I want to import variables, which way can I do it? Thank you.

Collapse
 
francescoxx profile image
Francesco Ciulla

Thanks for the feedback! 😊 If you want to import environment variables in your Next.js app with Docker, you can add them to a .env file and ensure they're available in both your app and Docker container. In Next.js, make sure variables you want to access in the frontend are prefixed with NEXT_PUBLIC_. When using Docker, you can pass these variables with a docker-compose.yml file or directly in the Dockerfile. Let me know if you need more details!

Collapse
 
bullbiased profile image
Bull

Awesome.

Collapse
 
francescoxx profile image
Francesco Ciulla

thank you!

Collapse
 
reza_asgary_4a7ec7f3fd3e0 profile image
reza asgary

how about the most important part how to build once deploy multiple times there is no dynamic env how can i do that?

Collapse
 
koladev profile image
Mangabo Kolawole

Saving this article, I wanted to optimize the way I integrate Docker into my front-end applications. It will surely help.

Collapse
 
francescoxx profile image
Francesco Ciulla

stay tuned and check my youtube channel then