CodeGym /Courses /Docker SELF /Mounting Volumes

Mounting Volumes

Docker SELF
Level 12 , Lesson 2
Available

8.1 Introduction to Virtual Disks

Mounting volumes is one of the coolest features of Docker. It lets containers easily work with data from the host machine. This makes data management flexible and convenient: you can save data for long-term use, share it between containers, and easily back up or restore data if something goes wrong.

Volumes in Docker are a way to store data so that it doesn't depend on containers. Even if a container is deleted, the info stored in volumes remains safe. You can attach (mount) volumes to containers, allowing you to store data separately and reuse it no matter how many times you create or remove containers.

Key Volume Types

  1. Anonymous Volumes
    • Automatically created by Docker if no volume is explicitly specified for a container.
    • Used for temporary data storage.
  2. Named Volumes
    • Created and managed by Docker.
    • Can be attached to multiple containers and persist even after they’re deleted or stopped.
  3. Bind Mounts
    • Link a directory on the host machine to a directory in the container.
    • Perfect for accessing host system data and handy during development.

Basic Syntax of the docker run Command for Mounting Volumes


-v <host_path>:<container_path>

Full Syntax:


docker run -v <host_path>:<container_path> [OPTIONS] IMAGE [COMMAND] [ARG...]

Main Parameters for Mounting Volumes

1. Parameter -v or --volume

Used for creating a named volume or a bind mount.

Example:

Terminal

docker run -d -v /host/data:/container/data nginx

2. Parameter --mount

Provides a more flexible and detailed way to mount volumes, supporting additional parameters.

Example:

Terminal

docker run -d --mount type=bind,source=/host/data,target=/container/data nginx

8.2 Creating and Using Volumes

1. Named Volumes

Named volumes are created and managed by Docker. They're used for long-term storage of data that should be retained between container restarts or removals.

Creating a named volume:

Terminal

docker volume create my_volume

Running a container with a mounted volume:

In this example, the volume my_volume is mounted to the directory /data inside the container my_container. All data written to /data will be preserved in the volume and remain accessible even after the container is removed.

Terminal

docker run -d -v my_volume:/data --name my_container nginx

2. Anonymous Volumes

Anonymous volumes are created by Docker automatically and are tied to a specific container. They are useful for temporary data that doesn't need to be retained after the container is removed.

Running a container with an anonymous volume:

In this example, Docker will automatically create an anonymous volume and mount it to the directory /data inside the container.

Terminal

docker run -d -v /data --name my_container nginx

3. Bind Mounts

Bind mounts allow you to mount directories on the host system into containers. This is handy for sharing data between containers and the host system. It's also often used during development when the source code is stored on the host system.

Running a container with a bind mount:

In this example, the directory /host/data on the host machine is mounted to the directory /container/data inside the container my_container.

Terminal

docker run -d -v /host/data:/container/data --name my_container nginx

8.3 Examples of Using Volumes

1. Database Data Persistence

Using volumes for databases helps keep the data intact even during container restarts or updates. This is especially crucial for reliable operation in production environments.

Example:

In this example, PostgreSQL data is stored in the db_data volume, ensuring its preservation during container restarts or deletion.

Terminal

docker volume create db_data
docker run -d -v db_data:/var/lib/postgresql/data --name postgres_container postgres

2. Sharing Data Between Containers

Sometimes it's necessary to share data between multiple containers. Volumes make this easy to achieve.

Example

Let's create a volume and run two containers that will use this volume. Both containers will have access to the data in the shared_data volume, allowing them to share information.

Terminal

docker volume create shared_data
docker run -d -v shared_data:/data --name container1 nginx
docker run -d -v shared_data:/data --name container2 nginx

3. Development and Testing

When developing applications, it's handy to use bind mounts to share source code between the container and the host machine. This allows developers to edit code on the host, and the container can immediately use the updates.

Example

In this example, the /path/to/source directory on the host machine is mounted into the /app directory inside the dev_container. This way, the container can work with the actual source code directly from the host system.

Terminal

docker run -d -v /path/to/source:/app --name dev_container node
<

8.4 Managing Volumes

Docker provides convenient commands for managing volumes, making it easy to create, delete, and view information about volumes.

1. Viewing all volumes

This command lists all available volumes on the host machine.

Terminal

docker volume ls

2. Viewing volume information

With this command, you can get detailed info about the volume my_volume: its location on the host file system, as well as information about containers using this volume.

Terminal

docker volume inspect my_volume

3. Removing a volume

This command removes the volume my_volume. Before removing, it's important to ensure that the volume isn't being used by any container, otherwise, the command won't execute.

Terminal

docker volume rm my_volume

8.5 Backing up volumes

To keep your data safe, it's super important to be able to back up your volumes and restore 'em when you need to.

1. Backing up a volume

In this example, the content of the volume my_volume gets archived into a file my_volume_backup.tar.gz, which is saved in the /backup directory on the host machine.

Terminal

docker run --rm -v my_volume:/volume -v /backup:/backup busybox tar czf /backup/my_volume_backup.tar.gz /volume

2. Restoring a volume

This example restores the content of the volume my_volume from the backup stored in the file my_volume_backup.tar.gz.

Terminal

docker run --rm -v my_volume:/volume -v /backup:/backup busybox tar xzf /backup/my_volume_backup.tar.gz -C /volume
3
Task
Docker SELF, level 12, lesson 2
Locked
Creating a named volume and mounting it into a container
Creating a named volume and mounting it into a container
3
Task
Docker SELF, level 12, lesson 2
Locked
Mounting a bind directory
Mounting a bind directory
3
Task
Docker SELF, level 12, lesson 2
Locked
Sharing a Volume Between Containers
Sharing a Volume Between Containers
3
Task
Docker SELF, level 12, lesson 2
Locked
Backup and Restore Volume
Backup and Restore Volume
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION