DEV Community

Cover image for How To Build a Node.js Application with Docker [Quickstart]

How To Build a Node.js Application with Docker [Quickstart]

katjuell on February 17, 2020

Introduction This tutorial will walk you through creating an application image for a static website that uses the Express framework and ...
Collapse
 
karataev profile image
Eugene Karataev

Thanks for the great introduction to Docker!

If you follow the tutorial and like me have the error
Error response from daemon: Dockerfile parse error line 4: unknown instruction: /HOME/NODE/APP
during docker build, then replace lines 3,4 in Dockerfile with one line.

From

RUN mkdir -p /home/node/app/node_modules && chown -R node:node 
/home/node/app

to

RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app
Collapse
 
katjuell profile image
katjuell

Looks like that was a formatting error in the markdown – should be fixed now. That instruction is definitely all one line. Thanks for catching!

Collapse
 
chrisme profile image
Chris

To keep the image as small as possible you can delete the cache npm builds

RUN npm install --production && npm cache clean --force

And by putting "fix" settings that don't change, like

EXPOSE 8080

towards the beginning of the Dockerfile you can sometimes save build time because docker can build upon existing layers that didn't change.

Collapse
 
gergelypolonkai profile image
Gergely Polonkai

If you can install podman, you no longer need a sudo account as podman can work without root access. It works just like docker, using the very same parameters. You can even alias docker=podman.

Also, why creating package.json by hand? npm init can create it for you so you wonʼt suffer from possible typos.

Collapse
 
ltroya profile image
Luis Troya • Edited

Thanks for sharing your knowledge!

One thing I am curious about, why is there two COPY on Dockerfile? Is it the first one neccesary?

COPY . .

COPY --chown=node:node . .
Collapse
 
katjuell profile image
katjuell

Hi! That is a typo – thanks for catching! I've updated the code above.

Collapse
 
birkoff profile image
Boris Petrov

thank you!

Collapse
 
menaiala profile image
Menai Ala Eddine

Do we make static websites with express?

Collapse
 
new_blicio_us profile image
New.blicio.us

Thanks for the easy to understand guide. I needed this for an upcoming project.