Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does VOLUME command do in Dockerfile?

I am having it difficulty in understanding added advantage of VOLUME(https://docs.docker.com/engine/reference/builder/#volume) .

In Dockerfile one can have mkdir to create a directory. Once the directory is created we can have handle to it. why specify a VOLUME (mount)and assign to that directory? What advantage VOLUME mount gives? I am trying to understand here without VOLUME what will we miss.

to me its looks like a redundant function , however I might be wrong.

like image 797
T D Avatar asked May 15 '17 13:05

T D


People also ask

What is the reason for using volumes in docker?

The purpose of using Docker volumes is to persist data outside the container so it can be backed up or shared. Docker volumes are dependent on Docker's file system and are the preferred method of persisting data for Docker containers and services.

Can I create a volume in Dockerfile?

Volumes can be created via a Dockerfile or an API request. Then, when the container is created, Docker will create the volume with any data that already exists at the specified location. Note that if you create a volume using a Dockerfile, you still need to declare the mountpoint for the volume at run time.

What is NFS volume docker?

Volumes are existing directories on the host filesystem mounted inside a container. They can be accessed both from the container and the host system. Docker also allows users to mount directories shared over the NFS remote file-sharing system.


3 Answers

The VOLUME command will specify a mount point in the container. This mount point will be mapped to a location on the host that is either specified when the container is created or if not specified chosen automatically from a directory created in /var/lib/docker/volumes.

If the directory chosen as the mount point contains any files then these files will be copied into this volume. The advantage over mkdir is that it will persist the files to a location on the host machine after the container is terminated.

It appears some people have questioned why you would use the VOLUME command since it creates an anonymous volume. Anonymous volumes don't have much use any more and are basically an artifact of the early days of Docker before volumes could be named. You would normally specify the volume name when creating the container:

docker container run -v my-volume:/data image_tag

In this example, /data is the mount point in the container and my-volume is the volume on the local host. If my-volume does not exist when this command is run then it is created on the local host.

like image 117
rmullig2 Avatar answered Jan 04 '23 01:01

rmullig2


A volume is very helpful when we don't want to lose data once the container is deleted. A configuration file, a database data, etc. Hence, you are able to use the same volume with a complete new docker container. If you only create a directory within the Dockefile, the data inside the folder will be deleted toghether with the container.

like image 21
kimy82 Avatar answered Jan 04 '23 02:01

kimy82


The mkdir in the Dockerfile will make a directory within that container only. A volume can be on the host machine or a different container, and so remains in existence when the container is killed.

like image 26
Colm Prunty Avatar answered Jan 04 '23 02:01

Colm Prunty