Everything you need to know about docker volumes - Docker tips and tricks

Consider a scenario where you develop a Nodejs Application with MongoDB as a database. your application stores the data in MongoDB and everything goes well.

After some point of time, you want to automate the process of deployment. so, everytime Pull request is merged into master branch. you want to create a new docker container to serve your application.

One of the main challenges that you face here is, everytime you delete an existing container. your mongodb data will also be deleted. it will be a problem while using a docker.

To overcome this problem, we need to use Docker Volumes. Let's learn everything about docker volumes in this guide.

What is Docker volume

Docker volume is a persistent data storage mechanism to store the data in docker. before volumes, docker uses bind mounts to store data in the container. bind mounts are dependent on the directory structure of host machine.

Let's look at this in detail to understand it further. As you know, docker runs a isolated containers with separate process that shares the file system and hardware from the host machine . Whenever you create a container, it mounts a directory with the container to run our application.

docker volume

File system managed by container can not be shared with other containers. To create a directory managed by container. we use bind mounts.

problem with bind mounts is, it is not persistent. once we remove the container, docker deleted the respective file directory of the respective container.

To solve this, we use docker volumes, one of the main differences are docker manages volume directly. In that way, even if you delete the container. data will be persistent.

Getting started with docker volume

Create a Volume

To create a volume in docker, use the command,

1docker volume create <VOLUME NAME>

docker volume

Note: warning will not occur for your docker configuration.

List Volume

Once you create a volume, you can list all the docker volume using the command,

1docker volume ls

Remove a volume

To remove a volume from docker, you can use the follow command,

1docker volume rm

Remove all unused volume

you can also remove all unused volume from docker using the command,

1docker volume prune

docker volume - example

Let's take an example to learn about docker volumes. here, we will take a two nodejs application which shares a same docker volume. both will contains API to write and read a file. let's see if docker volume can be shared between two container.

At the end of this guide, i have a real world example as an exercise for you. so, stay tuned

docker volume app

let's build this application and use docker volume to store the data. create a application with npm init and add app.js

1const express = require("express")
2const bodyParser = require("body-parser")
3const path = require("path")
4const fs = require("fs")
5const app = express()
6
7app.use(bodyParser.json())
8app.use(bodyParser.urlencoded({ extended: false }))
9
10const dataPath = path.join(process.env.DATA_PATH || "./data.txt")
11
12app.get("/", (req, res) => {
13 res.send("Welcome To docker volume")
14})
15
16app.get("/data", (req, res) => {
17 let data = fs.readFileSync(dataPath)
18 data = data.toString()
19 res.status(200).send(data)
20})
21
22app.post("/add", (req, res) => {
23 const data = req.body.text
24 fs.writeFileSync(dataPath, data)
25
26 res.status(201).send({ success: true })
27})
28
29async function start() {
30 app.listen(3001, () => {
31 console.log(`Server is running on PORT ${3001}`)
32 })
33}
34
35start()

Here, we add two API's add and data where add writes the request data to file. and data GET API returns the data.

let's create a Dockerfile for our application.

1FROM node:alpine
2
3WORKDIR /src
4
5COPY package*.json ./
6
7RUN npm install
8
9COPY . .
10
11CMD ["node","app.js"]

If you're beginner to docker world, i recommend you to checkout this article

After that, you need to build docker image for our application to run it in container.

1docker build -t <TAG NAME> .

once you build docker image for our app. you need to run it in the container. here's where you need to mount docket volume for your application container.

Mounting a docker volume

To mount a docker volume with container, you need to add the --mount flag in docker run command.

1docker run --mount source=[volume_name],destination=[path_in_container] [docker_image]

source specifies the name of the volume and destination specifies the path in container.

For the above application, you need to specified it as,

1docker run -d --env DATA_PATH=/data/data.txt --mount type=volume,src=file-st,target=/data -p 3001:3001 <IMAGE TAG>

it will mount the volume with specified container path. now, anything you write and read will be from the docker volume.

Let's create an another application which shares the same docker volume to check whether it is working. you can use the same setup by changing the port in the application and docker container.

1app.listen(3002, () => {
2 console.log(`Server is running on PORT ${3002}`)
3})

docker run command will be

1docker run -d --env DATA_PATH=/data/data.txt --mount type=volume,src=file-st,target=/data -p 3002:3002 ganeshmani009/volume-app-two:latest

Now, that we have a two application running with shared docker volume. let's try to write a file and see if it gets reflected in the another application.

docker volume demo

Alright. our applications works the way we expected. now, it's time to do our exercise with a real world example.

Real world Example

Here's the task that you need to do,

  1. Create a Simple Todo Nodejs Application with MongoDB connection to store the data.
  2. Pesist the Mongodb Data with Docker volume.
  3. Create an another Nodejs Application to read the Todo from MongoDB and show it as logs.

Conclusion

i recommend you to try this real world scenario to get hands-on experience on the docker volumes. having a knowledge on docker as a web developer become vital in the current software development world. Happy coding :-)

To Read More

Everything you need to know about d...

Docker volume is a persistent data storage mechanism to store the data in docker...

How to remove docker image - Docker...

This guide will cover everything you need to know about removing docker images f...

How to build an Actionable data ta...

In this article, we will see how to build an Actionable data table using a react...