Docker volumes are a crucial part in containerized conditions providing a technique to persist and manage information across containers. Basically, a volume in Docker is a catalog (or registry like information) that exists outside the containers’ filesystems however it is open to the container. This partition permits information to persist regardless of whether the compartment is halted or erased, empowering information sharing and perseverance between containers, as well as working with information reinforcement and migration.These commands allows clients to make volumes, list existing volumes, review volume details, and eliminate volumes when they are not generally required. Docker volume commands empower designers and administrators to productively deal with information related tasks inside Docker containers, improving adaptability, unwavering quality, and simplicity and scalability of the board in containerized conditions.
Understanding Docker volume commands is fundamental for Docker users who need to manage information persistency and sharing really inside their containerized applications. By utilizing these commands, clients or users can ensure the data trustworthiness, empower consistent information move among holders, and smooth out the organization and the executives of Docker-based applications.
In this article we will guide you what is docker and what is docker daemon and types of volumes in docker and finally how the docker volume commands works exactly.
What Is docker?
Docker is a platform and toolset that permits you to develop, deploy, and run applications inside containers. containers are lightweight, independent,standalone executable packages that include everything expected to run an application: code, runtime, framework apparatuses, libraries,dependencies and settings. Docker gives a deliberation(abstraction) layer that allows you to package and detach,isolate applications in a consistent manner, no matter what is the underlying infrastructure.
Understanding Of Primary Terminologies Related To Docker
- Docker Engine: The runtime environment that executes containers. It includes the Docker daemon, which manages container lifecycle, and the Docker CLI (Command-Line Interface), which allows users to interact with Docker through commands.
- Docker Image: A read-only template containing the application code, runtime, libraries, and dependencies required to run the application. Images are used as the basis for creating containers.
- Docker Container: An instance of a Docker image that runs as a standalone, isolated environment. Containers encapsulate the application and its dependencies, ensuring consistency and portability across different environments.
Docker Volumes
There are three main use cases for Docker Volumes as follows:
- To keep data around when a container is removed
- To share data between the host filesystem and the Docker container
- To share data with other Docker containers
By Docker Volumes, we are essentially going to look at how to manage data within the docker containers
- Persistent Data: A data or information that is infrequently accessed and not likely to be modified. Volumes permit you to store and keep up with information separate from the holder, guaranteeing information trustworthiness and constancy across containers lifecycles.
- Share Data Between Containers: Volumes can be mounted into multiple containers, enabling data sharing and collaboration between different parts of an application or between different applications.
- Manage Data Backup And Migration: Volumes provide a convenient way to back up and migrate data between environments, facilitating application deployment and disaster recovery processes.
- Overall, Docker volumes play a vital role in Docker-based applications by providing a flexible and efficient mechanism for handling persistent data storage and management.
Types Of Docker Volumes
There are three types of volumes in docker:
1) Named volumes
2) Host bind mounts
3) Anonymous volumes
1. Named volumes
- This type of volume is similar to an anonymous volume.
- Docker manages where on disk the volume is created, but
- you give it a volume name, so that managing becomes easy
docker run -v name:/path/in/container
2. Host Volumes / Bind Mount
- This type of volume is managed by user, as the data is stored wherever the user wants
docker run -v /path/on/host:/path/in/container
3. Anonymous Volumes
- This volume is useful, when you would rather have Docker handle where the files are going to be stored
- It can be difficult to manage if you have multiple anonymous volumes
docker run -v /path/in/container
Implementation Of Docker Volume Commands: A Step-By-Step Guide
Step 1 : Launch Instance In AWS Account
Launch An Instance With Configuration:
- Use AMI as amazon Linux 2
- instance type- t2.micro
- Security group- allow SSH(22),HTTP(80),HTTPS(443) traffic from anywhere
- Configure storage – 8gb with root volume type gp2
- Connect this instance with any CLI terminal by using SSH
ssh -i "keypair-pem file" ec2-user@<instance-public-ip address>compute-1.amazonaws.com
Step 2: Installing Docker
- Install docker using yum command
- Start and enable docker
- Now, docker daemon is created and set the permissions to this docker-daemon to read and write permissions.
sudo yum install -y docker
- To Start the docker service use the following command:
sudo systemctl start docker
- To Enable the Docker service to running state at the time of booting only use the following command:
sudo systemctl enable docker
- Provide the permissions to the docker socket with the following command:
sudo chmod 666 /var/run/docker.sock
In Docker, volumes are a way to persist data generated by and used by Docker containers. They allow data to be stored separately from the container itself, which enables easier management and sharing of data between containers. Here are some common Docker volume commands:
Docker volume Commands
Execute the below docker volume commands for better understanding:
docker volume create
- This command creates a new volume. You specify the name of the volume as an argument.
docker volume create my_volume
docker volume ls
- This command lists all volumes that exist on the Docker host.
docker volume ls
Docker Volume Inspect
- This command provides detailed information about a specific volume, including its configuration and usage.
docker volume inspect my_volume
Docker Volume rm
- This command removes one or more volumes. You specify the names of the volumes to remove as arguments.
docker volume rm my_volume
Docker Volume Prune
- This command removes all unused volumes from the host.
docker volume prune
Docker Run -v
- This command mounts a volume into a container when it’s launched. You specify the name of the volume and the path within the container where it should be mounted.
docker run -v my_volume:/path/in/container image_name
Docker Run –mount
- This is another way to mount volumes into containers, offering more flexibility in specifying volume options, such as read-only or read-write mode.
docker run --mount source=my_volume,target=/path/in/container,readonly image_name
Docker Volume Commands – FAQ’s
How Many Types Of Volumes Are There In Docker?
Docker volumes are used to persist data from within a Docker container
- Named volume
- Hosts or bind volume
- Anonymous volume
Where Are Our Created Docker Volumes Located ?
By default, Docker stores its volumes in the directory located at
cd /var/lib/docker/volumes
How Do I Attach A Docker Volume To A Container?
You can attach a Docker volume to a container when launching the container using the -v or –mount option. These options allow you to specify the volume name and the path within the container where the volume should be mounted.
docker run -v volume_name:/path/in/container image_name
Can We Attach Docker Volume From Container To Another Container?
Yes, you can attach a Docker volume that is already attached to one container to another container without losing data.
by executing this command you will achieve the task
docker run -itd --volumes-from existing_container_name --name new_container_name image_name