DEV Community

Cover image for Learn Docker - from the beginning, part II volumes

Learn Docker - from the beginning, part II volumes

Chris Noring on February 11, 2019

Follow me on Twitter, happy to take your suggestions on topics or improvements /Chris This article is part of a series: Docker — from the begin...
Collapse
 
neskhodovskiy profile image
Serhiy Neskhodovskiy • Edited

Hello Chris, thanks for the tutorial. I just noticed you didn't say anything about removing the "COPY . ." line from the Dockerfile after you have linked the volume. I assume there's no longer a need to copy the app into the container. Some would say "it's obvious" and some would say "who cares" since the volume is mounted later and overrides the directory anyway, but in my opinion a note on how to avoid redundant operations and keep things clean will make a nice addition to an otherwise great article.

Collapse
 
jplindstrom profile image
Johan Lindstrom

Do you not need the COPY . . for the docker build step? The volume is only mounted during docker run.

Collapse
 
thomasroest profile image
Thomas Roest • Edited

what about npm install in the Dockerfile? You don't need that either right? Isn't it a better idea to only mount a src directory?

Collapse
 
orphee profile image
orphee

As I'm running the app through DockerToolbox, I had to add the flag -L to nodemon, otherwise the listening part was not working

"scripts": {
    "start": "nodemon -L app.js",
Collapse
 
softchris profile image
Chris Noring

Thanks so much.. I must admit I haven't used Docker on Windows so it's great you are able to point out differences :)

Collapse
 
mrxcitement profile image
Mike Barker

This also applies to "Docker Desktop" running on macOS as well. More info here: github.com/remy/nodemon#applicatio...

Collapse
 
sygyzmundovych profile image
Vitaliy Rudnytskiy

Hi Chris. Thanks for the effort to write these nice tutorials. I'm not completely new to Docker, but still learned some new tricks :)

One additional suggestion is to call the command with volume option using " around $(pwd), like:
$ docker run -d -p 8000:3000 --name my-container --volume "$(PWD)":/app image-name

In my case the command from the tutorial was throwing an error

$ docker run -d -p 8000:3000 --name my-container --volume $(PWD):/app image-name
docker: invalid reference format.
See 'docker run --help'.

because the directory path had spaces in it. Do not ask me why, pls ;-)

Regards,
-Vitaliy

Collapse
 
softchris profile image
Chris Noring

oh wow.. Great tip Vitaly thanks.. I'll update the article :)

Collapse
 
goranpaunovic profile image
Goran Paunović

If you are on windows and using powershell, change $(pwd) to ${pwd}.

Collapse
 
tssidhu profile image
tssidhu • Edited

Very helpful article and thanks for taking time to put it together. I had a question about the last docker run command. Shouldn't that include an image name at the end? The version I see currently is:

docker run -d -p 8000:3000 --name my-container --volume $(PWD):/app
Enter fullscreen mode Exit fullscreen mode

But, when I use that in my machine, I get "docker run" requires at least 1 argument. Only way I was able to fix it was by adding the image name at the end.

docker run -d -p 8000:3000 --name my-container --volume "%cd%":/app chrisnoring/node
Enter fullscreen mode Exit fullscreen mode

NOTE: "%cd%" is being used instead of $(PWD) since it's a windows machine

Collapse
 
denniswebdel profile image
dennisFS

also worth adding that if you are on windows using git bash the path conversion gets messy so that command substituition needs to be escaped like this:
~> docker run -d -p 8000:3000 --name EXAMPLE --volume /$(pwd)/logs:/logs YOUR_IMAGE

Collapse
 
bijoy26 profile image
Anjum Rashid

For me (in windows git bash), I also had to wrap around with "" to make it work.

$ docker run -d -p 8000:3000 --name YOU_NAME --volume /"$(pwd)"/logs:/logs YOUR_IMAGE

Collapse
 
softchris profile image
Chris Noring

Hey. You are completely right. Sorry, you had to lose time over this and thank you for posting this correction, I've updated the article.

Collapse
 
jayywalker profile image
Jordan Walker • Edited

Hi Chris, I really appreciate that you've taken the time to produce these wonderful tutorials. I've learnt so much covering this tutorial during the Easter break.

I had a little problem I came across which I felt I should point out for other devlings hoping to learn Docker. In networked environments, sometimes nodemon doesn't restart, which was the case for myself. To fix this, use nodemon -L app.js rather than nodemon app.js as your start script.

EDIT: just realised there was another comment pointing this out too. Oh well, the first paragraph counts :D

Collapse
 
softchris profile image
Chris Noring

hi Jordan. Appreciate your comment, happy it was useful :) Let me know if there is anything I can do :)

Collapse
 
lingtalfi2 profile image
lafitte pierre

cool

Collapse
 
amirdamirov profile image
amirdamirov

Hi,

I added new lines to package.json but when i try to build image it givem me next errors:

