...

/

Working with Volume Mounting

Working with Volume Mounting

Learn what volume mounting in Docker is and how to do it.

Docker volumes help with the management of persistent data in docker containers. Persistent data is data where preservation is needed whenever a system restarts, the software is updated, or any other event that might lead to data loss. Some examples of persistent data are log files, databases, or settings. The persistent data is stored on a disk and isn’t like volatile memory, which is lost when the system is powered off.

Normally, when we run a container, file systems are created, and whenever the container is stopped or removed, the data within is lost. Docker volumes help by fixing that challenge by creating volumes that hold the data that’s all within the container. With Docker volumes, data can still be accessed even when the container is stopped or removed.

Docker volume mount

Docker volume mount can be described as attaching Docker volume to a container file system and making it accessible from inside the container. This allows for the exposure of a file on the host machine to the container, especially for mapping cases.

Press + to interact
Docker volume mount
Docker volume mount

From our illustration above, we can see that our container can either be mounted to a filesystem using Docker volumes or a temporary memory through tmpfs mount.

tmpfs is a type of file system in Linux used for storing temporary files in memory. They’re created in RAM (random-access memory used for storing temporary files that need to be accessed quickly). The tmpfs files can be created using the following command:

docker run -d --tmpfs /tmp <image_name>

This command creates the tmpfs mount within the /tmp directory and is used to store files temporarily when the container is running. Details within the /tmp directory are lost once the container is lost or removed. Docker volume mounts allow keeping application data separate from the container image, making it easy for the application or container to update without losing data. We can mount volumes to containers using either -v or –mount flag as seen below:

docker run -v /host/path:/container/path <image_name>

Difference between the -v and the –mount options

Using Docker, both -v and –mount can be used when attaching a volume to a container file system. The -v flag is an older option and has been in use since earlier Docker versions were being used. The -v option is much easier to use because it’s shorter. We can see its usage below:

docker run -v /host/path:/container/path <image_name>

The –mount option is recent and much more powerful than the -v option. It creates the option to add ...