Pages

Tuesday, June 4, 2019

Docker - Volumes using API

Data persistence is very important while working with containers. Docker provides us a way to bind host directories using the -V option which running the container. In addition to binding host directories docker provides us with volumes. A mechanism for persisting data generated by and used by the docker container. While bind mounts are dependent on the host machine directories volumes on the other side are completely managed by docker. Volumes have more advantages than bind mounts as,
  1. Volumes are created, managed by docker cli
  2. Volumes are easier to backup or migrate to bind mounts
  3. Volumes work on both linux and windows
  4. Volumes can be more safely shared among multiple containers
  5. New volumes can have their content pre-populated by a container

In addition, volumes are often a better choice than persisting data in a container’s writable layer, because a volume does not increase the size of the containers using it, and the volume’s contents exist outside the lifecycle of a given container.

1. Create a Volume with the name html as below,
[root@ip-172-31-23-59 ~]# docker volume create --name html
html

2. Inspect the volume using,
[root@ip-172-31-23-59 ~]# docker volume ls
DRIVER              VOLUME NAME
local               html
[root@ip-172-31-23-59 ~]# docker volume inspect html
[
   {
       "CreatedAt": "2019-06-02T12:07:42Z",
       "Driver": "local",
       "Labels": {},
       "Mountpoint": "/var/lib/docker/volumes/html/_data",
       "Name": "html",
       "Options": {},
       "Scope": "local"
   }
]

The mount point defined here is the path on the docker host where the volume can be accessed. We can check the volume defined in the mount point where the volume location is auto generated
3. To mount the volume we can use,

We can now use this volume and mount it on a specific path of a container. We will use a Nginx image and mount the html volume onto /usr/share/nginx/html folder within the container.

Note: /usr/share/nginx/html is the default folder served by nginx. It contains 2 files: index.html and 50x.html
[root@ip-172-31-23-59 ~]# docker run --name www -d -p 8080:80 -v html:/usr/share/nginx/html nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx

743f2d6c1f65: Pull complete
6bfc4ec4420a: Pull complete
688a776db95f: Pull complete
Digest: sha256:23b4dcdf0d34d4a129755fc6f52e1c6e23bb34ea011b315d87e193033bcd1b68
Status: Downloaded newer image for nginx:latest
2c2b6c5610ad8f45271aa0559cab3aa506a3dd49b0f04d766976c934c1269ed0

And from Host, we can see the files in the mount location as,
[root@ip-172-31-23-59 ~]# ls /var/lib/docker/volumes/html/_data
50x.html  index.html

The content of the /usr/share/nginx/html folder of the www container has been copied into the /var/lib/docker/volumes/html/_data folder on the host.
cat<<END >/var/lib/docker/volumes/html/_data/index.html
> some one here
> END

[root@ip-172-31-23-59 ~]# curl localhost:8080
some one here

No comments :

Post a Comment