Docker Volumes

Docker Volumes


Docker volumes let you save data used by your containers, so it doesn’t disappear when the container stops or is removed. Think of it as a way to keep your important files safe and share them across containers.


Why Use Docker Volumes?

  1. Keep Data Safe: Save your container’s data even after it stops.
  2. Share Data: Let multiple containers use the same data.
  3. Better Speed: Volumes are faster than other ways of sharing files.
  4. Easy Backups: You can back up your data easily from volumes.


Types of Docker Volumes

  1. Anonymous Volumes: Created automatically by Docker. These don’t have names and are harder to manage.
  2. Named Volumes: You create these with a name and can reuse them in multiple containers.
  3. Bind Mounts: Link a folder on your computer to a folder inside the container. They’re flexible but less isolated.




Hands-On: Using Docker Volumes with MySQL

Step 1: Get the MySQL Image

Download the MySQL image:

docker pull mysql:latest        
Article content

Step 2: Create a Volume

Create a named volume to store MySQL’s data:

docker volume create mysql-data        

Check if it’s created:

docker volume ls        

You should see mysql-data in the list.

Article content

Step 3: Start the MySQL Container

Run the MySQL container and use the volume:

docker run -d  --name mysql-container -e MYSQL_ROOT_PASSWORD=password -v mysql-data:/var/lib/mysql mysql:latest        

What this does:

  • -d: Runs the container in the background.
  • --name: Gives the container a name (mysql-container).
  • -e MYSQL_ROOT_PASSWORD=password: Sets the MySQL root password to password.
  • -v mysql-data:/var/lib/mysql: Uses the mysql-data volume to store database files.


Check if It’s Running

docker ps        

You should see mysql-container in the list.

Article content

Step 4: Access MySQL

Enter the MySQL shell:

docker exec -it mysql-container mysql -u root -p        

Type the password (password) when asked.

show databases ;        

Displays all the databases created.

create database demo ;        

Creates a Database "demo".

exit        

Exits the interactive terminal.

Article content

Step 5: Stop and Remove the Container

Stop the container:

docker stop mysql-container        

Remove the container:

docker rm mysql-container        


Article content

Step 6: Check Data Persistence

Run a new container with the same volume:

docker run -d  --name mysql-container -e MYSQL_ROOT_PASSWORD=password -v mysql-data:/var/lib/mysql mysql:latest        

The data from the old container will still be there, showing that volumes save your data.

Article content
















To view or add a comment, sign in

More articles by Rishabh Sharma

  • Docker Fundamentals : Hands On - II

    Understanding Dockerfile A Dockerfile is a simple text file containing a set of instructions used to build a Docker…

  • Docker Fundamentals : Hands On - I

    Setting Up Docker on AWS EC2 (Ubuntu) Step 1: Update the Package List Purpose: Updates the package list on your system…

  • Docker Fundamentals : Part III

    Understanding Docker Engine Docker Engine is the core software that enables the creation, management, and operation of…

  • Docker Fundamentals : Part II

    Understanding Virtualization Virtualization is a technology that allows multiple operating systems (OS) to run on a…

  • Docker Fundamentals : Part I

    Understanding Docker in Layman's Terms Imagine you bake a cake. To do this, you need specific ingredients, tools, and…

    1 Comment
  • Exploring GitHub

    If you are new to Git and Github , check out this blog for understanding git. What Is GitHub? GitHub is a web-based…

  • Version Control System: Git

    1. What is a Version Control System (VCS)? A Version Control System (VCS) is a crucial tool that enables developers to…

  • Understanding Shell Scripting

    Shell scripting is a powerful way to automate repetitive tasks, manage system processes, and handle system…

  • Basics of Shell Scripting

    What is a Shell Script? A shell script is simply a text file that contains a series of commands to be executed by the…

  • Linux System Management Tools: systemctl and find

    Linux system management can be both powerful and efficient when you know how to use the right command-line tools. Two…

Explore content categories