Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does docker have docker volumes and volume containers

Why does docker have docker volumes and volume containers? What is the primary difference between them. I have read through the docker docs but couldn't really understand it well.

like image 735
Manohar Negi Avatar asked Jun 18 '14 15:06

Manohar Negi


People also ask

What is the reason for using volumes in Docker?

While bind mounts are dependent on the directory structure and OS of the host machine, volumes are completely managed by Docker. Volumes have several advantages over bind mounts: Volumes are easier to back up or migrate than bind mounts. You can manage volumes using Docker CLI commands or the Docker API.

What are Docker container volumes?

A Docker volume “lives” outside the container, on the host machine. From the container, the volume acts like a folder which you can use to store and retrieve data. It is simply a mount point to a directory on the host. There are several ways to create and manage Docker volumes.

What is the purpose of volumes?

It is also known as the capacity of the object. Finding the volume of an object can help us to determine the amount required to fill that object, like the amount of water needed to fill a bottle, an aquarium or a water tank.

Is it safe to delete Docker volumes?

Given you likely deleted the container long ago, the volumes are almost always safe to delete. You can run the following to delete anything with the long hash name. The deletes will fail if the volumes are currently in use, so there's no risk to running or even stopped containers.


1 Answers

Docker volumes

You can use Docker volumes to create a new volume in your container and to mount it to a folder of your host. E.g. you could mount the folder /var/log of your Linux host to your container like this:

docker run -d -v /var/log:/opt/my/app/log:rw some/image

This would create a folder called /opt/my/app/log inside your container. And this folder will be /var/log on your Linux host. You could use this to persist data or share data between your containers.

Docker volume containers

Now, if you mount a host directory to your containers, you somehow break the nice isolation Docker provides. You will "pollute" your host with data from the containers. To prevent this, you could create a dedicated container to store your data. Docker calls this container a "Data Volume Container".

This container will have a volume which you want to share between containers, e.g.:

docker run -d -v /some/data/to/share --name MyDataContainer some/image

This container will run some application (e.g. a database) and has a folder called /some/data/to/share. You can share this folder with another container now:

docker run -d --volumes-from MyDataContainer some/image

This container will also see the same volume as in the previous command. You can share the volume between many containers as you could share a mounted folder of your host. But it will not pollute your host with data - everything is still encapsulated in isolated containers.

My resources

https://docs.docker.com/userguide/dockervolumes/

like image 178
Thomas Uhrig Avatar answered Sep 22 '22 21:09

Thomas Uhrig