search
HomeOperation and MaintenanceDockerDocker summarizes and shares data volume technology

This article brings you the relevant knowledge of data volume technology summarized and shared by Docker. I hope it will be helpful to everyone.

Docker summarizes and shares data volume technology

Docker data volume technology

Docker summarizes and shares data volume technology

##What is a container data volume

Docker’s concept review

Package the application and environment into a mirror!

data? If the data is in the container, then if we delete the container, the data will be lost! Requirement: Data can be persisted

MySQL. If the container is deleted, the database will be deleted and run away ---> Requirement: MySQL data can be stored locally!

There can be a data sharing technology between containers! The data generated in the Docker container is synchronized to the local!

This is roll technology! Directory mounting, mount the directory in our container to Linux×!

Docker summarizes and shares data volume technology

Summary: persistence and synchronization operations of containers! Data sharing can also be achieved between containers!

Using data volumes

docker run -it -v 宿主机目录: 容器目录 -p 主机端口:容器端口 容器id

Practical exercises

inspect to view synchronization details

Docker summarizes and shares data volume technology

Create a file in the container and see if it is synchronized to the local folder

Docker summarizes and shares data volume technology

When the container is closed, modify the local file and see the modification Will the file be synchronized to the container?

Docker summarizes and shares data volume technology

Mysql actual combat

Command

docker run -d -p 3310:3306 -v /root/mysql/conf:/etc/mysql/conf.d -v /root/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7

Parameters

-d Background operation

-p Port mapping

-v Data volume mount: synchronize data

-e Set environment variables: The MySQL login password is set here

--name Container name

Docker summarizes and shares data volume technology##Test result: Connection successful

Docker summarizes and shares data volume technologyProblems encountered

The latest version of mysql used first: 8.0----When using navicat to connect, an inexplicable error is always reported

Solution: Switch to version 5.7, there is no problem

When we run the mirror mysql, we do not add -e MYSQL_ROOT_PASSWORD=123456 to set the password. The container will always be closed, even if it is restarted. It will be turned on

Add the -e MYSQL_ROOT_PASSWORD=123456 parameter to turn it on perfectly

Even if we delete the container, the data volume we mounted to the local is still not lost, which realizes the container data Persistence function

Named mount and anonymous mount

Anonymous mount-v Path within the container!

docker run -d --name nginx01 -v /etc/nginx nginx

Here we found that this is an anonymous mount. We only wrote the path inside the container after -v, but did not write the path outside the container.

View the situation of all volumes

docker volume ls

Docker summarizes and shares data volume technology

Named mount-v Name of the mounted volume: path within the container

docker run -d --name nginx01 -v 具名挂在名称:/etc/nginx nginx

Here we find that this is an anonymous mount. We write the path inside the container after -v, and there is no path outside the container, but there is a name

Docker summarizes and shares data volume technologyDocker’s content directory:/var/lib/docker

All volumes in the docker container, if no directory is specified, are in:/var/lib/docker/volume /xx/_data

We can easily find one of our volumes through named mounting, and the most commonly used method when using volumes is named mounting

Docker summarizes and shares data volume technology

Additional knowledgeHow to distinguish between named mounting and anonymous mounting, specify the path to mount

-v path inside the container # Anonymous mount

-v Volume name: path inside the container #Named mount

-v /path outside the container: path inside the container #Specify path mount

Extension

-v Path within the container: ro rw Change read and write permissions

ro readonly Read only

rw readwrite Readable and writable

一旦设置了这个容器权限,容器对我们挂载出来的内容就有限定了

docker run -d --name nginx01 -v 具名挂在名称:/etc/nginx:ro nginx
docker run -d --name nginx01 -v 具名挂在名称:/etc/nginx:rw nginx

Ro 只要看到ro就说明这个路径只能通过宿主机来进行操作,容器内是无法操作的!

初识Dockerfile

dockerfile

dockerfile就是用来构建docker镜像文件的!命令脚本!先体验一下

通过这个脚本可以生成镜像,镜像是一层一层的,脚本是一个个的命令,每个命令都是一层!

写一个dockerfile

# 创立一个dockerfile文件,名字可以随便的取,最好叫做dockerfile
# 文件中的内容 指令(大写) 参数
FROM centos
VOLUME ["volme01","volume02"]
CMD echo "--------end----------"
CMD /bin/bash
#这里每个指令就是镜像的一层

使用dockerfile生成镜像

docker build -f dockerfile文件位置 -t 镜像名称和版本 镜像生成的位置

Docker summarizes and shares data volume technology

进入镜像查看详情

