2

What is a container data volume

The docker concept packages applications and environments into one image!
If the data is all in the container, then we delete the container and the data will be lost!
There can be a data sharing technology between containers! The data generated in the Docker container is synchronized to the local! This requires the use of data volumes, which is to mount the directory in our container to Linux!

Summary: The persistence and synchronization operations of containers can also share data between containers!

Use data volumes

Direct command mount -v

 docker run -it -v 主机目录:容器内的目录

# 启动后可以使用下面的命令查看挂载有没有成功
docker inspect 容器id 
# 可以在 Mounts 内查看

2. Test
1. Add or modify files locally to see if there are any changes in the container
2. Add or modify the container to check whether the local directory has been changed synchronously

Named and anonymous mounts

1. Anonymous: only write the path inside the container, not the path outside the container

 -v 容器内路径
docker run -d -p --name nginx01 -v /etc/nginx nginx

# 查看所有卷的情况
docker volume ls
#发现 数据

#

2. Named

 通过-v 卷名:容器内路径
docker run -d -p --name nginx01 -v [卷名]:/etc/nginx nginx
# 查看卷
docker volume inspect 卷名

# 一旦设置了容器权限 容器对我们挂在出来的容器就有限定了
ro readonly # 只读
rw readwrite # 可读写
docker run -d -p --name nginx01 -v [卷名]:/etc/nginx:ro nginx

docker run -d -p --name nginx01 -v [卷名]:/etc/nginx:rw nginx

Dockfile mount

1. Create a Dockfile

 FROM nginx
# 匿名挂载
VOLUME ["test1", "test2"]
CMD echo "----end---"
CMD /bin/bash

2. Command run

 # 构建镜像
docker build -f deockfile所在目录 -t shuai/nginx:1.0 .

# 启动容器
docker run  

# 查看是否挂载成功

code_shuai
818 声望32 粉丝

love money = love code