#90DaysOfDevops | Day 19

Rajendra PatilRajendra Patil
6 min read

Introduction on docker volumes :

  • Docker volumes are a feature in Docker that allows you to manage and persist data between containers.

  • They provide a way to store and share data separate from the container itself. Volumes are used to:

    1. Persist Data: Volumes ensure that data generated or used by containers is retained even if the container is stopped or removed.

    2. Share Data Between Containers: Volumes enable multiple containers to access and share the same data, making it easier to build multi-container applications.

    3. Separate Concerns: Volumes separate the concerns of data storage and container execution, making it easier to manage, backup, and restore data.

    4. Backup and Migration: Volumes facilitate data backup, migration, and recovery, as the data is decoupled from the container lifecycle.

    5. Performance Optimization: Volumes can offer improved performance compared to bind mounts, especially when dealing with large amounts of data.

    6. Driver Support: Docker provides various types of volume drivers, allowing you to choose the best storage solution for your needs, such as local, remote, or cloud-based storage.

In summary, Docker's volume

s provide a way to manage data persistence, sharing, and separation in containers, enhancing the flexibility, scalability, and manageability of your Dockerized applications.

What are docker volumes?

  • Docker volumes are a mechanism for managing and persisting data in Docker containers.

  • They allow you to separate the storage of data from the lifecycle of containers, providing a way to store, share, and manage data in a more controlled and flexible manner.

  • In Docker, containers are designed to be lightweight and ephemeral, meaning they can be started, stopped, and destroyed without affecting the underlying data.

  • However, some applications require persistent data that should survive container restarts, updates, or removals.

  • Docker volumes address this need by providing a way to manage data independently of the container instances.

What is Docker Network?

  • Docker networks are a fundamental feature of Docker that enables communication and connectivity between containers, as well as between containers and the host system.

  • Docker networks provide an isolated environment for containers to interact with each other while maintaining security and segregation.

  • To create and manage Docker networks, you can use the Docker CLI commands like docker network create, docker network ls and docker network connect. You can also define networks in Docker Compose YAML files.

  • Docker networks play a crucial role in facilitating communication and connectivity between containers, both within a single host and across multiple hosts.

  • They provide an essential tool for building and deploying complex applications composed of multiple interacting containers.

Task-1

Create a multi-container docker-compose file that will bring UP and bring DOWN containers in a single shot ( Example - Create application and database container )

Here is an example of a Docker Compose file that defines a multi-container setup with an application container and a database container. This example includes configurations for both bringing the containers up and bringing them down using a single command.

Create a file named docker-compose.yml and add the following content:

  • We can also adjust the port mappings, environment variables, and other settings as needed.

  • To bring up the containers, navigate to the directory containing the docker-compose.yml file in your terminal and run:

    This command will start both the application and database containers in detached mode (-d), allowing them to run in the background.

    To bring down the containers, use the following command:

    This command will stop and remove the containers, along with any associated networks and volumes.

Task-2

  • how to use Docker Volumes and Named Volumes to share files and directories between multiple containers.

    1. Create a Docker Volume (Named Volume):

You can create a named volume using the docker volume create command. This volume will store the shared data that can be accessed by multiple containers.

Create Containers Using the Named Volume:

When creating containers, you can specify the named volume to be mounted into the containers. This allows them to share data using the same volume.

  1. In this example, my-image1 and my-image2 are the images of the containers you want to create. The -v flag specifies the volume to be mounted into the containers.

  2. Access Shared Data:

    Inside the containers, the /shared-data directory will contain the shared data from the named volume. Both container1 and container2 will have access to the same data.

Here's how you can achieve the same using Docker Compose:

Create a Docker Compose YAML File:

Create a docker-compose.yml file that defines the named volume and the containers that use it.

Run Docker Compose:

Navigate to the directory containing the docker-compose.yml file and run:

  1. This command will create and start the containers using the named volume.

  2. Access Shared Data:

    Inside the containers, you can access and modify the shared data in the /shared-data directory.

Using Docker volumes and named volumes as described above, you can easily share files and directories between multiple containers. This is particularly useful for scenarios where you need data consistency and synchronization across different parts of your application.

  1. Create two or more containers that read and write data to the same volume using the docker run --mount command.

    Create a Docker Volume:

    Let's start by creating a Docker volume that the containers will use to share data

    Create Containers:

    Now, create two containers that will read and write data to the shared volume using the docker run --mount command.

    Container 1

    docker run -d --name container1 --mount source=my-shared-volume,target=/shared-data busybox sh -c "echo 'Hello from Container 1' > /shared-data/message.txt"

    Container 2

    docker run -d --name container2 --mount source=my-shared-volume,target=/shared-data busybox sh -c "cat /shared-data/message.txt"

    In this example, we're using the busybox image, a lightweight base image. The first container writes a message to a file in the shared volume, and the second container reads and displays the message.

    Access Shared Data:

    You can check the output of the second container to verify that it successfully read the message from the shared volume.

    The output should display: "Hello from Container 1".

    This example demonstrates how to create two containers that interact with the same volume using the docker run --mount command. You can adapt this approach to more complex scenarios and use cases, where multiple containers need to share and manipulate data in a coordinated manner.

  2. Verify that the data is the same in all containers by using the docker exec command to run commands inside each container.

    Verify Data Using docker exec:

    Use the docker exec command to run a command inside each container and verify that the data is the same:

    1. Use the docker volume ls command to list all volumes and the docker volume rm command to remove the volume when you're done.

List All Volumes:

Use the docker volume ls command to list all volumes created on your system:

This will display a list of volumes along with their names and other information.

Remove the Volume:

Once you're done testing and want to remove the volume you created, you can use the docker volume rm command followed by the volume name.

In our previous examples, the volume name is my-shared-volume:

Note that you can only remove a volume if there are no containers or services that are actively using it. If you attempt to remove a volume that is still in use, you will receive an error message.

Remember that removing a volume is a permanent action and will result in the loss of any data stored in that volume. Make sure you've backed up any important data before removing a volume.

By using the docker volume ls and docker volume rm commands, you can easily manage and clean up volumes that are no longer needed in your Docker environment.

Happy Learning..

0
Subscribe to my newsletter

Read articles from Rajendra Patil directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rajendra Patil
Rajendra Patil