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 (when 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.