npm ERR! code EJSONPARSE
npm ERR! file /app/package.json
npm ERR! JSON.parse Failed to parse json
npm ERR! JSON.parse Unexpected string in JSON at position 162 while parsing '{
npm ERR! JSON.parse "name": "node",
npm ERR! JSON.parse "version": "1.0.0"'
npm ERR! JSON.parse Failed to parse package.json data.
npm ERR! JSON.parse package.json must be actual JSON, not just JavaScript.

npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2019-09-11T07_41_58_281Z-debug.log

This my package.json file :

{
"name": "node",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"start": "nodemon app.js"
"log": "echo \"Logging something to screen\""
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "4.17.1"
},
"devDependencies": {
"nodemon": "1.19.2"
}
}

Collapse
 
softchris profile image
Chris Noring

looks like you are missing comma , between your tasks in scripts

Collapse
 
amirdamirov profile image
amirdamirov

First Thanks for detailed articles, its really helpful.
Second thanks for quick response =)
I will check it.

Thread Thread
 
softchris profile image
Chris Noring

thanks, happy to hear that :)

Collapse
 
adtm profile image
Tomas Eglinskas

Awesome tutorials! I'll be a pro after all the series! 😂

Collapse
 
jplindstrom profile image
Johan Lindstrom

Very helpful article, thanks!

Collapse
 
tduvally profile image
tduvally

Thank you for this guide, but I'm finding that your examples just aren't consistent. You aren't using the same values for the things, which means the commands error out.

Collapse
 
softchris profile image
Chris Noring

hi.. please tell me which command is erroring out so I can fix it?

Collapse
 
tduvally profile image
tduvally • Edited

Hi Chris, yes:

I don't know node.js, so I had to guess at the fix for the first error, but it looks like just adding the "Hello Chris" route doesn't work. The "=\>" should be "=>" and I had to remove the existing app.get line, since that conflicted from Part I.

After that I got stuck at adding the docker get.app line as well, as that was completely different. I modified it and got it working.

You go between showing the code to run in the text and in screenshots. In one you use "--volume my-volume:/logs", but in the other you use "--volume logs:/logs"

Also, it looks like the comma in the "start" line in package.json causes the build to fail.

Thread Thread
 
softchris profile image
Chris Noring

about the \ they were, unfortunately, introduced when this post was imported from my medium account.. sorry to hear you were struggling with it ( removed now ). As for being different in screenshots and text, let me see if I can take new screenshots. Appreciate you taking the time to tell me these things. Btw would you benefit from a video version of this tutorial?

Thread Thread
 
softchris profile image
Chris Noring • Edited

I've now added a section to explain scripts a bit in Node.js, hopefully, that clears up any confusion, it start withs : Let's describe what we did above, in case you are new to Node.js. Adding a start script to a package.json file means we go into a section called "scripts" and we add an entry start, like so...

Collapse
 
december1981 profile image
Stephen Brown

Volumes are convenient and persistence is a blessing ... except when they're not. We moved to using Docker swarm the other day, and realized that vanilla volumes are not replicated across a swarm. Rather, if a service changes its node and ends up on a different host, we also end up with a new volume (or an outdated one from the last time the container instance lit up on that node). Yes, with volumes you can use different driver types, like NFS (or Glusterfs if you want to be fancy) ... but it gets messy. So we ended up just using fixed database services -the main thing for us requiring persistent volumes- external to our swarm.

Collapse
 
moinuddin14 profile image
Khaja Moinuddin Mohammed

Thanks @chris for the wonderful series. I am facing the following issue, running from Mac OS X
I was able to establish the connection between host folder (logs) with container folder (logs) and able to see the contents for the first time. But then when i make the changes in the host machine for the same logs.txt file its not reflecting inside the container. I ain't sure if its with permissions, if it was with permissions, i believe it might not have shown the content of host machine file in the first instance itself. Appreciate if you can help with this.

Collapse
 
moinuddin14 profile image
Khaja Moinuddin Mohammed

It's working now, i don't know how. But, its working :)

Collapse
 
softchris profile image
Chris Noring

glad to hear that

Collapse
 
heet1996 profile image
Heet Shah • Edited

Hi All,

I don't know why but I am facing some issues when I run below script:

docker run -d -p 3000:3000 --name my-container --volume "%cd%":/app/usr 5b17ceeeae0b

Please refer image for error.

dev-to-uploads.s3.amazonaws.com/i/...

Thank You.

Collapse
 
softchris profile image
Chris Noring

hi Pablo.. I was trying to explain how you start out with node app.js. Then you add a volume and you can change files locally and same change happens inside of container. At that point the route is not changing because we haven't restarted our web server. So we replace node app.js with nodemon app.js, rebuild our image and container and now when I change locally, the change happens in container too aaand nodemon ensures web server is restarted. I changed in the text above but I hope my added explanation here made the scenario clearer?

Collapse
 
bmassioui profile image
Bouchaib MASSIOUI

Thanks a lot :)

Collapse
 
tp6hannah profile image
Hannah

hello, Chris, i have the same feeling like you when It works to route to /nodemon!!!
and thanks for the great series of these 5 articles!!!