mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-02-13 00:11:22 +08:00
translated
This commit is contained in:
parent
f8b5f5311f
commit
0aa039996e
@ -1,103 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (How to build Fedora container images)
|
||||
[#]: via: (https://fedoramagazine.org/how-to-build-fedora-container-images/)
|
||||
[#]: author: (Clément Verna https://fedoramagazine.org/author/cverna/)
|
||||
|
||||
How to build Fedora container images
|
||||
======
|
||||
|
||||
![][1]
|
||||
|
||||
With the rise of containers and container technology, all major Linux distributions nowadays provide a container base image. This article presents how the Fedora project builds its base image. It also shows you how to use it to create a layered image.
|
||||
|
||||
### Base and layered images
|
||||
|
||||
Before we look at how the Fedora container base image is built, let’s define a base image and a layered image. A simple way to define a base image is an image that has no parent layer. But what does that concretely mean? It means a base image usually contains only the root file system (_rootfs_) of an operating system. The base image generally provides the tools needed to install software in order to create layered images.
|
||||
|
||||
A layered image adds a collections of layers on top of the base image in order to install, configure, and run an application. Layered images reference base images in a _Dockerfile_ using the _FROM_ instruction:
|
||||
|
||||
```
|
||||
FROM fedora:latest
|
||||
```
|
||||
|
||||
### How to build a base image
|
||||
|
||||
Fedora has a full suite of tools available to build container images. [This includes][2] _[podman][2]_, which does not require running as the root user.
|
||||
|
||||
#### Building a rootfs
|
||||
|
||||
A base image comprises mainly a [tarball][3]. This tarball contains a rootfs. There are different ways to build this rootfs. The Fedora project uses the [kickstart][4] installation method coupled with [imagefactory][5] software to create these tarballs.
|
||||
|
||||
The kickstart file used during the creation of the Fedora base image is available in Fedora’s build system [Koji][6]. The _[Fedora-Container-Base][7]_ package regroups all the base image builds. If you select a build, it gives you access to all the related artifacts, including the kickstart files. Looking at an [example][8], the _%packages_ section at the end of the file defines all the packages to install. This is how you make software available in the base image.
|
||||
|
||||
#### Using a rootfs to build a base image
|
||||
|
||||
Building a base image is easy, once a rootfs is available. It requires only a Dockerfile with the following instructions:
|
||||
|
||||
```
|
||||
FROM scratch
|
||||
ADD layer.tar /
|
||||
CMD ["/bin/bash"]
|
||||
```
|
||||
|
||||
The important part here is the _FROM scratch_ instruction, which is creating an empty image. The following instructions then add the rootfs to the image, and set the default command to be executed when the image is run.
|
||||
|
||||
Let’s build a base image using a Fedora rootfs built in Koji:
|
||||
|
||||
```
|
||||
$ curl -o fedora-rootfs.tar.xz https://kojipkgs.fedoraproject.org/packages/Fedora-Container-Base/Rawhide/20190902.n.0/images/Fedora-Container-Base-Rawhide-20190902.n.0.x86_64.tar.xz
|
||||
$ tar -xJvf fedora-rootfs.tar.xz 51c14619f9dfd8bf109ab021b3113ac598aec88870219ff457ba07bc29f5e6a2/layer.tar
|
||||
$ mv 51c14619f9dfd8bf109ab021b3113ac598aec88870219ff457ba07bc29f5e6a2/layer.tar layer.tar
|
||||
$ printf "FROM scratch\nADD layer.tar /\nCMD [\"/bin/bash\"]" > Dockerfile
|
||||
$ podman build -t my-fedora .
|
||||
$ podman run -it --rm my-fedora cat /etc/os-release
|
||||
```
|
||||
|
||||
The _layer.tar_ file which contains the rootfs needs to be extracted from the downloaded archive. This is only needed because Fedora generates images that are ready to be consumed by a container run-time.
|
||||
|
||||
So using Fedora’s generated image, it’s even easier to get a base image. Let’s see how that works:
|
||||
|
||||
```
|
||||
$ curl -O https://kojipkgs.fedoraproject.org/packages/Fedora-Container-Base/Rawhide/20190902.n.0/images/Fedora-Container-Base-Rawhide-20190902.n.0.x86_64.tar.xz
|
||||
$ podman load --input Fedora-Container-Base-Rawhide-20190902.n.0.x86_64.tar.xz
|
||||
$ podman run -it --rm localhost/fedora-container-base-rawhide-20190902.n.0.x86_64:latest cat /etc/os-release
|
||||
```
|
||||
|
||||
### Building a layered image
|
||||
|
||||
To build a layered image that uses the Fedora base image, you only need to specify _fedora_ in the _FROM_ line instruction:
|
||||
|
||||
```
|
||||
FROM fedora:latest
|
||||
```
|
||||
|
||||
The _latest_ tag references the latest active Fedora release (Fedora 30 at the time of writing). But it is possible to get other versions using the image tag. For example, _FROM fedora:31_ will use the Fedora 31 base image.
|
||||
|
||||
Fedora supports building and releasing software as containers. This means you can maintain a Dockerfile to make your software available to others. For more information about becoming a container image maintainer in Fedora, check out the [Fedora Containers Guidelines][9].
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://fedoramagazine.org/how-to-build-fedora-container-images/
|
||||
|
||||
作者:[Clément Verna][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://fedoramagazine.org/author/cverna/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://fedoramagazine.org/wp-content/uploads/2019/08/fedoracontainers-816x345.jpg
|
||||
[2]: https://fedoramagazine.org/running-containers-with-podman/
|
||||
[3]: https://en.wikipedia.org/wiki/Tar_(computing)
|
||||
[4]: https://en.wikipedia.org/wiki/Kickstart_(Linux)
|
||||
[5]: http://imgfac.org/
|
||||
[6]: https://koji.fedoraproject.org/koji/
|
||||
[7]: https://koji.fedoraproject.org/koji/packageinfo?packageID=26387
|
||||
[8]: https://kojipkgs.fedoraproject.org//packages/Fedora-Container-Base/30/20190902.0/images/koji-f30-build-37420478-base.ks
|
||||
[9]: https://docs.fedoraproject.org/en-US/containers/guidelines/guidelines/
|
103
translated/tech/20190904 How to build Fedora container images.md
Normal file
103
translated/tech/20190904 How to build Fedora container images.md
Normal file
@ -0,0 +1,103 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (How to build Fedora container images)
|
||||
[#]: via: (https://fedoramagazine.org/how-to-build-fedora-container-images/)
|
||||
[#]: author: (Clément Verna https://fedoramagazine.org/author/cverna/)
|
||||
|
||||
如何构建 Fedora 容器镜像
|
||||
======
|
||||
|
||||
![][1]
|
||||
|
||||
随着容器和容器技术的兴起,现在所有主流的 Linux 发行版都提供了容器基础镜像。本文介绍 Fedora 项目如何构建其基本镜像。同时还展示了如何使用它来创建分层图像。
|
||||
|
||||
### 基础和分层镜像
|
||||
|
||||
在看如何构建 Fedora 容器基础镜像之前,让我们定义基础镜像和分层镜像。定义基础镜像的简单方法是没有父镜像的镜像。但这具体意味着什么呢?这意味着基础镜像通常只包含操作系统的根文件系统基础镜像(_rootfs_)。基础镜像通常提供安装软件以创建分层镜像所需的工具。
|
||||
|
||||
分层镜像在基础镜像上添加了一组层,以便安装、配置和运行应用。分层镜像在 _Dockerfile_ 中使用 _FROM_ 指令引用基础镜像:
|
||||
|
||||
```
|
||||
FROM fedora:latest
|
||||
```
|
||||
|
||||
### 如何构建基础镜像
|
||||
|
||||
Fedora 有一整套用于构建容器镜像的工具。[其中包括][2] _[podman][2]_,它不需要以 root 身份运行
|
||||
|
||||
#### 构建 rootfs
|
||||
|
||||
基础镜像主要包括 [tarball][3]。这个 tarball 包含一个 rootfs。有不同的方法来构建此 rootfs。Fedora 项目使用 [kickstart][4] 安装方式以及 [imagefactory][5] 来创建这些 tarball。
|
||||
|
||||
在创建 Fedora 基础镜像期间使用的 kickstart 文件可以在 Fedora 的构建系统 [Koji][6] 中找到。_[Fedora-Container-Base][7]_ 包重新组合所有基础镜像构建。如果选择了一个构建,那么可以访问所有相关文件,包括 kickstart 文件。查看 [示例][8],文件末尾的 _%packages_ 部分定义了要安装的所有软件包。这就是让软件在基础镜像中的方法。
|
||||
|
||||
#### 使用 rootfs 构建基础镜像
|
||||
|
||||
ootfs 完成后,构建基础镜像就很容易了。它只需要一个包含以下指令的 Dockerfile:
|
||||
|
||||
```
|
||||
FROM scratch
|
||||
ADD layer.tar /
|
||||
CMD ["/bin/bash"]
|
||||
```
|
||||
|
||||
这里的重要部分是 _FROM scratch_ 指令,它会创建一个空镜像。然后,以下指令将 rootfs 添加到镜像,并设置在运行镜像时要执行的默认命令。
|
||||
|
||||
让我们使用 Koji 内置的 Fedora rootfs 构建一个基础镜像:
|
||||
|
||||
```
|
||||
$ curl -o fedora-rootfs.tar.xz https://kojipkgs.fedoraproject.org/packages/Fedora-Container-Base/Rawhide/20190902.n.0/images/Fedora-Container-Base-Rawhide-20190902.n.0.x86_64.tar.xz
|
||||
$ tar -xJvf fedora-rootfs.tar.xz 51c14619f9dfd8bf109ab021b3113ac598aec88870219ff457ba07bc29f5e6a2/layer.tar
|
||||
$ mv 51c14619f9dfd8bf109ab021b3113ac598aec88870219ff457ba07bc29f5e6a2/layer.tar layer.tar
|
||||
$ printf "FROM scratch\nADD layer.tar /\nCMD [\"/bin/bash\"]" > Dockerfile
|
||||
$ podman build -t my-fedora .
|
||||
$ podman run -it --rm my-fedora cat /etc/os-release
|
||||
```
|
||||
|
||||
需要从下载的存档中提取包含 rootfs 的 _layer.tar_ 文件。这在 Fedora 生成的镜像已经可以被容器运行时使用才需要。
|
||||
|
||||
因此,使用 Fedora 生成的镜像,获得基础镜像会更容易。让我们看看它是如何工作的:
|
||||
|
||||
```
|
||||
$ curl -O https://kojipkgs.fedoraproject.org/packages/Fedora-Container-Base/Rawhide/20190902.n.0/images/Fedora-Container-Base-Rawhide-20190902.n.0.x86_64.tar.xz
|
||||
$ podman load --input Fedora-Container-Base-Rawhide-20190902.n.0.x86_64.tar.xz
|
||||
$ podman run -it --rm localhost/fedora-container-base-rawhide-20190902.n.0.x86_64:latest cat /etc/os-release
|
||||
```
|
||||
|
||||
### 构建分层镜像
|
||||
|
||||
要构建使用 Fedora 基础镜像的分层镜像,只需在 _FROM_ 行指令中指定 _fedora_:
|
||||
|
||||
```
|
||||
FROM fedora:latest
|
||||
```
|
||||
|
||||
_latest_ 标记引用了最新的 Fedora 版本(编写本文时是 Fedora 30)。但是可以使用 image 标签获得其他版本。例如,_FROM fedora:31_ 将使用 Fedora 31 基础镜像。
|
||||
|
||||
Fedora 支持将软件作为容器来构建并发布。这意味着你可以维护 Dockerfile 来使其他人可以使用你的软件。关于在 Fedora 中成为容器镜像维护者的更多信息,请查看 [Fedora 容器指南][9]。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://fedoramagazine.org/how-to-build-fedora-container-images/
|
||||
|
||||
作者:[Clément Verna][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://fedoramagazine.org/author/cverna/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://fedoramagazine.org/wp-content/uploads/2019/08/fedoracontainers-816x345.jpg
|
||||
[2]: https://fedoramagazine.org/running-containers-with-podman/
|
||||
[3]: https://en.wikipedia.org/wiki/Tar_(computing)
|
||||
[4]: https://en.wikipedia.org/wiki/Kickstart_(Linux)
|
||||
[5]: http://imgfac.org/
|
||||
[6]: https://koji.fedoraproject.org/koji/
|
||||
[7]: https://koji.fedoraproject.org/koji/packageinfo?packageID=26387
|
||||
[8]: https://kojipkgs.fedoraproject.org//packages/Fedora-Container-Base/30/20190902.0/images/koji-f30-build-37420478-base.ks
|
||||
[9]: https://docs.fedoraproject.org/en-US/containers/guidelines/guidelines/
|
Loading…
Reference in New Issue
Block a user