DEV Community

How to Dockerize a Next.js Application

Shanu on August 03, 2024

Setting up a Next.js application with Docker can greatly enhance your development workflow by ensuring consistent environments and simplifying depl...
Collapse
 
samurai71 profile image
Mark Landeryou

I'm currently looking at learning docker however what are the advantages of using docker ?

Collapse
 
ezpieco profile image
Ezpie

quite a lot, I will use a scenario to explain, imagine you have to maintain legacy code, it uses node version 14, but as we all know node has skyrocketed to version 22.5.1, now how are you going to maintain legacy code? Well you could just install node 14, but that would mean you would need to get rid of node 22, to overcome this, you could just use docker, docker would create a kind of computer on your computer that has node version 14 installed in it with the source code, like this you can run the legacy app without having version conflicts.

A more practical example would be having to run a whole backend, imagine you are a frontend engineer, you need to use the backend, fully(which is the worst thing), now the backend has a lot of dependencies, python, rust, java, all 3 of them are being used, why? Because your backend team decided to go microservice that's why, you need the APIs and functionality, how are you going to get that? Docker it! You can install docker and docker will run each microservice as per your backend teams configurations, all you have to do, is run the container and hoping that your backend team has made proper configs which are automated, you won't need to do much work.

Collapse
 
ashishsimplecoder profile image
Ashish Prajapati

i understand it now clearly

Collapse
 
samurai71 profile image
Mark Landeryou

Thanks that makes sense . I love the scenario it helped me see the use

Collapse
 
shanu001x profile image
Shanu

Excellent examples! Docker truly shines when dealing with legacy code and complex backend setups. Using Docker to manage different Node versions avoids conflicts, and for microservices, it simplifies running and integrating diverse dependencies. These scenarios highlight Docker’s flexibility and efficiency in handling various development and operational challenges. Thanks for illustrating these use cases!

Thread Thread
 
weaponsforge profile image
Weapons Forge

Bonus points! I love how it greatly helps code portability and consistency across environments. Another thing I liked about it is, it provides a layer of protection against supply chain attacks when installing NPM (or Python) dependencies—they are installed in the container instead of directly on my development PC or laptap.

Collapse
 
denys_bochko profile image
Denys Bochko

I think one of the main advantages is that you can run identical containers for local dev and production.
I am coming from backend background and a lot of times when the production has different updates than the local environment, that's a big problem.
You can either create a docker container from the same dockerfile locally and in production or create it ones and then use in multiple places.

As it was mentioned before version testing is another great feature, that I actually used.

Collapse
 
samurai71 profile image
Mark Landeryou

I was looking at learning it however know I will learn it. However now I have a question say you are developing on local machine which is windows but server is Linux can docker run Linux without installing it?

Thread Thread
 
denys_bochko profile image
Denys Bochko

You are asking the wrong question. Docker is a technology that allows you to run specific services inside of your containers. You are not looking to run Linux or ubuntu, you are looking to run what you need to have: apache server, nodejs, react or python, mysql, postress, you name it.
Then you look to see if there are already existing docker images with for that kind of development. Most likely there is. So you just use it to run a container using that image on your local machine.

Windows runs docker, so you can run whatever service you need to run in docker on windows. You can run a lot of things in docker without installing them locally.

Thread Thread
 
samurai71 profile image
Mark Landeryou

Oh okay my apologies I am trying to get this and learn

Thread Thread
 
denys_bochko profile image
Denys Bochko

I suggest taking a course on docker. one on udemy from Academind helped me. You will get a much clear picture of the technology and how to use it.

Collapse
 
shanu001x profile image
Shanu

Docker provides consistency across environments, isolation of applications, and easy portability. It also allows for efficient resource use and simplifies dependency management.

Collapse
 
ed1nh0 profile image
Edson Jr.

I'm a frontend developer and I'm using Vercel to deploy simple NextJS apps. As for learning purposes I think using Docker isn't necessary, am I right? In what scenario or case should I use it? Thanks in advance.

Collapse
 
denys_bochko profile image
Denys Bochko

It depends. I am using docker to develop vue.js apps. The advantage of using docker is that I don't have to install node locally on my machine, it's all done in docker containers.
It is better for organization purposes as you can setup a docker-compose which serves as a separate network to host your db, api and frontend. That separates it from the rest of your projects, so there is no mess. You can also use docker with the same project to upgrade node or any framework you are using keeping the original project intact and trying the new things in a separate docker container.

Collapse
 
shanu001x profile image
Shanu

Great points! Docker indeed offers fantastic benefits for development, such as avoiding local dependencies and maintaining a clean and organized environment. The use of docker-compose for managing different components and the flexibility to upgrade frameworks without affecting your main setup are invaluable. Thanks for sharing your experience with Docker for Vue.js apps!

Collapse
 
ed1nh0 profile image
Edson Jr.

Well as per Vercel's documentation, it does not support running docker instances, but I can run it locally for practice purposes. Thank you.

vercel.com/guides/does-vercel-supp...

Thread Thread
 
denys_bochko profile image
Denys Bochko

I think there are two questions merged in your post. For learning purposes using Docker with your Nextjs app is a great idea as Docker is being asked by many companies as it is a great containerization tool. One of the main features of docker is that you can have local dev and prod dev using the same container setup which would improve quality of your releases.
Vercel is a cloud offering so of course it does not support docker as it is a platform on its own. I would not be surprised if they themselves use docker. It is similar to other hosting providers but one that already has Nodejs preinstalled. So, it's no surprise it does not support docker.
I heard about Vercel before, but I prefer to have the local dev done on the local machine, so I can even when there is no internet and have my code in git.
It is simply a matter of preference. :)

Thread Thread
 
ed1nh0 profile image
Edson Jr.

Well explained! Thanks a lot for your attention!

Collapse
 
denys_bochko profile image
Denys Bochko

I have a similar guide on github going a bit further with automation so you don't have to work with docker file, but simply setup your project name and port to use on your app and it will create the containers and create files to run them.

Collapse
 
shanu001x profile image
Shanu

That sounds fantastic! Automation can really simplify the process. Could you share the link to your guide? I'd love to check it out and learn more about your approach.

Collapse
 
denys_bochko profile image
Denys Bochko
Collapse
 
joseph_gitonga_08483a40cf profile image
Joseph Gitonga

This is OK for non-production app. However, it's good to consider optimization via multi-step build, running the app as non-root user, using pm2 for running the app, among other best practices ✅

Collapse
 
shanu001x profile image
Shanu

Thank you for your feedback! You’re absolutely right. For a production-ready setup, optimizing the Dockerfile with multi-stage builds, running the application as a non-root user, and using tools like PM2 are indeed best practices that enhance performance, security, and maintainability. I’ll consider incorporating these points in future updates or articles. Thanks for contributing to the discussion!

Collapse
 
shusain_d54c3f4df3007d80 profile image
s.husain

yrs