containerd的挂载与docker的挂载方式一样
本人使用相对较少,自行查询资料吧
仅存储在主机系统的内存中,并且永远不会写入主机系统的文件系统
Volumes(数据卷)是一个可供一个或多个容器使用的位于宿主机上特殊目录。
它拥有以下特性:
使用数据卷的步骤一般分为两步:
第一步:创建一个数据卷
注:非必须。当进行第二步时,如果你挂载在一个还不存在的数据卷,nerdctl会自动创建它
第二步:nerdctl run启动容器时,使用-v
或--mount
参数将数据卷挂载容器指定目录中
提示:如果需要挂载多个卷,那么使用多个
-v
或--mount
参数即可
# --mount示例
# 示例:nerdctl run -d --name=jd-nginx -p 9527:80 --mount source=nginx-vol,destination=/usr/share/nginx/html/ nginx:latest
nerdctl run {其他参数} --mount source={挂载卷名},destination={容器中的目录} {镜像}
# -v示例
# 示例:nerdctl run -d --name=jd-nginx -p 9527:80 -v nginx-vol:/usr/share/nginx/html/ nginx:latest
nerdctl run {其他参数} -v {挂载卷名}:{容器中的目录} {镜像}
# 挂载多个示例
nerdctl run -d --name=jd-nginx -p 9527:80 --mount source=nginx-vol,destination=/usr/share/nginx/html/ --mount source=nginx-vol1,destination=/tmp/ nginx:latest
更多可参考here
[root@localhost tmp]# # 查看挂载卷
[root@localhost tmp]# nerdctl volume ls
[root@localhost tmp]# # 创建挂载卷nginx-vol
[root@localhost tmp]# nerdctl volume create nginx-vol
nginx-vol
[root@localhost tmp]#
[root@localhost tmp]# nerdctl volume ls
VOLUME NAME DIRECTORY
nginx-vol /var/lib/nerdctl/1935db59/volumes/default/my-vol/_data
[root@localhost tmp]# # 查看挂载卷详情
[root@localhost tmp]# nerdctl volume inspect nginx-vol
[
{
"Name": "nginx-vol",
"Mountpoint": "/var/lib/nerdctl/1935db59/volumes/default/nginx-vol/_data",
"Labels": {}
}
]
[root@localhost tmp]# # 启动容器,并指定挂债卷 --mount source={挂载卷名},destination={容器中的目录}
[root@localhost tmp]# nerdctl run -d --name=jd-nginx -p 9527:80 --mount source=nginx-vol,destination=/usr/share/nginx/html/ nginx:latest
c3e54529d11bbb3865a180548360539227efcde84133fc399f672e156c791d7d
[root@localhost _data]# # 查看容器
[root@localhost _data]# nerdctl ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c3e54529d11b docker.io/library/nginx:latest "/docker-entrypoint.…" 5 seconds ago Up 0.0.0.0:9527->80/tcp jd-nginx
[root@localhost _data]# 访问测试
[root@localhost _data]# curl localhost:9527
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost _data]# # 切换到挂载卷nginx-vol对应的存储位置
[root@localhost _data]# cd /var/lib/nerdctl/1935db59/volumes/default/nginx-vol/_data
[root@localhost _data]# ls
50x.html index.html
[root@localhost _data]# # 修改nginx首页, 将'Welcome to nginx!'修改为'Welcome to nginx123!'
[root@localhost _data]# vim index.html
[root@localhost _data]# # 再次访问测试,发现修改成功
[root@localhost _data]# curl localhost:9527
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx123!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost _data]#
Bind mounts模式和Volumes非常相似,不同点在于Bind mounts模式是将宿主机上的**任意文件(或文件夹)**挂载到容器,而Volumes本质上是将nerdctl服务管理的一块区域(默认是 /var/lib/nerdctl/ 下的某个子孙文件夹)挂载到容器
提示:如果需要挂载多个目录或文件,那么使用多个
-v
或--mount
参数即可
# --mount(挂载目录)示例
# 示例:nerdctl run -d --name=jd-nginx -p 9527:80 --mount type=bind,source=/usr/local/web/,destination=/usr/share/nginx/html/ nginx:latest
nerdctl run {其他参数} --mount type=bind,source={宿主机中的目录},destination={容器中的目录} {镜像}
# -v(挂载目录)示例
# 示例:nerdctl run -d --name=jd-nginx -p 9527:80 -v /usr/local/web/:/usr/share/nginx/html nginx:latest
nerdctl run {其他参数} -v {宿主机中的目录}:{容器中的目录} {镜像}
# --mount(挂载文件)示例
# 相当于启动时将host中的指定文件复制进容器中,会替换掉容器中的同名文件(如果容器中有同名文件的话),启动后就互相不影响了
nerdctl run -d --name=jd-nginx -p 9527:80 --mount type=bind,source=/usr/local/web/index.html,destination=/usr/share/nginx/html/index.html nginx:latest
# 挂载多个示例
nerdctl run -d --name=jd-nginx -p 9527:80 --mount type=bind,source=/usr/local/web/,destination=/usr/share/nginx/html/ --mount type=bind,source=/tmp/,destination=/tmp/ nginx:latest
[root@localhost _data]# # 先确保host中要挂载的目录存在
[root@localhost _data]# mkdir -p /usr/local/web/
[root@localhost _data]# # nerdctl run启动容器时,指定bind挂载, --mount type=bind,source={宿主机中的目录},destination={容器中的目录}
[root@localhost _data]# nerdctl run -d --name=jd-nginx -p 9527:80 --mount type=bind,source=/usr/local/web/,destination=/usr/share/nginx/html/ nginx:latest
76df148851c4d40171f05c06346dba2e484d99c582664d4063d24826391b429a
[root@localhost _data]# # 访问测试, 此时报错403,因为/usr/local/web/目录下没有index.html文件
[root@localhost _data]# curl localhost:9527
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.23.1</center>
</body>
</html>
[root@localhost _data]# # 找一个线程的index.html文件,移动至host中的挂载目录/usr/local/web/
[root@localhost _data]# mv /var/lib/nerdctl/1935db59/volumes/default/nginx-vol/_data/index.html /usr/local/web/
[root@localhost _data]# # 再次访问,成功了
[root@localhost _data]# curl localhost:9527
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx123!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost _data]#
- volumes卷挂载:如果容器中的待挂载的目录不是一个空目录,那么该目录下的文件会被复制到数据卷中
- bind mounts绑定挂载:使用bind mounts挂载宿主机目录到一个容器中的非空目录,那么此容器中的非空目录中的文件会被隐藏,容器访问这个目录时能够访问到的文件均来自于宿主机目录
声明:下面的结论是基于bind mounts(绑定挂载)
此时,完全以host上的文件夹为准
host | container | 挂载结果 |
---|---|---|
文件夹为空 | 文件夹不存在 or 存在但为空 or 存在且不为空 |
1. 确保container对应目录存在 2. 清空container对应目录,并使内容与host上对应文件夹保持一致 |
文件夹不为空 | 文件夹不存在 or 存在但为空 or 存在且不为空 |
1. 确保container对应目录存在 2. 清空container对应目录,并使内容与host上对应文件夹保持一致 |
注:若要挂载的host中的文件夹不存在,那么执行nerdctl run启动容器时,会启动失败,并打印对应的错误提示
注:上述冲突的处理,只发生在初始阶段。等container运行起来后,host中对文件夹内文件的操作会同步到container对应文件夹中,container中对文件夹内文件的操作会同步到host对应文件夹中
声明:下面的结论是基于bind mounts(绑定挂载)
声明:当前(0.22.0)版本的nerdctl对文件挂载的支持还不是很强
相当于启动时将host中的指定文件复制进容器中,会替换掉容器中的同名文件(如果容器中有同名文件的话),启动后就互相不影响了
host | container | 挂载结果 |
---|---|---|
不存在的文件 | 禁止行为(挂载报错) | |
存在的文件 | 不存在的文件 or 已经存在的文件 |
效果等同于 新增 or 覆盖 新增效果上等同于:host上的文件新增到container中 覆盖效果上等同于:以host上的文件覆盖container中的同名文件 |
注:上述冲突的处理,只发生在初始阶段。等container运行起来后,host中对该文件的操作不会影响到container中的对应文件;container中无法修改对应的文件(这明显不同于文件夹的挂载,同时也不是我们期望的可以操作互相影响的结果,这可能是当前(0.22.0)版本的nerdctl对文件挂载的支持还不是很强的原因)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。