Manohar Negi
Manohar Negi

Reputation: 455

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.

Upvotes: 14

Views: 4843

Answers (1)

Thomas Uhrig
Thomas Uhrig

Reputation: 31603

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/

Upvotes: 23

Related Questions