Deploy Node.js on Docker

Share this video with your friends

Send Tweet

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.

Adam
Adam
~ 8 years ago

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).

Will Button
Will Button(instructor)
~ 8 years ago

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.

Alexandre Rodriguez
Alexandre Rodriguez
~ 8 years ago

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 !

Will Button
Will Button(instructor)
~ 8 years ago

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

Alexandre Rodriguez
Alexandre Rodriguez
~ 8 years ago

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/

Will Button
Will Button(instructor)
~ 8 years ago

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!

Euan Millar
Euan Millar
~ 8 years ago

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?

Will Button
Will Button(instructor)
~ 8 years ago

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!

John Michelin
John Michelin
~ 7 years ago

I am getting a permissions error when I attempt to chown on a mac.

chown: changing ownership of '/home/nodejs/app/.gitignore': Operation not permitted
chown: changing ownership of '/home/nodejs/app/package.json': Operation not permitted
chown: changing ownership of '/home/nodejs/app/server.js': Operation not permitted
chown: changing ownership of '/home/nodejs/app': Operation not permitted

Any ideas?