【Docker】了解Docker Desktop桌面应用程序,TA是如何管理和运行Docker容器(2)

欢迎来到《小5讲堂》,大家好,我是全栈小5。
这是《Docker容器》系列文章,每篇文章将以博主理解的角度展开讲解,
特别是针对知识点的概念进行叙说,大部分文章将会对这些概念进行实际例子验证,以此达到加深对知识点的理解和掌握。
温馨提示:博主能力有限,理解水平有限,若有不对之处望指正!

在这里插入图片描述

前言

接着上篇提到的,Docker Desktop目前是没有官方提供的汉化版,它主要是使用英文界面。
所以,本文还是接着来了解下TA界面的基本内容和信息。

Volumes(存储)

持久化列表

我们可以从下面描述就i可以大概了解到Volumes作用

Containers can use volumes to store data(容器可以使用卷来存储数据)
All data in a container lost noce it is removed.(容器中的所有数据在被删除之前都会丢失)
Containers use volumes to persist data.(容器使用卷来持久化数据)在这里插入图片描述

创建Volume

  • 输入数据持久化名称
    在这里插入图片描述
  • 查看列表
    在这里插入图片描述
  • 关联容器

docker run -d -v test_volume_data:/app/data mycorehub

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
自动生成了一个容器,并且在对应目录下有一个文件夹app/data
在这里插入图片描述
在这里插入图片描述

  • 添加数据
    在这里插入图片描述
  • Volume界面
    会自动同步显示在Volume对应的Data里,所以,即时容器被移除,文件还会存在
    在这里插入图片描述

基本概念

Volumes 是用于管理 Docker 容器的持久化数据存储。
在 Docker 中,容器是临时性的,当容器重新启动或销毁时,容器内部的数据也会丢失。
但是,在某些情况下,我们需要保留容器中的数据,以便下一次启动容器时能够继续使用。

Volumes 允许将主机文件系统中的目录或文件与容器内的目录或文件进行关联。
这样,无论容器是否重新启动或销毁,与之关联的数据都会被保留。
通过 Docker Desktop 的图形界面或命令行工具,可以轻松创建和管理这些卷。
这为我们开发人员和运维人员提供了方便的方法来管理持久化数据,并简化了与容器数据相关的操作。

值得注意的是,Docker Volume 是一种独立于容器的实体,并可以在多个容器之间共享和重复使用
这使得数据在容器之间的移动和共享变得非常简单和灵活。

知识点学习

容器间数据

Persist your data between containers(在容器间保持数据,3分钟上手)
1、Container data is isolated from your local folders
在这里插入图片描述

Docker isolates all content, code, and data in a container from your local filesystem. This means, that when you delete a container within Docker Desktop, all the content within it is deleted.
Sometimes you may want to persist data that a container generated. This is when you can use volumes.

Docker将容器中的所有内容、代码和数据与本地文件系统隔离开来。这意味着,当您删除Docker Desktop中的容器时,其中的所有内容都会被删除。
有时,您可能希望持久化容器生成的数据。此时您可以使用卷。

2、Get the sample application
在这里插入图片描述

For this, you will be reusing the repository at https://github.com/docker/multi-container-app⁠.
git clone https://github.com/docker/multi-container-app
为此,您将在重新使用存储库

3、How volumes work在这里插入图片描述

If you want to persist data even after a container is deleted, you can use a volume. A volume is a location in your local filesystem, managed by Docker.
如果要在删除容器后仍保留数据,可以使用卷。卷是本地文件系统中的一个位置,由Docker管理。

4、Adding volumes to Compose
在这里插入图片描述

To add a volume to this project, simply go to the compose.yaml file and uncomment the following lines:

todo-database:
    # ...
    volumes:
      - database:/data/db
                      
# ...
volumes:
  database:

Digging deeper
The volumes element that is nested in todo-database tells Compose to mount the volume named database to /data/db in the container for the todo-database service.
The top-level volumes element defines and configures a volume named database that can be used by any of the services in the Compose file.

要向该项目添加卷,只需转到compose.yaml文件并取消注释以下行:
挖得更深
嵌套在todo-database中的volumes元素告诉Compose将名为database的卷装载到todo-data服务的容器中的/data/db。
顶级卷元素定义并配置名为卷的数据库,Compose文件中的任何服务都可以使用该数据库。

5、Delete adn restart
在这里插入图片描述

Now, no matter how often you delete and restart the container, your data is persisted and accessible to any container on your system by mounting the database volume. Docker will check for a volume and create one if there is none present.
现在,无论您多久删除和重新启动一次容器,您的数据都是持久的,并且可以通过装载数据库卷对系统上的任何容器进行访问。Docker将检查一个卷,如果没有,则创建一个卷。

相关文章

【Docker】linux、nginx、容器镜像三者基本概念
【Docker】在Windows下使用Docker Desktop创建nginx容器并访问默认网站
【Docker】在Windows操作系统上安装Docker
【Docker】了解Docker Desktop桌面应用程序,TA是如何管理和运行Docker容器(1)
【Docker】使用VS创建、运行、打包、部署.net core 6.0 webapi

Docker学习文档中心:https://docs.docker.com/

总结:温故而知新,不同阶段重温知识点,会有不一样的认识和理解,博主将巩固一遍知识点,并以实践方式和大家分享,若能有所帮助和收获,这将是博主最大的创作动力和荣幸。也期待认识更多优秀新老博主。

猜你喜欢

转载自blog.csdn.net/lmy_520/article/details/136054642