Docker for DevOps (Docker Volumes)

Docker for DevOps (Docker Volumes)

Why Do We Need Docker Volumes? 📂

Containers are usually stateless—when a container stops or is removed, its data is gone. This is great for apps that don’t need to store data long-term, but what about databases, logs, or user uploads? This is where Docker Volumes come in. Volumes allow containers to:

  • Store persistent data even after the container is stopped or removed.
  • Share data between containers.
  • Separate application data from the container itself, keeping data safe.


Types of Docker Storage 📦

Docker provides several ways to manage data, with Volumes being the most common choice:

  1. Volumes: Docker-managed storage that’s easy to share between containers and persists beyond container lifecycles. Volumes are stored outside the container’s file system, making them the best choice for persistent data.
  2. Bind Mounts: Links a container directory to a specific path on the host machine. Bind mounts are useful when you need more control, like accessing a specific directory on the host.
  3. Tmpfs Mounts: Stored in the container’s memory (RAM) only. Useful for sensitive data or temporary files that don’t need to persist.

For most DevOps scenarios, Volumes are the go-to option since they’re portable, secure, and optimized by Docker.


Creating and Using Docker Volumes 🛠️

Let’s walk through a simple example where we’ll use a Docker Volume to persist data for a MySQL database.

  1. Create a Volume:

docker volume create my_data_volume        

2. Run a MySQL container with the volume:

docker run -d --name mydb \
  -e MYSQL_ROOT_PASSWORD=rootpassword \
  -v my_data_volume:/var/lib/mysql \
  mysql:5.7        

Here’s what’s happening:

-v my_data_volume:/var/lib/mysql: Mounts the volume my_data_volume to /var/lib/mysql in the container, which is where MySQL stores its data.

3. Inspect the Volume:

docker volume inspect my_data_volume        

4. Stop and Remove the Container:

docker rm -f mydb        

5. Run a New MySQL Container with the Same Volume:

docker run -d --name mydb_new \
  -e MYSQL_ROOT_PASSWORD=rootpassword \
  -v my_data_volume:/var/lib/mysql \
  mysql:5.7        

Data persists because it’s stored in my_data_volume, outside of the container’s lifecycle. You can stop or replace containers without losing data.


Docker Volume Commands Cheat Sheet 📝

Here are some useful commands for managing Docker Volumes:

  • List all volumes:

docker volume ls        

  • Remove a volume:

docker volume rm my_data_volume        

  • Remove all unused volumes:

docker volume prune        

Real-World Example: Using Volumes for Web App Data Storage 🌐

Imagine you’re running a web app that needs to store user uploads. With Docker Volumes, you can separate application data from the container. Here’s a simple docker-compose.yml setup:

version: '3.8'

services:
  web:
    image: my-web-app
    ports:
      - "8080:80"
    volumes:
      - uploads_volume:/app/uploads

volumes:
  uploads_volume:        

In this setup:

  • uploads_volume is a named volume where user uploads will be stored.
  • Even if the web container is stopped or replaced, data in uploads_volume will persist.


Why Docker Volumes are Essential in DevOps 🌟

Volumes are a DevOps must-have because they make data management secure, efficient, and resilient. In production, they’re perfect for databases, application logs, user-generated content, and any data that needs to stick around regardless of container lifecycles.


Fun Fact 💡

Docker Volumes are stored under /var/lib/docker/volumes on your host machine. But, if you use named volumes, Docker handles all the complexity, so you don’t need to worry about exact paths!




To view or add a comment, sign in

More articles by Sahil Kasekar

  • End-to-End DevOps Project

    🌟 What We’re Building Today Goal: Create a CI/CD pipeline that automates: Building a Dockerized application. Testing…

  • Automated Testing in DevOps

    🌐 What is Automated Testing? Automated Testing is the process of running tests on software, applications, or…

  • GitOps in DevOps (Introduction to GitOps)

    🌐 What is GitOps? 🤔 GitOps is a set of practices for managing infrastructure and application deployments using Git…

  • Cloud Computing & IaC in DevOps (Terraform vs Ansible)

    🛠️ Terraform: Key Features 🌍 Infrastructure as Code (IaC): Define and provision infrastructure declaratively…

  • Cloud Computing & IaC in DevOps (Integrate Ansible)

    🌐 Why Integrate Ansible with CI/CD? 🤔 Integrating Ansible with CI/CD pipelines allows you to: Automate Infrastructure…

  • Cloud Computing & IaC in DevOps (Ansible Roles)

    🌐 What is an Ansible Role? 🤔 An Ansible Role is a way to organize and modularize your Ansible code. It packages…

  • Cloud Computing & IaC in DevOps (Ansible Playbook)

    🌐 What is a Playbook? 🤔 An Ansible playbook is a YAML file that defines a set of tasks to run on target machines. It…

  • Cloud Computing & IaC in DevOps (Ansible)

    🌐 What is Ansible? 🤔 Ansible is an open-source configuration management and automation tool developed by Red Hat. It…

    1 Comment
  • Cloud Computing & IaC in DevOps (Terraform Advanced)

    🌐 Why Advanced Terraform Concepts Matter 🤔 As your infrastructure grows, so does the complexity of managing it…

  • Cloud Computing & IaC in DevOps (Terraform)

    🌐 What is Terraform? 🤔 Terraform is an open-source IaC tool developed by HashiCorp. It enables you to define and…

Others also viewed

Explore content categories