Volumes

When the code of the project changes, it is necessary to rebuild the image for every update. This is a tedious task, and is not desired in development.

This repetitive task of building images can be avoided by using Docker Volumes. The docker run command can accept a -v or --volume attribute. The attribute can accept two directory locations separated by a :. This represents the <host-src>:<container-dst> shared volume. This means that files from the host will be mounted inside the container. If any files already exist at that location in the container, they will be preceded (overridden, not overwritten, an image is immutable, and cannot change).

docker run -p 80:80 -v /c/path/to/project:/var/www/html hello-world

Docker Terminal makes use of the Windows file system. The notation it uses is the Linux directory notation. Eg: C:\path\to\project needs to be listed as /c/path/to/project

This feature enables us to override the original index.php file with an other file that is on the host system. Changes made to this file, will reflect directly inside the container. This means that if we update the code, we do not need to rebuild the image and run the container for the updates to reflect.

Try it out. Change the index.php file on your machine and refresh the page in the browser to see if those changes are visible.

Persistent storage

Volume can also be used to persist data that is generated by the container. For example an application that allows you to upload images, should persist the images so that when the container restarts no information is lost.

Docker provides two types of volumes. Managed volumes and bind mount volumes. The example above makes use of bind mount volumes. More information about this can be found in the Docker documentation.

Managed volumes

Bind mount volumes

Sharing state

Volumes can also be used to share state and information between different containers.

Last updated