Implementation of Docker data volume operations

Implementation of Docker data volume operations

Getting Started with Data Volumes

In the previous case, if we need to copy data from the host to the container, we usually use Docker's copy command, but the performance is still a little poor. Is there no way to make this copy reach the local disk I/O performance? have!

Data volumes can bypass the copy system and share directories or files between multiple containers or between containers and the host. Data volumes bypass the copy system and can achieve local disk I/O performance.

This article first uses a simple case to show readers the basic usage of data volumes.

Taking the nginx image used above as an example, when running the container, you can specify a data volume with the following command:

docker run -itd --name nginx -v /usr/share/nginx/html/ -p 80:80 bc26f1ed35cf

The operation effect is as follows:

At this point, we have created a data volume and mounted it to the /usr/share/nginx/html/ directory of the container. As you know, this directory is actually the html directory where nginx saves. By mounting the data volume here, we only need to modify the local mapping location to modify the page.

Next, use the docker inspect command to view the details of the container just created and find the data volume mapping directory, as follows:

As you can see, Docker uses the host's /var/lib/docker/volumes/0746bdcfc045b237a6fe2288a3af9d7b80136cacb3e965db65a212627e217d75/_data directory as the source directory by default. Next, enter the directory as follows:

At this time, it is found that the file content under this directory is consistent with the file content under the /usr/share/nginx/html/ directory in the container. This is because when an empty data volume is mounted to a non-empty directory in the container, the files in this directory will be copied to the data volume (if a non-empty data volume is mounted to a directory in the container, the data in the data volume will be displayed in the directory in the container. If there is data in the directory in the original container, the original data will be hidden).

Tips:

Because Docker on Mac is a bit special, the /var/lib/xxxx directory mentioned above can be directly entered if it is in a Linux environment. If it is in a Mac, you need to first execute the following command to enter the /var/lib/xxx directory in the newly entered command line:

screen ~/Library/Containers/com.docker.docker/Data/vms/0/tty

Next, modify the content of the index.html file in the file as follows:

echo "hello volumes">index.html

After the modification is completed, return to the browser and enter http://localhost to view the data in the index.html page in nginx, and find that it has changed. This indicates that the files in the host machine are shared with the container.

Combined with the host directory

The usage of data volumes in the above is not the best solution. Generally speaking, we may need to explicitly specify that a directory on the host machine should be mounted into the container. The specification method is as follows:

docker run -itd --name nginx -v /Users/sang/blog/docker/docker/:/usr/share/nginx/html/ -p 80:80 bc26f1ed35cf

This will mount /Users/sang/blog/docker/docker/ directory in the host to the /usr/share/nginx/html/ directory in the container. Next, readers only need to add html files in /Users/sang/blog/docker/docker/ directory, or modify html files, and the effect can be immediately seen in nginx access.

This usage is very convenient for development and testing, without the need to redeploy or restart the container.

Note: The host address is an absolute path

More Operations

Data volumes in Dockerfile

If the developer uses Dockerfile to build the image, they can also declare the data volume when building the image, for example:

FROM nginx
ADD https://www.baidu.com/img/bd_logo1.png /usr/share/nginx/html/
RUN echo "hello docker volume!">/usr/share/nginx/html/index.html
VOLUME /usr/share/nginx/html/

In this way, an anonymous data volume is configured. During operation, data is written to the /usr/share/nginx/html/ directory, which can achieve stateless changes in the container storage layer.

View all data volumes

Use the following command to view all data volumes:

docker volume ls

As shown in the figure:

View data volume details

You can view data details according to the volume name, as follows:

docker volume inspect

The execution result is as follows:

Deleting a Data Volume

You can use the docker volume rm command to delete a data volume, or you can use docker volume prune to delete data volumes in batches, as follows:

During batch deletion, all data volumes were not deleted, and one was left. This is because the data volume is still in use. Stop and remove the related containers, and then delete the data volume again to successfully delete it, as shown in the figure:

Data volume container

A data volume container is a container specifically used to mount data volumes. This container is mainly referenced and used by other containers. The so-called data volume container is actually an ordinary container, as shown below:

  • Create a data volume container

Create a data volume container using the following method:

docker run -itd -v /usr/share/nginx/html/ --name mydata ubuntu

The command execution effect is as follows:

  • Reference Container

Use the following command to reference the data volume container:

docker run -itd --volumes-from mydata -p 80:80 --name nginx1 nginx
docker run -itd --volumes-from mydata -p 81:80 --name nginx2 nginx

At this point, nginx1 and nginx2 both mount the same data volume to the /usr/share/nginx/html/ directory. If any of the three containers modifies a file in the directory, the other two can see the change.

At this point, use the docker inspect command to view the details of the container and find that the descriptions of the data volumes of the three containers are consistent, as shown below:

Summarize

This article mainly introduces the container operations in data volumes. Overall, it is very simple. Friends, have you learned it?

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Docker data volume container creation and usage analysis
  • Detailed explanation of Docker container data volumes
  • Using Docker data volumes and container orchestration
  • Detailed explanation of Docker data management (data volumes & data volume containers)
  • Advanced Docker Volumes
  • Detailed explanation of Docker data volume management
  • Docker data volume common operation code examples

<<:  Detailed explanation of the command mode in Javascript practice

>>:  How to solve the mysql ERROR 1045 (28000)-- Access denied for user problem

Recommend

Detailed explanation of the usage and difference between nohup and & in Linux

Example: We use the Python code loop_hello.py as ...

Vue implements carousel animation

This article example shares the specific code of ...

mysql data insert, update and delete details

Table of contents 1. Insert 2. Update 3. Delete 1...

Web page HTML ordered list ol and unordered list ul

Lists for organizing data After learning so many ...

Solution to MySQL master-slave delay problem

Today we will look at why master-slave delay occu...

How to modify create-react-app's configuration without using eject

1. Why is eject not recommended? 1. What changes ...

Linux process management tool supervisor installation and configuration tutorial

Environment: CentOS 7 Official documentation: htt...

How to change the color of the entire row (tr) when the mouse stops in HTML

Use pure CSS to change the background color of a ...

Graphic tutorial on installing Mac system in virtual machine under win10

1. Download the virtual machine version 15.5.1 I ...

Complete steps of centos cloning linux virtual machine sharing

Preface When a Linux is fully set up, you can use...

The practical process of login status management in the vuex project

Table of contents tool: Login scenario: practice:...

Detailed explanation of Vue's props configuration

<template> <div class="demo"&g...