Difference between docker bind mount and docker volumes

One of the major design challenge you face while running your applications and services on docker containers is how to persist data. By default the docker containers do not store persistent data. Data is linked to the lifecycle of the container.

To overcome the above challenge, persist data and de-link it from the lifecycle of the container docker provides two options 1. Bind Mounts  2. Volumes


In this blog post I would be discussing in detail the above two methods to persist data while using containers with the pros and cons of using each approach.

Bind Mounts
Volumes
A file/folder stored anywhere on the container host filesystem, mounted on a running container
Preferred way to store persistent data
Can exist anywhere on the filesystem
Docker volumes exist at specific location within the filesystem. For Ubuntu they are at /var/lib/docker/volumes
Processes outside of docker can also modify it
Docker completely manages them and processes outside docker do not have access
Use mount flag with type as bind
Use mount flag with type as volume
The source directory/file must be present in the filesystem else the command throws an error
The docker volume must be created as a pre-requisite using docker volume create

Bind Mounts:

root@ip-172-31-10-3:/# mkdir testdir
root@ip-172-31-10-3:/# cd testdir
root@ip-172-31-10-3:/testdir# touch testmount.txt
root@ip-172-31-10-3:/testdir# pwd
/testdir
root@ip-172-31-10-3:/testdir# docker run -d --name testmountbind --mount type=bind,source=/testdir,target=/data nginx
dafe22161811ec330988e3575970ab9cb62e148bb33166506d8934632b640338
root@ip-172-31-10-3:/testdir# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
dafe22161811        nginx               "nginx -g 'daemon of…"   5 seconds ago       Up 4 seconds        80/tcp              testmountbind
1e293de1097c        nginx               "nginx -g 'daemon of…"   16 minutes ago      Up 16 minutes       80/tcp              test42
5e06d9313eb6        nginx               "nginx -g 'daemon of…"   18 minutes ago      Up 18 minutes       80/tcp              test3
root@ip-172-31-10-3:/testdir# docker exec -it dafe22161811 sh
# ls
bin  boot  data  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
# cd /data
# ls
testmount.txt
#




Docker Volumes:


root@ip-172-31-10-3:/# docker volume create testvol
testvol

root@ip-172-31-10-3:/# docker volume ls
DRIVER              VOLUME NAME
local               testvol


root@ip-172-31-10-3:/var/lib/docker/volumes/testvol/_data# docker run -d --name test3 --mount type=volume,source=testvol,target=/data nginx
5e06d9313eb64c5498d48ebd4d9c1a1dafc84144752f9e3e9614473da7eb338c
root@ip-172-31-10-3:/var/lib/docker/volumes/testvol/_data# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
5e06d9313eb6        nginx               "nginx -g 'daemon of…"   2 seconds ago       Up 2 seconds        80/tcp              test3

root@ip-172-31-10-3:/var/lib/docker/volumes/testvol/_data# docker exec -it 5e06d9313eb6 sh
# ls
bin  boot  data  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
# cd data
# ls
test.txt
# exit


#####Spinning new container and mounting the same volume on it as container "test3"##########

root@ip-172-31-10-3:/var/lib/docker/volumes/testvol/_data# docker run -d --name test42 --mount type=volume,source=testvol,target=/data nginx
1e293de1097cf5f4c1fa7167d4fea1f5b463ffe4ef76f7b9fc9c6bd49e2ed529
root@ip-172-31-10-3:/var/lib/docker/volumes/testvol/_data# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
1e293de1097c        nginx               "nginx -g 'daemon of…"   3 seconds ago       Up 2 seconds        80/tcp              test42
5e06d9313eb6        nginx               "nginx -g 'daemon of…"   2 minutes ago       Up 2 minutes        80/tcp              test3
root@ip-172-31-10-3:/var/lib/docker/volumes/testvol/_data# docker exec -it 1e293de1097c sh
# cd /data
# ls
test.txt  test2.txt


Comments

Popular posts from this blog

python3: unpickling error

Azure Data Analytics: Part1: Hosting Data Lake storage: Gen1 and Gen2