DEV Community

Cover image for Introduction to Docker for Javascript Developers (feat Node.js and PostgreSQL)

Introduction to Docker for Javascript Developers (feat Node.js and PostgreSQL)

Alex Eagleson on December 22, 2021

All code from this tutorial as a complete package is available in this repository. If you find this tutorial helpful, please share it with your fr...
Collapse
 
thereis profile image
Lucas Reis

Awesome article Alex! It really helped me on a very specific case :)

Collapse
 
slidenerd profile image
slidenerd

nice post! how to persist postgres data between container restarts?

Collapse
 
alexeagleson profile image
Alex Eagleson

you need volumes, this section should help answer:

dev.to/alexeagleson/docker-for-jav...

Collapse
 
prabhukadode profile image
Prabhu

Will try this out

Collapse
 
cyebukayire profile image
Peace

OMG! I should follow you for more quality content. Thank you Alex
You're so Great

Collapse
 
daryushn profile image
Daryush Nadim • Edited

Excellent tutorial, Alex! After a few years of having no interest in understanding containers, this tutorial finally explained their practical use coupled with a good example and now I want to learn more!

I have been trying to follow the example in the tutorial, but I'm having an issue. Basically, I opted not to install NPM on the host machine and want to have NPM only installed in the container. When I run the sample without the volume, everything works fine and I can access the Hello World web page, i.e. this works:

docker run -p 3000:8080 --name my-node-app-container my-node-app

However, when I try to run the sample with the volume "mapping", then the command fails, i.e. this does NOT work:

docker run -p 3000:8080 --name my-node-app-container --volume ${PWD}:/usr/src/app my-node-app

This is the output:

PS C:\docker-template> docker run -p 3000:8080 --name my-node-app-container --volume ${PWD}:/usr/src/app my-node-app

> server@1.0.0 start /usr/src/app
> nodemon server.js

sh: nodemon: not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! server@1.0.0 start: nodemon server.js
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the server@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?

npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2022-01-03T09_07_28_520Z-debug.log

What puzzles me is the "but node_modules missing, did you mean to install" message? Does this mean that since I am syncing my local host folder with the working folder in the container that the syncing ends up overwriting the potentially already built "node_modules" folder and hence it's missing? As mentioned, my local host folder (C:\docker-template) does not have node_modules because I opted not to install NPM on the host and was hoping to get away with NPM only in the container.

I am running Docker on Windows via Docker for Desktop and on WSL2. My C:\docker-template folder contains only 3 files: Dockerfile, package.json and server.js.

Collapse
 
alexeagleson profile image
Alex Eagleson

You bring up a good point there is an issue here when you don't have a node_modules directory in the source. Basically the "lack of the directory" gets overwritten on the volume mount.

Here is a stackoverflow discussion about the issue:

stackoverflow.com/a/32785014

It talks about it in the context of docker-compose but the same principle should apply without compose. The syntax for adding the additional volume would be:

docker run -p 3000:8080 --name my-node-app-container --volume  ${PWD}:/usr/src/app --volume /usr/src/app/node_modules my-node-app
Enter fullscreen mode Exit fullscreen mode
Collapse
 
timothydjones profile image
Tim Jones

Fantastic tutorial, Alex! I've tried several times to understand the various Docker concepts and why I'd want to use particular things like volumes and Docker Compose. It all became clear with your explanation. I really appreciate your effort and, especially, how you went step by step! Thank you!

Collapse
 
alexeagleson profile image
Alex Eagleson

That's the best result I could hope for, thank you for sharing your experience.

Collapse
 
drmaquino profile image
Mariano Aquino

This article is SO AWESOME!

Collapse
 
alexeagleson profile image
Alex Eagleson

Cheers friend.

Collapse
 
iamfelipy profile image
Felipy

Obrigado pela ótima explicação.

From Brazil

Collapse
 
teleinformatique profile image
Khadim Sarr

Thanks You Alex.

Collapse
 
sangeethsn profile image
Sangeeth Saseendran Nair • Edited

Hi Alex, Thanks for this great tutorial. But I’m getting an error after runinng "docker-compose up --build" attached below:

"Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
Attaching to for-interview-app-1, for-interview-postgres-1
Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:5432 -> 0.0.0.0:0: listen tcp 0.0.0.0:5432: bind: address already in use".

Could you please help me here?

Collapse
 
pavelloz profile image
Paweł Kowalski • Edited

Very much needed introduction in the front-end/JS world! Great job :)

Collapse
 
alexeagleson profile image
Alex Eagleson

Glad you enjoyed it. It's a really fantastic tool.

Collapse
 
protik111 profile image
Rafiur Rahman Protik

Worth reading as a beginner. Thanks Alex!