在docker镜像中添加目录和镜像

  1. 第一步是从 docker hub 拉取最新的 CentOS 镜像。
# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
d9aaf4d82f24: Pull complete 
Digest: sha256:4565fe2dd7f4770e825d4bd9c761a81b26e49cc9e3c9631c58cfc3188be9505a
Status: Downloaded newer image for centos:latest
# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              d123f4e55e12        2 weeks ago         197MB
  1. 下载 CentOS 镜像后,我们将基于此镜像运行名为“centos_test”的 docker 容器。
# docker run -it --name="centos_test" centos:latest /bin/bash
[root@e121d03b20dc /]#
  1. 现在让我们在容器“test_dir”中创建一个新目录,其中包含一个名为“test_file”的文件。
    还要在 test_file 中添加一些随机文本。
[root@e121d03b20dc /]# mkdir test_dir
[root@e121d03b20dc /]# cd test_dir
[root@e121d03b20dc test_dir]# echo "This is a sample text" > test_file         
[root@e121d03b20dc test_dir]# cat test_file
This is a sample text
[root@e121d03b20dc test_dir]# ls -lrt
total 4
-rw-r--r--. 1 root root 22 Nov 19 16:12 test_file
  1. 下一步是使用新创建的 docker 容器通过 docker commit 命令构建新镜像。
    'docker commit' 命令从 docker 主机运行,而不是从 docker 容器本身运行。
# docker commit -m="This a test image" centos_test new_centos_image
sha256:93603e53ff5329b314da097e3e5607b60cd1ce126f48cae542c083c715f069f7

这里,
-m="This a test image" : 是一个提交消息。

centos_test :我们从中创建镜像的容器的名称。

new_centos_image :创建的新镜像的名称。

  1. 运行上述命令后,我们将在系统本地可用的 docker 镜像列表中看到新镜像“centos_image”。
# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
new_centos_image    latest              93603e53ff53        52 seconds ago      197MB
centos              latest              d123f4e55e12        2 weeks ago         197MB
如何在 Docker 镜像中更新/添加文件

这篇文章讨论了如何根据需要更改从 Docker 中心的公共存储库中提取的标准 Docker 镜像。
我们将拉取最新的 CentOS docker 镜像,并添加一个测试目录“test_dir”并其中创建一个测试文件“test_fiel”。

查看更多教程 https://on  itroad.com

测试新的 docker 镜像

我们现在将通过在其上运行一个新容器来测试新创建的镜像。
我们应该能够列出在新容器中创建的测试目录和测试文件。

  1. 从新建的镜像创建一个新的容器。
# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
new_centos_image    latest              93603e53ff53        11 minutes ago      197MB
centos              latest              d123f4e55e12        2 weeks ago         197MB
# docker run -it --name="image_testing" new_centos_image:latest /bin/bash
[root@24bd49cd0e0e /]#
  1. 检查我们之前在镜像中创建的测试目录和测试文件。
[root@24bd49cd0e0e /]# ls -lrt test_dir
total 4
-rw-r--r--. 1 root root 22 Nov 19 17:09 test_file
[root@24bd49cd0e0e /]# cd test_dir
[root@24bd49cd0e0e test_dir]# cat test_file
This is a sample text
日期:2020-09-17 00:16:28 来源:oir作者:oir