Docker Volume

In Docker, volumes are the preferred mechanism for persisting data generated by and used by Docker containers. They provide a way to store data outside of the container’s writable layer, ensuring that the data remains intact even if the container is stopped, removed, or recreated.

Features

Sure! Here’s a rewritten version of your explanation using simpler structure and clearer phrasing, while keeping the technical meaning intact:

Persistent Storage

By default, Docker containers use a temporary filesystem. This means any data written inside the container disappears once the container is removed. Volumes fix this problem by offering a persistent storage location on the host machine that keeps your data safe even after the container is deleted or recreated.

Managed by Docker

Volumes are handled directly by Docker. You don’t need to set up anything manually. Docker takes care of where the volume is stored (usually in /var/lib/docker/volumes on Linux), how it’s managed, and when it’s cleaned up.

Independent from Containers

Data stored in a volume isn’t tied to one specific container. You can stop, delete, or recreate containers — as long as you use the same volume, the data remains intact and available.

Shareable Across Containers

You can attach the same volume to multiple containers. This allows containers to share data — for example, one container can write logs while another reads them.

Improved Performance

Volumes offer better speed and efficiency than writing to the container’s internal filesystem. They bypass the complex layers of Docker’s storage drivers, resulting in faster read/write operations.

Portable and Consistent

Since Docker manages volumes, they don’t depend on host-specific file paths like bind mounts do. This makes it easier to move your containers and data across different environments or systems without worrying about configuration changes.

Types of Docker Volumes

Named Volumes

Named volumes are user-defined volumes that can be easily referenced by name and reused across multiple containers. They are stored in Docker’s internal volume store.

docker volume create myVolume
docker run -d --name my_container -v myVolume:/data node_container

How they work: Docker creates and manages a dedicated directory on the host machine. You refer to these by a name (e.g., my_data_volume).

Use cases: This is the recommended approach for most persistent data storage, especially for stateful applications like databases (MySQL, PostgreSQL, MongoDB), application data (user uploads, logs), and shared data between containers.

Advantages:

  • Managed by Docker, offering better control and portability.
  • Easier to back up and migrate.
  • Can be pre-populated with data from an image.
  • Better performance.

Anonymous Volumes

Anonymous volumes are created when no name is specified. They are typically used for temporary data that does not need to persist beyond the container’s lifecycle.

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

Bind Mounts:

Bind mounts map a directory or file from the host filesystem to a container. This allows direct access to the host’s filesystem, making it ideal for scenarios where data needs to be shared between the host and the container.

docker run -d --name my_container -v /path/on/host:/data node_container

How they work: You directly mount a file or directory from the host machine into a container. You specify the absolute path on the host.Use cases: Often used in development workflows for:

  • Mounting source code into a container for live development and testing (changes on the host are immediately reflected in the container).
  • Sharing configuration files or logs between the host and container.

Advantages:

  • Direct access to host filesystem.
  • Useful for ad-hoc scenarios.

Disadvantages:

  • Less portable (dependent on host directory structure and OS).
  • Security implications (container can modify host files).
  • Can lead to permission issues if UID/GID inside the container don’t match the host.

tmpfs Volumes

tmpfs volumes mount a temporary filesystem in the container’s memory. They are useful for storing non-persistent data that shouldn’t be written to disk, like sensitive information or temporary files.

  • How they work: Stores data in the host’s memory, not on disk.
  • Use cases: For temporary, non-persistent data that needs to be written quickly and frequently, such as sensitive temporary files or caching.
  • Disadvantages: Data is lost when the container stops or the host reboots.

How to Use Docker Volumes:

Create a Named Volume

docker volume create myvolume

Use It in a Container

docker run -d -v myvolume:/app/data myimage

This mounts the volume to /app/data inside the container.

Inspect Volume

docker volume inspect myvolume

List All Volumes

docker volume ls

Remove Volume

docker volume rm myvolume

FAST API implementation with docker volume

You’re building a FastAPI backend that:

  • Stores and retrieves data from MongoDB
  • Needs MongoDB data to persist using Docker volumes
  • Uses Docker Compose to manage both FastAPI and MongoDB services

Project Structure

fastapi-mongo/
│
├── app/
│   └── main.py
│
├── Dockerfile
├── requirements.txt
└── docker-compose.yml

app/main.py

from fastapi import FastAPI
from pymongo import MongoClient
from bson.objectid import ObjectId

app = FastAPI()

client = MongoClient("mongodb://mongo:27017", serverSelectionTimeoutMS=30000)
db = client.mydatabase
collection = db.mycollection

@app.post("/items/")
def create_item(item: dict):
    result = collection.insert_one(item)
    return {"id": str(result.inserted_id)}

@app.get("/items/{item_id}")
def read_item(item_id: str):
    item = collection.find_one({"_id": ObjectId(item_id)})
    if item:
        item["_id"] = str(item["_id"])
    return item

requirements.txt

fastapi
pymongo
uvicorn

Dockerfile

FROM python:3.11

WORKDIR /code

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]

docker-compose.yml

version: '3.8'

services:
  fastapi:
    build: .
    ports:
      - "8000:8000"
    depends_on:
      - mongo

  mongo:
    image: mongo:6
    ports:
      - "27017:27017"
    volumes:
      - mongo-data:/data/db  # This is the persistent MongoDB volume

volumes:
  mongo-data:

Run the App

docker-compose up --build

We will call api for insertion.

POST http://localhost:8000/items/ with JSON body: { "name": "test" }

For fetching data

GET http://localhost:8000/items/{item_id}

Check Volume

docker volume ls
docker volume inspect fastapi-mongo_mongo-data

Related Posts

CI/CD Pipeline

A CI/CD pipeline coordinates all the processes involved in continuous integration and continuous delivery. Continuous integration (CI) is the practice of automating the integration of code changes…

Kubernetes

Kubernetes (also called K8s) is an open-source platform that helps you automates the deployment, scaling, and management of containerized applications. Kubernetes helps you organize and control them efficiently just like…

Docker

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly….

Docker Vs Kubernetes

Docker and Kubernetes are both crucial technologies for containerized applications, but they serve different purposes. Docker is a platform for building, sharing, and running containers, while Kubernetes is…

Leave a Reply

Your email address will not be published. Required fields are marked *