The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Deploy Node.js on Docker

Deploy Node.js on Docker

9:54
Getting nodejs to run inside of Docker is relatively simple. Getting nodejs to run inside of Docker while using recommended best practices takes some planning. In this lesson, I’ll show you how to get a simple nodejs web server running in Docker on your local workstation while adhering to best practices.
Watch this lesson now
Avatar
egghead.io

Getting nodejs to run inside of Docker is relatively simple. Getting nodejs to run inside of Docker while using recommended best practices takes some planning. In this lesson, I’ll show you how to get a simple nodejs web server running in Docker on your local workstation while adhering to best practices.

Avatar
Adam

I'm still a little confused as to why you needed to create a new volume for /home/nodejs/app/node_modules. You've already mounted the entire project directory which contains the node_modules you have installed locally. Why can't the container just use those? Also, You didn't specify a "matching" directory when you mounted the 2nd volume. What is this doing? From what I understand this is creating a directory locally that only the Docker engine has access to (well, for the most part).

Avatar
Will

You are correct.
The node_modules folder on my local workstation has modules compiled for OSX, but my docker host is running Linux. The new volume was needed to provide a location for the docker host to place the npm modules compiled for its OS (Linux in this case).
The 2nd volume is created locally on the docker host. Since it contains the npm modules compiled specifically for its OS, they aren't of any use outside of the docker host so I didn't expose it.

In reply to Adam
Avatar
Alexandre

Hi Will,

I needed to add : RUN mkdir $HOME/app after ENV NODE_ENV=production
in my Dockerfile, don't know if it's related to Docker version or something else but without I had a missing dir error when docker was running npm install on build

Best !

In reply to Will
Avatar
Will

Nice catch!
It may be that, or an oversight by the author when recording the lesson! :-D

In reply to Alexandre
Avatar
Alexandre

EDIT: Service 'app' failed to build: When using COPY with more than one source file, the destination must be a directory and end with a /

Need to replace:
COPY package.json npm-shrinkwrap.json $HOME/app
by
COPY package.json npm-shrinkwrap.json $HOME/app/

Avatar
Will

Yup, that was a typo on the screen during recording. The git repo with the lesson has the corrected trailing slash on the command. I'll update the lesson to correctly show that as well.
Thanks!

In reply to Alexandre
Avatar
Euan Millar

Hi, I'm At 5.25, I'm not following you when you describe how & why we should amend the "." volume path on the remote host. Are you able to explain this a bit more please? Is this a path within the docker container, and if so, do I need to mkdir in the the Dockerfile for the creation of that folder? Is that where the app will be installed in the container?

Avatar
Will

Hi, thanks for posting your question!
In the line .:/home/nodejs/app, we are telling docker to create a volume in the container. The syntax to do this is [source directory]:[destination directory], or stated another way take this stuff : and put it here. So in our example, the . refers to the current local directory. If I'm in the directory /home/foo, the . will refer to the contents of the foo directory. If I'm in/home/bar, the.will refer to the contents of the directorybar. The end result in our example is the contents of my application are created in the Docker container as/home/nodejs/app`.
Hope that answers your question!

In reply to Euan Millar
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?