Docker chapter 3 colume mount VS bind mount

note1:

create a volume

docker volume create myvolume

note 2 :

run a container and mount a volume to persist data

cs 复制代码
docker run -dp 127.0.0.1:3000:3000 --mount type=volume,src=todo-db,target=/etc/todos getting-started
  • --mount type=volume,src=todo-db,target=/etc/todos 参数挂载了一个 Docker 卷(volume)。这个卷的名字是 todo-db,它被挂载到 Docker 容器的 /etc/todos 目录。这意味着 Docker 容器可以在 /etc/todos 目录中读写数据,这些数据实际上被存储在 Docker 宿主机的 todo-db 卷中。

note 3 :

inspect a volume particular message

cs 复制代码
docker volume  inspect  myvolume

[

{

"CreatedAt": "2024-02-07T07:05:59Z",

"Driver": "local",

"Labels": null,

"Mountpoint": "/var/lib/docker/volumes/todo-db/_data",

"Name": "todo-db",

"Options": null,

"Scope": "local"

}

]

The Mountpoint is the actual location of the data on the disk. Note that on most machines, you will need to have root access to access this directory from the host

note 4:

本地docker volume 的 数据 会 push到 远程吗

不会。Docker 的 push 命令只会将 Docker 镜像推送到远程的 Docker Registry,而不会推送 Docker 容器的数据,包括挂载的 volume。

Docker volume 中的数据是存储在 Docker 宿主机上的,它们是与 Docker 容器分开管理的。当你推送一个 Docker 镜像时,你只是推送了用于创建 Docker 容器的镜像,而不是容器的运行状态或者数据。

如果你需要将 Docker volume 中的数据备份或者迁移到其他地方,你需要使用其他的方法,例如使用 docker cp 命令来复制数据,或者使用 tar 命令来创建数据的归档文件,然后将归档文件复制到其他地方。

note 5 :

volume mount 和 bind mount 两者的使用场景的区别

Docker 提供了两种主要的挂载类型:volume mount 和 bind mount。它们都可以将数据持久化并共享到容器之外,但是使用场景和特性有所不同。

Volume Mounts

  • Volume mounts 是 Docker 管理的挂载,数据存储在 Docker 宿主机的 /var/lib/docker/volumes/ 目录下。
  • 适用于你不需要或不关心数据在宿主机上存储的具体位置,但希望 Docker 管理数据的生命周期。
  • 可以很容易地在容器之间共享和重用。
  • 可以使用 Docker CLI 或者 Docker API 来备份、恢复或迁移数据。
  • 适用于生产环境。

Bind Mounts

  • Bind mounts 可以将宿主机上的任何文件或目录挂载到容器中。
  • 适用于你需要控制数据在宿主机上存储的具体位置。
  • 可以用于开发环境,例如,将本地的源代码目录挂载到容器中,这样你就可以在本地编辑代码,而容器可以立即看到并使用这些更改。
  • 不适合在生产环境中使用,因为它可能会带来安全和文件权限的问题。

总的来说,如果你在开发环境中,需要在容器和宿主机之间共享文件,你可能会选择使用 bind mounts。如果你在生产环境中,需要数据持久化,你可能会选择使用 volume mounts。

note 6:

cs 复制代码
docker run -it --mount type=bind,src="$(pwd)",target=/src ubuntu bash
  • -it 参数让 Docker 容器在交互模式下运行,这意味着你可以在终端中输入命令并看到输出。

    -it 是两个 Docker 命令行参数的缩写:

  • -i--interactive:保持 STDIN 开启,即使没有附加到容器。

  • -t--tty:为容器分配一个伪终端。

  • 这两个参数通常一起使用(-it-ti),以便在新容器中启动一个交互式 shell。这样,你就可以在终端中输入命令并看到输出,就像在本地终端中一样

  • --mount type=bind,src="$(pwd)",target=/src 参数挂载了一个 bind mount。这个 bind mount 将当前工作目录($(pwd) 是一个 shell 命令,用于获取当前工作目录的路径)挂载到 Docker 容器的 /src 目录。这意味着 Docker 容器可以在 /src 目录中读写数据,这些数据实际上被存储在 Docker 宿主机的当前工作目录中。

  • ubuntu 是 Docker 容器的基础镜像,这里使用的是 Ubuntu Linux。

  • 所以,这个 docker run 命令的作用是:启动一个新的 Docker 容器,将当前工作目录挂载到 /src 目录,然后在容器中启动一个 bash shell

  • bash 是在 Docker 容器中运行的命令,这个命令启动了一个 bash shell,你可以在这个 shell 中输入命令并看到输出。

copy everything in current folder to target /path

相关推荐
最新小梦2 小时前
Docker日志管理
运维·docker·容器
ZHOU西口3 小时前
微服务实战系列之玩转Docker(十五)
nginx·docker·微服务·云原生·swarm·docker swarm·dockerui
lgbisha4 小时前
828华为云征文|华为云Flexus X实例docker部署最新Appsmith社区版,搭建自己的低代码平台
低代码·docker·华为云
记得开心一点嘛4 小时前
在Linux系统上使用Docker部署javaweb项目
linux·运维·docker
Persistence is gold6 小时前
cassandra指定配置文件的docker启动方法
运维·docker·容器
C语言扫地僧7 小时前
Docker 镜像制作(Dockerfile)
linux·服务器·docker·容器
ken_coding11 小时前
Windows11 WSL2的ubuntu 22.04中拉取镜像报错
linux·ubuntu·docker
Richardlygo11 小时前
(k8s)Kubernetes部署Promehteus
云原生·容器·kubernetes
炸裂狸花猫12 小时前
Kubernetes从零到精通(12-Ingress、Gateway API)
容器·kubernetes·gateway
自律的kkk14 小时前
docker配置镜像加速器
运维·docker·容器