DevSecOps for BBK
  • Introduction
  • Getting Started
    • Virtual Training Practices
    • Training Plan
    • Tools and Configuration
    • Troubleshooting
  • Docker Fundamentals
    • Docker Setup
    • Docker First Container
    • Docker Network Basics
    • Docker Network Exercise
    • Docker Volume Basics
    • Docker Storage
  • Dockerfile Fundamentals
    • Dockerfile Instructions
    • Dockerfile Environment Variables
    • Dockerfile Build Arguments
    • Non-privileged Containers
    • Dockerfile Order of Execution
    • Dockerfile Volume
    • Entrypoint Command
    • Multiple Stage Build
  • Container Management
    • Containers Start Automatically
  • Docker Security
    • Seccomp Profile
  • Docker Compose
    • Docker Compose Install
    • Docker Compose Commands
    • Docker Compose File
    • Docker Compose Volumes and Network
  • K8s Cluster Setup
    • Minikube Installation
    • Calico Networking
    • K8s Walkthrough
    • K8s Cheat Sheet
  • Understand K8s
    • Pod Connectivity
    • Deployments
    • Service Cluster IP
    • Service NodePort
    • ClusterIP Exercise
    • NodePort Exercise
    • Service LoadBalancer
    • Configmap
    • Secrets
  • Application Lifecycle Management
    • Rolling Updates and Rollback
    • Multi Container Pod
  • K8s Storage
    • Persistent Volume
    • Persistent Volume Claim
    • PVC in POD
  • Istio
    • AKS Setup
    • Kubectl Setup in Windows
    • Istio Setup Manual
    • Istio Demo App
    • Istio Observability
  • Terraform
    • Terraform Setup
    • Terraform Demo
    • Azure Terraform Setup
  • Terraform AWS
    • Terraform AWS Setup
    • Terraform AWS Demo S3
  • Refrences
    • Docker Static Site
    • Docker First Image
Powered by GitBook
On this page
  1. Docker Fundamentals

Docker Storage

Using Volumes for Persistent Storage

In this lesson, we will take a deeper look into using volumes with our Docker containers. Volumes are the preferred method for maintaining persistent data.

Volumes are easier to back up or migrate than bind mounts. You can manage volumes using Docker CLI commands or the Docker API. They work on both Linux and Windows containers. Volumes can be more safely shared among multiple containers. Volume drivers allow for:

  • Storing volumes on remote hosts or cloud providers

  • Encrypting the contents of volumes

  • Add other functionality

  • New volumes can have their content pre-populated by a container.

Step 1: Create a new volume for an Nginx container:

docker volume create html-volume

Step 2: Creating a volume using that volume mount:

docker container run -d \
 --name nginx-volume1 \
 --mount type=volume,source=html-volume,target=/usr/share/nginx/html/ \
 nginx

Step 3: Inspect the volume:

docker volume inspect html-volume

Step 4: List the contents of html-volume:

sudo ls /var/lib/docker/volumes/html-volume/_data

Step 5: Creating a volume using that volume flag:

docker container run -d \
 --name nginx-volume2 \
 -v html-volume:/usr/share/nginx/html/ \
 nginx

Step 6: Edit index.html ( From outside the Container )

sudo vi /var/lib/docker/volumes/html-volume/_data/index.html

Step 7: Inspect nginx-volume2 to get the private IP:

docker container inspect nginx-volume2

Step 8: Login into nginx-volume1 and go to the html directory

docker container exec -it nginx-volume1 /bin/bash
cd /usr/share/nginx/html
cat index.hml

Install Vim ( inside the container, if you dont have any editor )

apt-get update -y
apt-get install vim -y

Step 9: Using a readonly volume:

docker run -d \
  --name=nginx-volume3 \
  --mount source=html-volume,target=/usr/share/nginx/html,readonly \
  nginx

Step 10: Login into nginx-volume3 and go to the html directory:

docker container exec -it nginx-volume3 /bin/bash
cd /usr/share/nginx/html
cat index.hml

Install Vim:

apt-get update -y
apt-get install vim -y
PreviousDocker Volume BasicsNextDockerfile Fundamentals

Last updated 2 years ago