Docker Volumes: "A Comprehensive Guide"

Docker Volumes: "A Comprehensive Guide"

In Docker, a volume is a way to persist data generated by and used by Docker containers.

Here are some key points about Docker volumes:

  • Volume is simply a directory inside our container.

  • Firstly, we have to declare this directory as a volume and then share volume.

  • Even if we stop the container, still we can access volume.

  • Volume will be created in one container.

  • You can declare a directory as a volume only while creating container.

  • You can't create volume from existing container.

  • You can share one volume across any number of containers.

  • Volume will not be included when you update an image.

  • You can mapped volume in two ways:

  1. Container <-> Container

  2. Host <-> Container

🖇️Benefits of Docker Volumes

  • Decoupling container from storage.

  • Share volume among different container.

  • Attach volume to container.

  • On deleting container volume does not delete.

Here are some commonly used Docker volume commands:

Step 1: Launch an EC2 Instance

With the help of this: https://lalitakashyapblog.hashnode.dev/step-by-step-guide-to-setting-up-an-ec2-instance

Step 2: Install docker

With the help of this: https://lalitakashyapblog.hashnode.dev/from-zero-to-docker-a-beginners-introduction-to-containers

Step 3: Create docker container by pulling image from dockerhub of "mysql"

docker run -d --name mysql -e MYSQL_ROOT_PASSWORD=test@123 mysql:5.7

Step 4: Check container and image created or not

docker ps
docker images

Step 5: Execute docker container in interactive mode

docker exec -it <container_id> bash

Step 6: Give username to mysql & Password "test@123"

mysql -u root -p

Step 7: Show data

show databases;

Step 8: Create new database

create database kyc;

Step 9: Check database created or not

show databases;

Step 10: Create table in database

use kyc;
CREATE TABLE messages (
    id INT AUTO_INCREMENT PRIMARY KEY,
    message TEXT
);

Step 11: Insert messages in table

insert into messages (message) values ("lol");

Step 12: Check inserted message and exit from container

select * from messages;
exit
exit

Step 13: Now kill docker container and do docker ps

docker kill <container_ID>
docker ps

Step 14: Create volumes directory and move into it

mkdir volumes
cd volumes/

Step 15: Create directory for "mysql" and move into it

mkdir mysql-data
cd mysql-data/

Step 16: Mount the volume in present working directory

target path - /var/lib/mysql

docker run -d -v $(pwd):/var/lib/mysql --name mysql-safe -e MYSQL_ROOT_PASSWORD=test@123 mysql:5.7

Step 17: Check image & container are created or not and do ls

docker ps
docker images
ls

Step 18: Move into container

docker exec -it <container_ID> bash

Step 19: Follow "step 6" to "step 13"

Step 20: Check "kyc" database present in current directory

ls

Step 21: First remove "mysql-safe" name container

docker rm mysql-safe

Step 22: Again run below command to create mysql container

docker run -d -v $(pwd):/var/lib/mysql --name mysql-safe -e MYSQL_ROOT_PASSWORD=test@123 mysql:5.7

Step 23: Do docker ps

docker ps

Step 24: Follow "step 5" to "step 7" and your database is persist

Some other commands are:

  • List down all the volumes
docker volume ls
  • Create docker volume with name
docker volume create <volume_name>
  • Remove docker volume
docker volume rm <volume_name>
  • Remove all unused docker volume
docker volume prune
  • Inspect docker volume
docker volume inspect <volume_name>

We hope this guide has provided you with a solid foundation to create a docker volume and persist your data.

Thank you 🙏

Keep Learning..