Docker summarizes and shares data volume technology

查看卷的同步目录

docker ps -a

docker inspect 容器id

Docker summarizes and shares data volume technology

最后测试两个文件夹中是不是同步

同步成功

总结

使用dockerfile构建镜像的方式在我们未来的使用中非常的多,因为我们通常会构建自己的镜像

假设构建镜像时候没有挂载卷,要手动镜像挂载-v

数据卷容器

实际上即使保证的容器之间的数据共享的问题

Docker summarizes and shares data volume technology

数据卷容器实际上就是被拷贝数据的容器(A- volumes-from ->B,B是数据卷容器)

测试两个镜像之间同步

首先先开三个容器

创建docker01

Docker summarizes and shares data volume technology

创建docker02  --volumes-from docekr01

Docker summarizes and shares data volume technology

创建docker03  --volumes-from docekr02

Docker summarizes and shares data volume technology

查看docker01和docker03之间的数据共享

Docker summarizes and shares data volume technology

接着我们将docker02删除,查看docker01和docker03之间的数据共享

Docker summarizes and shares data volume technology

总结:

Docker summarizes and shares data volume technology

实现多个mysql之间的数据共享

  docker run -d -p 3310:3306 -v /root/mysql/conf:/etc/mysql/conf.d -v /root/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7
  docker run -d -p 3310:3306 --volumes-from mysql01 -e MYSQL_ROOT_PASSWORD=123456 --name 
  mysql02 mysql:5.7

总结:

容器之间配置信息的传递。数据卷容器的声明周期一直持续到没有容器使用位置

理解:容器之间只要是共享就会数据copy,即使有的容器被删除,数据依然存在,直到所有共享 的容器都删除,数据才会被彻底删除

但是一旦你持久化到了本地,这个时候,本地的数据是不会删除的。

推荐学习:《docker视频教程

The above is the detailed content of Docker summarizes and shares data volume technology. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金. If there is any infringement, please contact admin@php.cn delete
Mastering Docker: A Guide for Linux UsersMastering Docker: A Guide for Linux UsersApr 18, 2025 am 12:08 AM

Using Docker on Linux can improve development efficiency and simplify application deployment. 1) Pull Ubuntu image: dockerpullubuntu. 2) Run Ubuntu container: dockerrun-itubuntu/bin/bash. 3) Create Dockerfile containing nginx: FROMubuntu;RUNapt-getupdate&&apt-getinstall-ynginx;EXPOSE80. 4) Build the image: dockerbuild-tmy-nginx. 5) Run container: dockerrun-d-p8080:80

Docker on Linux: Applications and Use CasesDocker on Linux: Applications and Use CasesApr 17, 2025 am 12:10 AM

Docker simplifies application deployment and management on Linux. 1) Docker is a containerized platform that packages applications and their dependencies into lightweight and portable containers. 2) On Linux, Docker uses cgroups and namespaces to implement container isolation and resource management. 3) Basic usages include pulling images and running containers. Advanced usages such as DockerCompose can define multi-container applications. 4) Debug commonly used dockerlogs and dockerexec commands. 5) Performance optimization can reduce the image size through multi-stage construction, and keeping the Dockerfile simple is the best practice.

Docker: Containerizing Applications for Portability and ScalabilityDocker: Containerizing Applications for Portability and ScalabilityApr 16, 2025 am 12:09 AM

Docker is a Linux container technology-based tool used to package, distribute and run applications to improve application portability and scalability. 1) Dockerbuild and dockerrun commands can be used to build and run Docker containers. 2) DockerCompose is used to define and run multi-container Docker applications to simplify microservice management. 3) Using multi-stage construction can optimize the image size and improve the application startup speed. 4) Viewing container logs is an effective way to debug container problems.

How to start containers by dockerHow to start containers by dockerApr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to view logs from dockerHow to view logs from dockerApr 15, 2025 pm 12:24 PM

The methods to view Docker logs include: using the docker logs command, for example: docker logs CONTAINER_NAME Use the docker exec command to run /bin/sh and view the log file, for example: docker exec -it CONTAINER_NAME /bin/sh ; cat /var/log/CONTAINER_NAME.log Use the docker-compose logs command of Docker Compose, for example: docker-compose -f docker-com

How to check the name of the docker containerHow to check the name of the docker containerApr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to create containers for dockerHow to create containers for dockerApr 15, 2025 pm 12:18 PM

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

How to exit the container by dockerHow to exit the container by dockerApr 15, 2025 pm 12:15 PM

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop <container_name> Command Use docker kill <container_name> command in the host terminal (force exit)

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor