Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would I want to to use VOLUME inside a Dockerfile?

Tags:

linux

docker

To me the VOLUME in a Dockerfile doesn't seam to be doing anything, where -v on the commandline actually make a directory available inside the container.

When I read the Docker manual for VOLUME, it is not really clear to me, why I ever want to write it in the Dockerfile, and not just on the commandline?

like image 476
Jasmine Lognnes Avatar asked Dec 07 '15 13:12

Jasmine Lognnes


2 Answers

Defining the volume in the Dockerfile doesn't expose the volumes to the host by default. Instead it sets up the linked volume to allow other containers to link to the volume(s) in other Docker containers. This is commonly used in a "Data Container" configuration where you start a container with the sole purpose of persisting data. Here's a simple example:

 docker run -d --name docker_data docker/image1
 docker run -d --volumes-from docker_data --name new_container docker/image2

Notice the --volumes-from flag.

See http://container-solutions.com/understanding-volumes-docker/ for a more thorough explanation.

like image 83
RustProof Labs Avatar answered Oct 05 '22 19:10

RustProof Labs


In addition to the accepted answer, another consideration for using volumes is performance. Typically, the layered filesystems used by Docker (typically AUFS or Devicemapper, depending on which Linux distribution you're using) aren't the fastest and may become a bottleneck in high-throughput scenarios (like, for example, databases or caching directories).

Volumes, on the other hand, even if not explicitly mapped to a host directory, are still simple bind mounts to the host file system, allowing a higher throughput when writing data.

For further reading, there's an interesting paper by IBM on this topic, which contains some interesting conclusions regarding the performance impact of using Docker volumes (emphasis mine):

AUFS introduces significant overhead which is not surprising since I/O is going through several layers, [...]. Applications that are filesystem or disk intensive should bypass AUFS by using volumes. [...] Although containers themselves have almost no overhead, Docker is not without performance gotchas. Docker volumes have noticeably better performance than files stored in AUFS.

like image 35
helmbert Avatar answered Oct 05 '22 18:10

helmbert