您的位置:首页 > 财经 > 产业 > 制作一个app软件需要多少时间_编程培训机构排名_网店推广平台_平台推广是什么

制作一个app软件需要多少时间_编程培训机构排名_网店推广平台_平台推广是什么

2025/8/10 21:25:53 来源:https://blog.csdn.net/LONG_Yi_1994/article/details/147364116  浏览:    关键词:制作一个app软件需要多少时间_编程培训机构排名_网店推广平台_平台推广是什么
制作一个app软件需要多少时间_编程培训机构排名_网店推广平台_平台推广是什么

本文详细讲述完全从零开始手动创建一个可以被 docker load 加载的 Docker 镜像。我们将不使用 Docker 工具,直接构造符合 Docker 镜像格式(OCI 镜像规范)的 tar 文件。最终生成的镜像可以通过 docker load 导入到 Docker 中运行。


从零创建 Docker 镜像

Docker 镜像是一个 tar 包,包含文件系统层、配置文件和元数据。我们将创建一个简单的镜像,包含一个 shell 脚本,运行时输出 “Hello from my custom image!”。

步骤 1:准备文件系统层

创建一个简单的文件系统层,包含一个可执行的 shell 脚本。

  1. 创建工作目录和文件系统结构:
mkdir -p my-image/rootfs/bin
cd my-image
  1. 创建一个 shell 脚本:
echo -e '#!/bin/sh\necho "Hello from my custom image!"' > rootfs/bin/hello.sh
chmod +x rootfs/bin/hello.sh
  1. 打包文件系统层为 tar 文件:
tar -cvf layer.tar -C rootfs .

这会生成 layer.tar,包含文件系统内容(bin/hello.sh)。

步骤 2:创建镜像配置文件

需要一个 JSON 文件(config.json)来描述镜像的运行时配置。

my-image 目录下创建 config.json

{"architecture": "amd64","os": "linux","config": {"Entrypoint": ["/bin/hello.sh"],"Env": ["PATH=/bin"],"WorkingDir": "/"},"rootfs": {"type": "layers","diff_ids": []},"history": [{"created": "2025-04-19T00:00:00Z","created_by": "Manual creation"}]
}

计算层 diff_id

diff_ids 是文件系统层的 SHA256 校验值,格式为 sha256:<hash>。我们需要计算 layer.tar 的 SHA256 值。

运行以下命令:

sha256sum layer.tar

假设输出为:

e4d7f1b4...  layer.tar

sha256:e4d7f1b4... 添加到 config.jsonrootfs.diff_ids 中,更新 config.json

{"architecture": "amd64","os": "linux","config": {"Entrypoint": ["/bin/hello.sh"],"Env": ["PATH=/bin"],"WorkingDir": "/"},"rootfs": {"type": "layers","diff_ids": ["sha256:e4d7f1b4..."]},"history": [{"created": "2025-04-19T00:00:00Z","created_by": "Manual creation"}]
}

步骤 3:创建 Manifest 文件

manifest.json 描述镜像的层和配置信息。

my-image 目录下创建 manifest.json

[{"Config": "config.json","RepoTags": ["my-custom-image:latest"],"Layers": ["layer.tar"]}
]

步骤 4:打包镜像

将所有文件打包成一个 tar 文件,符合 Docker 镜像格式。

my-image 目录下运行:

tar -cvf my-image.tar manifest.json config.json layer.tar

这会生成 my-image.tar,即最终的 Docker 镜像文件。

步骤 5:验证和加载镜像

my-image.tar 传输到有 Docker 的环境中,运行:

docker load -i my-image.tar

加载后,检查镜像:

docker images

应该能看到 my-custom-image:latest

运行镜像:

docker run my-custom-image:latest

预期输出:

Hello from my custom image!

注意事项

  1. SHA256 计算:确保 layer.tar 的 SHA256 值正确,否则 Docker 会报错。
  2. 文件权限:确保 hello.sh 有可执行权限(chmod +x)。
  3. 架构兼容性architecture 字段需与目标运行环境匹配(这里使用 amd64)。
  4. 时间格式history.created 使用 ISO 8601 格式(YYYY-MM-DDTHH:MM:SSZ)。
  5. 依赖问题:此例中,hello.sh 依赖 /bin/sh。如果目标环境没有 /bin/sh,需要将 sh 二进制文件包含在 rootfs 中。

扩展:添加更多层

如果需要多个层,重复以下步骤:

  1. 创建新的文件系统目录,添加或修改文件。
  2. 打包成新的 layerN.tar
  3. 计算新层的 SHA256 值,添加到 config.jsondiff_ids
  4. manifest.jsonLayers 列表中添加新层。

例如,添加第二个层:

mkdir rootfs2
echo "Another file" > rootfs2/another.txt
tar -cvf layer2.tar -C rootfs2 .
sha256sum layer2.tar  # 假设得到 sha256:abcdef...

更新 config.json

"rootfs": {"type": "layers","diff_ids": ["sha256:e4d7f1b4...", "sha256:abcdef..."]
}

更新 manifest.json

"Layers": ["layer.tar", "layer2.tar"]

重新打包:

tar -cvf my-image.tar manifest.json config.json layer.tar layer2.tar

总结

通过以上步骤,你可以完全不依赖 Docker 工具,从零创建一个符合 OCI 规范的 Docker 镜像。最终的 my-image.tar 可以通过 docker load 导入并运行。此方法适用于需要深度定制镜像或在无 Docker 环境中构建镜像的场景。


版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com