Chuck's Academy

Docker

Volumes and Persistent Storage in Docker

In applications that require persistent data storage, Docker allows managing data through volumes. Volumes ensure that data is not deleted when the container is stopped or removed, which is crucial for applications like databases. In this chapter, we will explore how to create and use volumes in Docker.

What is a Volume in Docker?

A volume is a storage unit managed by Docker that allows storing and sharing data between containers and with the host system. Volumes offer a secure and efficient way to handle persistent data.

This image shows an example of how a volume works in DockerThis image shows an example of how a volume works in Docker

Creating a Volume

To create a volume, use the command docker volume create followed by the volume name:

bash
"The command docker volume create my hyphen data hyphen volume creates a volume named my-data-volume that can be used to store persistent data."

Checking Existing Volumes

To list all available volumes on the system, use the command:

bash
"The command docker volume ls lists all volumes created on the system, showing their names and details."

Mounting Volumes in Containers

Once the volume is created, it can be mounted in a container when starting it. This allows the container to access and store data in the volume.

bash
"This command runs the container my-app and mounts the volume my-data-volume to the folder app slash data in the container, enabling the container to store data persistently."

Types of Volumes

Docker offers various types of volumes to meet different storage needs:

  • Anonymous Volumes: Created automatically and do not have an assigned name. They are useful for temporary storage.
  • Named Volumes: Created with a specific name and are reusable in different containers.

Example of Named Volume

bash
"Here, the volume my-data-volume is a named volume, meaning it can be used and mounted in other containers to maintain consistent access to data."

Example of Anonymous Volume

bash
"This command creates an anonymous volume mounted in the folder app slash data of the container. This type of volume is typically used for temporary storage since it does not have a specific name."

Copying Files between Container and Volume

Docker allows copying files between the host and the container's volume using the command docker cp. This is useful for transferring data without modifying the container image.

bash
"This command copies the folder host-folder from the host system to the container my-container in the folder app slash data. It is useful for transferring files to the container without having to rebuild the image."

Deleting Volumes

If a volume is no longer needed, it can be deleted with the command docker volume rm followed by the volume name:

bash
"The command docker volume rm my-data-volume deletes the volume named my-data-volume from the system, freeing up storage space."

It is important to remember that once deleted, the data stored in the volume will be lost.

Practice: Mounting a Volume in a Web Application

Below is a practical example of how to mount a volume in a Node.js web application to store data persistently.

Example Dockerfile:

dockerfile
"In this Dockerfile, the base image is node fourteen. It defines the working directory as app, copies the source code, installs dependencies, and defines the main command that runs server dot js."

Command to run the container with a volume:

bash
"This command runs the image my-node-app in a container. It maps port three thousand on the host to port three thousand on the container and mounts the volume my-data-volume to the folder app slash data in the container, allowing persistent data storage."

Conclusion

Volumes are an essential tool in Docker for handling persistent data, allowing containers to store and share data securely and efficiently. In the next chapter, we will explore Docker's networking system and how containers can communicate with each other and the outside world.


Ask me anything