DEV Community

Cover image for Mastering Docker for Node.js: Advanced Techniques and Best Practices

Mastering Docker for Node.js: Advanced Techniques and Best Practices

David Chibueze Ndubuisi on March 17, 2023

Introduction In today's fast-paced software development environment, containerization has become a popular and effective way to package ...
Collapse
 
lissy93 profile image
Alicia Sykes • Edited

This is a really nicely explained article David :) Take my Unicorn 🦄
I would have loved a guide like this a few years ago, when I was Dockerizing my first application!

One final step that could be good to also mention (but maybe a bit out of scope) is distributing images on a registry like DockerHub / GHCR. Either through the UI, via CLI or using something like GitHub Actions to build, tag and publish images to registries, ready for direct consumption by users.

Collapse
 
davydocsurg profile image
David Chibueze Ndubuisi

Thank you so much, Alicia. I'm glad you found the article helpful!

And you're absolutely right about the importance of distributing images on a registry like DockerHub or GHCR. That step is definitely worth mentioning, and I appreciate you bringing it up. It can be a bit overwhelming at first, but it's an essential part of the process, especially for teams working on collaborative projects.

Thanks again for reading and commenting on my article. I appreciate your feedback!

Collapse
 
baljitweb profile image
Baljitweb • Edited

After docker compose up
I checked in postman with mentioned json payload.\
It is NOT working
Did we miss any step?
Even localhost8080 also not working and postman send error as :

`



Error

Cannot POST /api/auth/register

`

Collapse
 
davydocsurg profile image
David Chibueze Ndubuisi

The URL is not correct, it should be: localhost:8080/api/register

Collapse
 
baljitweb profile image
Baljitweb • Edited

But you had mentioned in your blog:
"To test the endpoints, send a POST request to register endpoint: localhost:8080/api/auth/register with the following JSON payload."

Collapse
 
davydocsurg profile image
David Chibueze Ndubuisi • Edited

Did your MongoDB connection establish successfully?
Also, check if you missed any steps.

Collapse
 
baljitweb profile image
Baljitweb • Edited

yes:
console.log is
node-with-docker-app-1 | MongoDB connected

Also I checkout github repo [github.com/davydocsurg/docker-node...] and try docker compose up from this repo.
same error.

Thread Thread
 
davydocsurg profile image
David Chibueze Ndubuisi

Can you provide a screenshot of the error?

Thread Thread
 
baljitweb profile image
Baljitweb

Sure...

Image description

Thread Thread
 
ashishbeetle profile image
Ashish Binu

your post url is incorrect. you can either change it to : localhost:8080/api/register
or
change your routes/auth.js routes to:
/auth/login and
/auth/register

Thread Thread
 
wilsonibekason profile image
Wilson Ibekason • Edited

@baljitweb Have a look at your setup as well
Image description

Collapse
 
muzzammil194 profile image
Muzzammil Shaikh

Nice content easy to understand the code line by line it's especially for beginners

Collapse
 
wilsonibekason profile image
Wilson Ibekason

This is really amazing and straight forward, I had issues with containerizing my docker to Nodejs, but this your article helped me resolve it. I would also recommend a typescript version

Collapse
 
davydocsurg profile image
David Chibueze Ndubuisi

Thank you @wilsonibekason. I'm glad you found this helpful.

Collapse
 
william_glasse_a132326a1b profile image
William Glasse

Really great article! This was a nice refresher for me as I'm getting back into web development after a break.

My only thought was that your demonstration doesn't highlight a multi-stage build. You speak to the benefits of why we'd write a multi-stage build, but you'd need to use multiple images for it to be multi-stage.

# Stage 1: Build the application
FROM node:14 AS build
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
RUN npm run build

# Stage 2: Copy the built application to a smaller base image
FROM node:14-alpine
WORKDIR /app
COPY --from=build /app/dist /app
CMD ["npm", "start"]
Enter fullscreen mode Exit fullscreen mode

In this example, the first stage builds the application using the official Node.js image, and the second stage copies only the built files to a smaller Node.js Alpine image. The --from=build option in the COPY instruction tells Docker to copy files from the first stage of the build.

Collapse
 
bohooslav profile image
Богуслав

Well, I was looking for ways to optimize build of my app in the cloud, and you said that making the build multi-stage helps with that, but in your example did everything in just one stage. No examples on how one could use the FROM directive.

Collapse
 
alexagc profile image
Alejandro Gomez Canal

I would check the dockerfile, you use multi-stage builds but only use one stage so you are not using the benefits of multi-stage caching images builds