管理数据
将数据从宿主机挂载到容器中的三种方式

volume
查看数据卷
创建数据卷
查看数据卷详细信息
删除数据卷
挂载案例演示
bind-mounts
挂在案例演示
综合案例
Last updated

Last updated
docker volume lsdocker volume create nginx_vol
# 创建名为 nginx_vol 的数据卷docker volume inspect nginx_vol
# 查看名为 nginx_vol 的数据卷的详细信息docker volume rm nginx_vol
# 删除名为 nginx_vol 的数据卷docker run --name web01 --mount type=volume,src=nginx_vol,dst=/usr/share/nginx/html -d -p 80:80 nginx:latest
# 以上命令中--mount部分也可使用-v方式书写,如下
# docker run --name web01 -v nginx_vol:/usr/share/nginx/html -d -p 80:80 nginx:latest
# 建议使用--mount
# 如果没有指定卷,则会自动创建
# type=volme 默认可省
# source / src 皆可
# destination / target / dst 皆可docker run --name web01 --mount type=bind,src=/mnt,dst=/usr/share/nginx/html -d -p 90:80 nginx:latest
# 如果挂载的宿主机源目录不存在则会直接报错
# 以上命令中--mount部分也可使用-v方式书写,如下
# docker run --name web01 -v /mnt:/usr/share/nginx/html -d -p 91:80 nginx:latest
# 建议使用 --mount 的方式书写docker run -d --name web01 \
-p 80:80 -p 81:81 \
--mount type=bind,src=/server/scripts/tmp.conf,dst=/etc/nginx/conf.d/tmp.conf \
--mount type=bind,src=/mnt,dst=/tmp \
nginx:latestcat > /server/scripts/tmp.conf << EOF
server {
listen 81;
server_name localhost;
location / {
root /tmp;
index index.html index.htm;
}
}
EOFdocker container cp /tmp/hello web01:/usr/share/nginx/html/
# 拷贝宿主机上的 /tmp/hello 至容器 web01 的/usr/share/nginx/html/下