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
Linux Containers: The Foundation of DockerLinux Containers: The Foundation of DockerApr 14, 2025 am 12:14 AM

LXC is the foundation of Docker, and it realizes resource and environment isolation through cgroups and namespaces of the Linux kernel. 1) Resource isolation: cgroups limit CPU, memory and other resources. 2) Environment isolation: namespaces provides independent process, network, and file system views.

Docker on Linux: Best Practices and TipsDocker on Linux: Best Practices and TipsApr 13, 2025 am 12:15 AM

Best practices for using Docker on Linux include: 1. Create and run containers using dockerrun commands, 2. Use DockerCompose to manage multi-container applications, 3. Regularly clean unused images and containers, 4. Use multi-stage construction to optimize image size, 5. Limit container resource usage to improve security, and 6. Follow Dockerfile best practices to improve readability and maintenance. These practices can help users use Docker efficiently, avoid common problems and optimize containerized applications.

Using Docker with Linux: A Comprehensive GuideUsing Docker with Linux: A Comprehensive GuideApr 12, 2025 am 12:07 AM

Using Docker on Linux can improve development and deployment efficiency. 1. Install Docker: Use scripts to install Docker on Ubuntu. 2. Verify the installation: Run sudodockerrunhello-world. 3. Basic usage: Create an Nginx container dockerrun-namemy-nginx-p8080:80-dnginx. 4. Advanced usage: Create a custom image, build and run using Dockerfile. 5. Optimization and Best Practices: Follow best practices for writing Dockerfiles using multi-stage builds and DockerCompose.

Docker Monitoring: Gathering Metrics and Tracking Container HealthDocker Monitoring: Gathering Metrics and Tracking Container HealthApr 10, 2025 am 09:39 AM

The core of Docker monitoring is to collect and analyze the operating data of containers, mainly including indicators such as CPU usage, memory usage, network traffic and disk I/O. By using tools such as Prometheus, Grafana and cAdvisor, comprehensive monitoring and performance optimization of containers can be achieved.

Docker Swarm: Building Scalable and Resilient Container ClustersDocker Swarm: Building Scalable and Resilient Container ClustersApr 09, 2025 am 12:11 AM

DockerSwarm can be used to build scalable and highly available container clusters. 1) Initialize the Swarm cluster using dockerswarminit. 2) Join the Swarm cluster to use dockerswarmjoin--token:. 3) Create a service using dockerservicecreate-namemy-nginx--replicas3nginx. 4) Deploy complex services using dockerstackdeploy-cdocker-compose.ymlmyapp.

Docker with Kubernetes: Container Orchestration for Enterprise ApplicationsDocker with Kubernetes: Container Orchestration for Enterprise ApplicationsApr 08, 2025 am 12:07 AM

How to use Docker and Kubernetes to perform container orchestration of enterprise applications? Implement it through the following steps: Create a Docker image and push it to DockerHub. Create Deployment and Service in Kubernetes to deploy applications. Use Ingress to manage external access. Apply performance optimization and best practices such as multi-stage construction and resource constraints.

Docker Troubleshooting: Diagnosing and Resolving Common IssuesDocker Troubleshooting: Diagnosing and Resolving Common IssuesApr 07, 2025 am 12:15 AM

Docker FAQs can be diagnosed and solved through the following steps: 1. View container status and logs, 2. Check network configuration, 3. Ensure that the volume mounts correctly. Through these methods, problems in Docker can be quickly located and fixed, improving system stability and performance.

Docker Interview Questions: Ace Your DevOps Engineering InterviewDocker Interview Questions: Ace Your DevOps Engineering InterviewApr 06, 2025 am 12:01 AM

Docker is a must-have skill for DevOps engineers. 1.Docker is an open source containerized platform that achieves isolation and portability by packaging applications and their dependencies into containers. 2. Docker works with namespaces, control groups and federated file systems. 3. Basic usage includes creating, running and managing containers. 4. Advanced usage includes using DockerCompose to manage multi-container applications. 5. Common errors include container failure, port mapping problems, and data persistence problems. Debugging skills include viewing logs, entering containers, and viewing detailed information. 6. Performance optimization and best practices include image optimization, resource constraints, network optimization and best practices for using Dockerfile.

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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools