Showing posts with label volume. Show all posts
Showing posts with label volume. Show all posts

What are Volumes in Docker and how to use them

 Docker Volume




Volumes are preferred mechanism for persisting data by docker containers. Volumes are completely managed by Docker. There are couple of important points which can convince you to use volumes over
bind mounts.

  • Volumes are easy to backup or migrate.
  • Volumes can be easily shared with other containers.
  • Volumes work on both Linux and windows.
  • Volumes can be managed by Docker CLI or Docker API.
  • New volumes can have their content pre populated by containers.
  • Volumes are better choice than persisting data on container' writable layer.
  • Volume's contents exist outside of lifecycle of a given container.

If your container generates non persistent state data, then you can consider using tmpfs mount to avoid storing the data  anywhere on permanently. And to increase the performance of the container you can always avoid writing data to container's writable layer. Always keep your container lightweight. 


What is tmpfs mount 

If you are running containers on Linux system, then you have one more option to use is tmpfs mount.
When you create a container with tmpfs mount then container can create files outside of container's writable layer. 

The tmps mount is temporary and only persisted in host memory. So when you stop the container the tmpfs mounts is removed and data persisted there won't be available.

The best use case for this is ,when you temporarily want to store the sensitive data and you don't want to persist it inside either host or container's writable layer.


Limitations on tmpfs mount

If you want to use tmpfs mount the you need to remember two things below
  • You can share tmpfs mount between containers
  • This functionality is only available on Linux.

Let's create tmpfs mount 

Start the container alpine and inspect it. Give the container a name. In my case i gave it tmptest0.


docker inspect tmptest0

[

    {

     

            "AutoRemove": false,

            "VolumeDriver": "",

            "VolumesFrom": null,

            "CapAdd": null,

            "CapDrop": null,

            "CgroupnsMode": "host",

            "Dns": [],

            "DnsOptions": [],

            "DnsSearch": [],

            "ExtraHosts": null,

            "GroupAdd": null,

            "IpcMode": "private",

            "Cgroup": "",

            "Links": null,

            "OomScoreAdj": 0,

            "PidMode": "",

            "Privileged": false,

            "PublishAllPorts": false,

            "ReadonlyRootfs": false,

            "SecurityOpt": null,


Now lets start one more container with alpine image and give it a name tmptest and attach tmpfs mount.

docker run -it -d --name tmptest --tmpfs /app alpine


Now inspect this tmptest container.

 "AutoRemove": false,

            "VolumeDriver": "",

            "VolumesFrom": null,

            "CapAdd": null,

            "CapDrop": null,

            "CgroupnsMode": "host",

            "Dns": [],

            "DnsOptions": [],

            "DnsSearch": [],

            "ExtraHosts": null,

            "GroupAdd": null,

            "IpcMode": "private",

            "Cgroup": "",

            "Links": null,

            "OomScoreAdj": 0,

            "PidMode": "",

            "Privileged": false,

            "PublishAllPorts": false,

            "ReadonlyRootfs": false,

            "SecurityOpt": null,

            "Tmpfs": {

                "/app": ""

            },



See the difference. It has added tmpfs mount.


Let's create the volume

Docker volume create new-volume

docker volume create new-volume                     

new-volume


Check the volume has been created or not.

docker volume ls

DRIVER    VOLUME NAME

local     new-volume


So we have the volume now. Lets inspect and check what's inside the volume

docker volume inspect new-volume

[

    {

        "CreatedAt": "2021-09-22",

        "Driver": "local",

        "Labels": {},

        "Mountpoint": "/var/lib/docker/volumes/new-volume/_data",

        "Name": "new-volume",

        "Options": {},

        "Scope": "local"

    }

]



Start the container with Volume


docker run -d --name voltest -v new-volume:/app alpine


Inspect the container and you'll see below result:

docker container inspect voltest


"HostConfig": {

            "Binds": [

                "new-volume:/app"

            ],


Now follow the cleanup with below commands.


Here i am stopping all running containers at once.

docker container stop $(docker ps -q) 


Remove all containers

docker container rm $(docker ps -aq)


Remove the volume

docker volume rm new-volume


That was it on volume. There is lot more on volume and that is creating a volume using volume driver, which I'll post on upcoming blogs. That's how we create and use the volumes on docker.

Quantum Computing: The Future of Supercomputing Explained

  Introduction Quantum computing is revolutionizing the way we solve complex problems that classical computers struggle with. Unlike tradi...