绑定挂载
- 使用–mount 将当前目录下的target目录,挂载到容器的/app目录下
docker run -d \ -it \ --name devtest \ --mount type=bind,source="$(pwd)"/target,target=/app \ nginx:latest - 使用 -v 将当前目录下的target目录,挂载到容器的/app目录下
docker run -d \ -it \ --name devtest \ -v "$(pwd)"/target:/app \ nginx:latest - 只读模式挂载 --mount
docker run -d \ -it \ --name devtest \ --mount type=bind,source="$(pwd)"/target,target=/app,readonly \ nginx:latest - 只读模式挂载 -v
docker run -d \ -it \ --name devtest \ -v "$(pwd)"/target:/app:ro \ nginx:latest - 查看挂载详情(查看Mounts部分)
docker inspect devtest - 删除刚才的容器
docker container stop devtest docker container rm devtest
tmpfs
- 启动容器时挂载(使用–mount)
docker run -d \ -it \ --name tmptest \ --mount type=tmpfs,destination=/app \ nginx:latest - 使用 --tmpfs
docker run -d \ -it \ --name tmptest \ --tmpfs /app \ nginx:latest - 查看挂载详情
docker inspect tmptest --format '' - 挂载时指定参数
docker run -d \ -it \ --name tmptest \ --mount type=tmpfs,destination=/app,tmpfs-mode=1770,tmpfs-size=16k \ nginx:latest
本文演示了如何使用Docker的--mount和-v选项将本地目录挂载到容器中,包括只读模式,并介绍了tmpfs的使用,包括挂载参数的设置。同时,展示了如何查看挂载详情以及停止并删除容器。





