-v ${local_path}:${docker_path}??
Docker提供了三种不同的方式将数据从宿主机挂载到容器中:volume、bind mounts、tmpfs mounts
volume:Docker管理宿主机文件系统的一部分(/var/lib/docker/volumes)
bind mounts:可以存储在宿主机系统的任意位置
tmpfs mounts:挂载存储在宿主机的内存中,不会写入宿主机的文件系统
volume
oot@prometheus204:~# docker volume create for_nginx
for_nginx
root@prometheus204:~# docker volume ls
DRIVER VOLUME NAME
local 18ac2738fb4027ebd0c3a4ba0bc6fafdce8d0376ef3df43707a357c2ca321cba
local docker-prometheus_grafana_data
local docker-prometheus_prometheus_data
local for_nginx
root@prometheus204:~# docker run --name munginx -d -p 80:80 --mount type=volume,source=for_nginx,target=/usr/share/nginx/html nginx:1.23.3
61545a6faf3e4884bb14c6d7d268ac9d104f4ef7a250d434c65399fee03cf990
root@prometheus204:~# cd /var/lib/docker/volumes/for_nginx/
root@prometheus204:/var/lib/docker/volumes/for_nginx# ls
_data
root@prometheus204:/var/lib/docker/volumes/for_nginx# cd _data/
root@prometheus204:/var/lib/docker/volumes/for_nginx/_data# ls
50x.html index.html
root@prometheus204:/var/lib/docker/volumes/for_nginx/_data# echo hello,123 > index.html
root@prometheus204:/var/lib/docker/volumes/for_nginx/_data#
# 访问nginx 显示 hello,123
bind mounts(绑定数据卷)
?-v ${local_path}:${docker_path}?
-v ${local_path}:${docker_path}
运行容器并设置挂载(正常方式)
docker run --name mynginx1 -d -p 80:80 \
--mount type=bind,source=/home/docker/volumes/nginx,target=/usr/share/nginx/html \
nginx:1.23.3
# 运行容器并设置挂载(-v方式)简写
docker run --name mynginx1 -d -p 80:80 \
-v /home/docker/volumes/nginx:/usr/share/nginx/html \
nginx:1.23.3
?tmpfs mounts(临时数据卷)
# 挂载方式运行容器
docker run --name m1nginx -d --mount type=tmpfs,target=/usr/share/nginx/html nginx:1.23.3
# 进入容器
docker exec -it m1nginx /bin/bash
# cd到具体目录
cd usr/share/nginx/html/
# 创建test.html文件
echo "container write info" > test.html
volumes:
bind mount:
tmpfs mount: