• 构建镜像开源工具 buildah


    构建镜像开源工具 buildah

    tags: images
    在这里插入图片描述

    1. 简介

    Buildah 是一种基于 Linux 的开源工具,用于构建与开放容器倡议 (OCI) 兼容的容器,这意味着容器也与DockerKubernetes兼容。借助 Buildah,您可以使用自己喜欢的工具从现有基础镜像或使用空镜像从头开始创建高效的容器镜像。这是一种更灵活、更安全的构建容器镜像的方式。

    BuildahDaniel Walsh 和他在 Red Hat 的团队于 2017 年创建。他们着手创建容器镜像的“coreutils”——一种可以与现有容器主机工具一起使用来构建 OCI 和 Docker 兼容容器镜像的工具。然后,这些镜像可以存储在容器仓库中,并在多个运行时环境中使用。

    2. 特点

    • 使用或不使用 Dockerfiles(包含用户可以调用以组装镜像的所有命令的文本文档)构建容器镜像
    • 从头开始或从现有容器镜像起点创建容器镜像;
    • 不在镜像本身中包含构建工具,减少构建镜像的大小,提高安全性,并允许使用更少的资源更容易地传输 ;
    • Dockerfiles 兼容,允许从 Docker 轻松转换;
    • 创建特定于用户的镜像,以便镜像可以按创建它们的用户进行排序;
    • 检查、验证和修改镜像;
    • 将容器和镜像从本地存储推送到公共或私有仓库或存储库;
    • Docker Hub 推送或拉取镜像;
    • 移除本地存储的容器镜像;
    • 挂载和卸载工作容器的根文件系统;
    • 使用容器根文件系统的更新内容作为新镜像的文件系统层。

    3. Buildah 和 Podman

    BuildahPodman都是互补的开源项目和命令行工具,使用并构建 OCI 镜像和容器。首先创建了 Buildah,Podman 使用与 Buildah 相同的代码进行构建。但是,Buildah 的命令比 Podman 的命令详细得多,允许对镜像进行更细粒度的控制并允许创建更精细的镜像层。Podman 的“构建”命令使用了 Buildah 功能的一个子集。

    Buildah 专注于构建容器镜像,复制在没有守护程序套接字组件的 Dockerfile 中找到的所有命令,而 Podman 专注于维护和修改容器中的这些镜像所需的东西。使用 Podman,您可以创建一个容器——使用 Buildah 提供容器镜像——然后使用熟悉的命令行界面 (CLI) 命令(如果您可以运行一个Docker CLI 中的命令,您可以在 Podman CLI 中运行相同的命令)。

    Podman 和 Buildah 的另一个不同之处是:Buildah 的容器主要是临时创建的,以允许将内容传输到正在创建的容器镜像中,而使用 Podman,用户创建传统容器,旨在使用和维护更长时间. Buildah 的容器用于短期目的,而 Podman 的容器用于长期目的。

    BuildahPodman 各自创建的容器是互相看不到的。

    4. 安装

    4.1 CentOS

    sudo yum -y install buildah
    
    • 1

    4.2 Ubuntu

    # Ubuntu 20.10 and newer
    sudo apt-get -y update
    sudo apt-get -y install buildah
    
    • 1
    • 2
    • 3

    4.3 RHEL7

    sudo subscription-manager repos --enable=rhel-7-server-extras-rpms
    sudo yum -y install buildah
    
    • 1
    • 2

    4.4 Fedora

    sudo dnf -y install buildah
    
    • 1

    或者

    $ sudo rpm-ostree install buildah
    
    • 1

    5. 命令

    CommandDescription
    buildah-add(1)将文件、URL 或目录的内容添加到容器中。
    buildah-build(1)使用 Containerfiles 或 Dockerfiles 中的指令构建镜像。
    buildah-commit(1)从运行的容器创建镜像。
    buildah-config(1)更新镜像配置设置。
    buildah-containers(1)列出工作容器及其基础镜像。
    buildah-copy(1)将文件、URL 或目录的内容复制到容器的工作目录中。
    buildah-from(1)从头开始或使用指定镜像作为起点创建一个新的工作容器。
    buildah-images(1)列出本地存储中的镜像。
    buildah-info(1)显示 Buildah 系统信息。
    buildah-inspect(1)检查容器或镜像的配置。
    buildah-mount(1)挂载工作容器的根文件系统。
    buildah-pull(1)从指定位置拉取镜像。
    buildah-push(1)将镜像从本地存储推送到其他地方。
    buildah-rename(1)重命名本地容器
    buildah-rm(1)删除一个或多个工作容器。
    buildah-rmi(1)删除一个或多个镜像.
    buildah-run(1)在容器内运行命令。
    buildah-tag(1)为本地镜像添加一个额外的名称。
    buildah-umount(1)卸载工作容器的根文件系统。
    buildah-unshare(1)在具有修改后的 ID 映射的用户命名空间中启动命令。
    buildah-version(1)显示 Buildah 版本信息

    6. 示例

    配置别名

    $ vim /root/.bashrc
    alias p='podman'
    alias b='buildah'
    alias s='skopeo'
    
    • 1
    • 2
    • 3
    • 4

    6.1 命令行构建一个 httpd 镜像

    第一步是提取基本映像并创建工作容器

    $ buildah from fedora
    fedora-working-container
    
    $ b ps
    CONTAINER ID  BUILDER  IMAGE ID     IMAGE NAME                       CONTAINER NAME
    89704476b76a     *     885d2b38b819 registry.fedoraproject.org/fe... fedora-working-container
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    将包添加到工作容器

    buildah run fedora-working-container dnf install httpd -y
    
    • 1

    为Web服务器创建包含某些内容的工作目录:

    mkdir demo-httpd && cd demo-httpd && echo 'sample container' > index.html
    
    • 1

    将本地文件复制到工作容器

    buildah copy fedora-working-container index.html /var/www/html/index.html
    
    • 1

    定义容器入口点以启动应用程序

    buildah config --entrypoint "/usr/sbin/httpd -DFOREGROUND" fedora-working-container
    
    • 1

    配置完成后,保存镜像:

    buildah commit fedora-working-container fedora-myhttpd
    
    • 1

    列出本地镜像

    $ buildah images
    REPOSITORY                          TAG      IMAGE ID       CREATED          SIZE
    localhost/fedora-myhttpd            latest   e1fb00a4662b   43 seconds ago   434 MB
    
    • 1
    • 2
    • 3

    现在可以使用podman在本地利用新生成的镜像运行容器:

    podman run -tid fedora-myhttpd
    
    • 1

    测试

    $ p exec -ti heuristic_solomon curl http://localhost
    sample container
    
    • 1
    • 2

    要将映像推送到本地Docker仓库,请执行以下操作:

    #登陆仓库
    $ buildah login -u registryuser -p registryuserpassword 192.168.10.80:5000
    Login Succeeded!
    #推送
    $ buildah push  fedora-myhttpd docker://192.168.10.80:5000/testuser/fedora-myhttpd:latest
    Getting image source signatures
    Copying blob d4222651a196 done
    Copying blob cc6656265656 done
    Copying config e1fb00a466 done
    Writing manifest to image destination
    Storing signatures
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    也可以这样执行:

    buildah push --creds registryuser:registryuserpassword fedora-myhttpd docker://192.168.10.80:5000/testuser/fedora-myhttpd:latest
    
    • 1

    Skopeo检查结果

    $ skopeo inspect docker://192.168.10.80:5000/testuser/fedora-myhttpd:latest
    
    • 1

    6.2 Dockerfile 构建

    $ mkdir fedora-http-server && cd fedora-http-server 
    $ nano Dockerfile
    
    • 1
    • 2
    # Base on the most recently released Fedora
    FROM fedora:latest
    MAINTAINER ipbabble email buildahboy@redhat.com # not a real email
    
    # Install updates and httpd
    RUN echo "Updating all fedora packages"; dnf -y update; dnf -y clean all
    RUN echo "Installing httpd"; dnf -y install httpd && dnf -y clean all
    
    # Expose the default httpd port 80
    EXPOSE 80
    
    # Run the httpd
    CMD ["/usr/sbin/httpd", "-DFOREGROUND"]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    CTRL+X退出,按Y保存,按Enter退出nano

    构建

    buildah bud -t fedora-http-server
    
    • 1

    运行容器

    podman run -p 8080:80  -tid fedora-http-server
    podman ps
    
    • 1
    • 2

    测试访问

    curl localhost:8080
    
    • 1

    6.3 构建镜像脚本(代替 Dockerfile)

    • build_buildah_upstream.sh
    #!/usr/bin/env bash
    # build_buildah_upstream.sh 
    #
    ctr=$(buildah from fedora)
    buildah config --env GOPATH=/root/buildah $ctr
    buildah run $ctr /bin/sh -c 'dnf -y install --enablerepo=updates-testing \
         make \
         golang \
         bats \
         btrfs-progs-devel \
         device-mapper-devel \
         glib2-devel \
         gpgme-devel \
         libassuan-devel \
         libseccomp-devel \
         git \
         bzip2 \
         go-md2man \
         runc \
         fuse-overlayfs \
         fuse3 \
         containers-common; \
         mkdir -p /root/buildah; \
         git clone https://github.com/containers/buildah /root/buildah/src/github.com/containers/buildah; \
         cd /root/buildah/src/github.com/containers/buildah; \
         make; \
         make install; \
         rm -rf /root/buildah/*; \
         dnf -y remove bats git golang go-md2man make; \
         dnf clean all' 
    
    buildah run $ctr -- sed -i -e 's|^#mount_program|mount_program|g' -e '/additionalimage.*/a "/var/lib/shared",' /etc/containers/storage.conf
    
    buildah run $ctr /bin/sh -c 'mkdir -p /var/lib/shared/overlay-images /var/lib/shared/overlay-layers; touch /var/lib/shared/overlay-images/images.lock; touch /var/lib/shared/overlay-layers/layers.lock' 
    
    buildah config --env _BUILDAH_STARTED_IN_USERNS="" --env BUILDAH_ISOLATION=chroot $ctr
    buildah commit $ctr buildahupstream 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    构建镜像:

    chmod 755 build_buildah_upstream.sh
    ./build_buildah_upstream.sh
    
    • 1
    • 2

    运行容器:

    $ podman run buildahupstream buildah version
    $ podman run buildahupstream bash -c "buildah from busybox; buildah images"
    
    • 1
    • 2

    参考:

  • 相关阅读:
    JavaScript 68 JavaScript Browser BOM 68.1 JavaScript Window - 浏览器对象模型
    菜鸟教程《Python 3 教程》笔记(17):输入和输出
    让ubuntu bash像git bash一样
    SpringBoot核心功能与基础配置
    redis高级面试
    vulnhub之school 1
    史上最全,最详细SQL基础
    Linux学习13—网站服务
    基于Python实现的业务数据分析系统
    DependencyProperty事件
  • 原文地址:https://blog.csdn.net/xixihahalelehehe/article/details/128024372