Community for developers to learn, share their programming knowledge. Register!
Docker Volumes

Sharing Volumes Between Containers in Docker


Welcome to this article, where you can get training on sharing volumes between containers in Docker! As an intermediate or professional developer, understanding how to efficiently share data between containers is crucial for building robust applications. This guide will explore the benefits of sharing volumes, how to create and mount them, as well as utilizing Docker Compose for seamless volume sharing.

Benefits of Sharing Volumes

Sharing volumes between Docker containers offers several significant advantages:

  • Data Persistence: Volumes allow you to persist data beyond the life cycle of individual containers. When a container is removed, the data in the volume remains intact, making it ideal for databases and other stateful applications.
  • Data Sharing: Multiple containers can share the same volume, enabling them to read and write data concurrently. This is particularly useful for microservices that need to communicate or share configuration files.
  • Decoupling: By isolating the file system from the container's image, volumes make it easier to update and manage your applications. You can modify the data without needing to rebuild the container image.
  • Performance: Using volumes can improve performance compared to storing data in the containerā€™s writable layer. Volumes are managed by Docker and can be optimized for performance.
  • Backup and Restore: Volumes can be easily backed up or restored, facilitating easier data management and disaster recovery.

By leveraging these advantages, developers can create more efficient and maintainable applications.

Creating Shared Volumes

Creating shared volumes in Docker is straightforward. You can create a volume using the Docker CLI or directly in your Dockerfile. Hereā€™s how to create a volume using the CLI:

docker volume create my_shared_volume

Once the volume is created, you can mount it in your containers. Hereā€™s an example of how to modify the Dockerfile of our Flask application to include a shared volume:

# Use an official Python runtime as a parent image
FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /usr/src/app

# Copy the current directory contents into the container at /usr/src/app
COPY . .

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Create a shared volume
VOLUME /usr/src/app/data

# Run app.py when the container launches
CMD ["python", "app.py"]

In this example, we declare a volume at /usr/src/app/data, where we can store data that needs to be shared across containers.

Mounting Volumes in Multiple Containers

Once you have created a volume, you can mount it in multiple containers. This allows different containers to share the same data. Hereā€™s how to run two instances of the Flask application using the shared volume:

docker run -d -v my_shared_volume:/usr/src/app/data --name flask_app1 my-flask-app
docker run -d -v my_shared_volume:/usr/src/app/data --name flask_app2 my-flask-app

In this command:

  • -d: runs the containers in detached mode.
  • -v my_shared_volume:/usr/src/app/data: mounts the created volume into both containers.

Now, both flask_app1 and flask_app2 can read and write to /usr/src/app/data, facilitating data sharing between them.

Example Use Case

Consider a scenario where you have two services: a logging service and a web application. The web application logs requests to a shared volume, which the logging service reads to process and store logs. This architecture allows you to decouple the logging functionality from the web application while ensuring both services have access to the same data.

Using Docker Compose for Volume Sharing

Docker Compose simplifies the management of multi-container applications, including the setup of shared volumes. Hereā€™s how to set up our Flask application with a shared volume using Docker Compose:

  • Create a docker-compose.yml file in the root of your project:
services:
  web:
    image: my-flask-app
    volumes:
      - my_shared_volume:/usr/src/app/data
    ports:
      - "80:80"

  logger:
    image: my-logger-app  # Assume this is another service that processes logs
    volumes:
      - my_shared_volume:/usr/src/app/data

volumes:
  my_shared_volume:
  • Start the services with Docker Compose:
docker compose up -d

In this configuration:

  • Both the web and logger services mount the same volume my_shared_volume. This setup allows them to share data seamlessly.
  • The volumes section at the bottom ensures that Docker orchestrates the shared volume's lifecycle.

Using Docker Compose not only streamlines the setup process but also enhances clarity by defining service relationships directly within the configuration file.

Summary

Sharing volumes between Docker containers is an essential skill for intermediate and professional developers. It enhances data persistence, promotes decoupling of services, and improves performance. By creating shared volumes, mounting them across containers, and utilizing Docker Compose for orchestration, you can build robust applications that are easier to manage and scale.

Last Update: 21 Jan, 2025

Topics:
Docker