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...
For further actions, you may consider blocking this person and/or reporting abuse
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.
Do you not need the
COPY . .
for the docker build step? The volume is only mounted during docker run.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?
As I'm running the app through DockerToolbox, I had to add the flag -L to nodemon, otherwise the listening part was not working
Thanks so much.. I must admit I haven't used Docker on Windows so it's great you are able to point out differences :)
This also applies to "Docker Desktop" running on macOS as well. More info here: github.com/remy/nodemon#applicatio...
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
oh wow.. Great tip Vitaly thanks.. I'll update the article :)
If you are on windows and using powershell, change $(pwd) to ${pwd}.
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:
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.
NOTE:
"%cd%"
is being used instead of$(PWD)
since it's a windows machinealso 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
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
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.
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 thannodemon app.js
as your start script.EDIT: just realised there was another comment pointing this out too. Oh well, the first paragraph counts :D
hi Jordan. Appreciate your comment, happy it was useful :) Let me know if there is anything I can do :)
cool
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"
}
}
looks like you are missing comma
,
between your tasks inscripts
First Thanks for detailed articles, its really helpful.
Second thanks for quick response =)
I will check it.
thanks, happy to hear that :)
Awesome tutorials! I'll be a pro after all the series! 😂
Very helpful article, thanks!
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.
hi.. please tell me which command is erroring out so I can fix it?
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.
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?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...
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.
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.
It's working now, i don't know how. But, its working :)
glad to hear that
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.
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?
Thanks a lot :)
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!!!