In the first part, we took a look at basics of Docker, building, stopping and removing them. This part will consist of mainly playing with changes and handling the concepts of volumes in docker.

If we have to change few lines of code or “Hello World” to “Hello Div”, we will have to bring down the container, remove it, rebuild the image and then run the container again. But this is too tedious and hence we use a volume to carry out the changes.

Volumes

Volumes is a way for us to create a place in the host machine where we can write files so they are persisted. It’s quite easy to create a volume and we can do so in many different ways, but mainly there are two ways:

  • before you create a container
  • lazily, e.g while creating the container

Creating Volumes:

Now we want to use the volume we create in our application. We want to be able to change or create files in our container so that when we pull it down and start it up again our changes will still be there.

If we want to start/run the container with the volume we have, we will need to do something like:

The entire application as a volume

First kill and remove your first application by:
“docker kill my-container && docker rm my-container”
and run

docker run -d -p 8000:3000 –name my-container –volume $(pwd):/app divyendrapatil/node

This will make the entire app a directory a volume and any changes made will be reflected inside the container. BUT, if you make any changes on the Node.js Express app, it will not be reflected in the server but rather throw an error. To overcome this issue, we need to install a library like nodemon that will restart the server.