首页 > 其他分享 >构建镜像开源工具 buildah

构建镜像开源工具 buildah

时间:2022-11-26 18:05:03浏览次数:54  
标签:容器 fedora Buildah 开源 构建 镜像 buildah


构建镜像开源工具 buildah

tags: images

构建镜像开源工具 buildah_推送

文章目录

  • ​​构建镜像开源工具 buildah​​
  • ​​1. 简介​​
  • ​​2. 特点​​
  • ​​3. Buildah 和 Podman​​
  • ​​4. 安装​​
  • ​​4.1 CentOS​​
  • ​​4.2 Ubuntu​​
  • ​​4.3 RHEL7​​
  • ​​4.4 Fedora​​
  • ​​5. 命令​​
  • ​​6. 示例​​
  • ​​6.1 命令行构建一个 httpd 镜像​​
  • ​​6.2 Dockerfile 构建​​
  • ​​6.3 构建镜像脚本(代替 Dockerfile)​​

1. 简介

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

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

2. 特点

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

3. Buildah 和 Podman

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

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

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

​Buildah​​​ 和 ​​Podman​​ 各自创建的容器是互相看不到的。

4. 安装

4.1 CentOS

sudo yum -y install buildah

4.2 Ubuntu

# Ubuntu 20.10 and newer
sudo apt-get -y update
sudo apt-get -y install buildah

4.3 RHEL7

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

4.4 Fedora

sudo dnf -y install buildah

或者

$ sudo rpm-ostree install buildah

5. 命令

Command

Description

​​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'

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

将包添加到工作容器

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

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

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

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

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

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

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

配置完成后,保存镜像:

buildah commit fedora-working-container fedora-myhttpd

列出本地镜像

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

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

podman run -tid fedora-myhttpd

测试

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

要将映像推送到本地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

也可以这样执行:

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

​Skopeo​​检查结果

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

6.2 Dockerfile 构建

$ mkdir fedora-http-server && cd fedora-http-server 
$ nano Dockerfile
# Base on the most recently released Fedora
FROM fedora:latest
MAINTAINER ipbabble email [email protected] # 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"]

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

构建

buildah bud -t fedora-http-server

运行容器

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

测试访问

curl localhost:8080

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

构建镜像:

chmod 755 build_buildah_upstream.sh
./build_buildah_upstream.sh

运行容器:

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

参考:


标签:容器,fedora,Buildah,开源,构建,镜像,buildah
From: https://blog.51cto.com/ghostwritten/5889094

相关文章

  • renren-fast-vue人人开源前端项目搭建保姆级教程
    1、从gitee上clone项目gitclonehttps://gitee.com/renrenio/renren-fast-vue.git2、准备好python环境需要有Python2以上的环境,我安装了Python3.10.0的版本,可以搭建成......
  • 推荐一款 .NET 编写的 嵌入式平台的开源仿真器
    Renode是一个开发框架,通过让你模拟物理硬件系统来加速物联网和嵌入式系统开发。Renode可以模拟Cortex-M、RISC-V等微控制器,不仅可以模拟CPU指令,还可以模拟外设,甚至可......
  • docker镜像详解
    Docker镜像详解什么是镜像镜像是一种轻量级、可执行的独立软件包,用来打包软件运行环境和基于运行环境开发的软件,它包含运行某个软件所需要的所有内容,包括代码,运行时(一个......
  • docker保存镜像
    在已有镜像系统上导出镜像保存镜像dockersave-oneo4j-3.5.35.tarneo4j:3.5.35-community导入镜像文件dockerload<neo4j-3.5.35.tar dockerimages   ......
  • 好消息 突破:IM开源项目OpenIM采用wasm技术实现jssdk
    OpenIM客户端sdk用golang实现,同时采用sqlite存储本地聊天记录,通过gomobile生成sdk,供iOSAndroid调用,达到了了一套代码多端复用的效果。最近融合wasm技术,让浏览器具备存储......
  • 构建Docker镜像
    作用 根据需求构建镜像,推送到harbor私有仓库保存与管理,如有相同构件组需求,可直接下载,减少重复劳动,有利于镜像级高层次复用。一、Docker镜像构建过程1、生成Dockerfile,......
  • 白宫开发Facebook机器人代理奥巴马与民众聊天:已开源
    新浪科技讯北京时间10月15日上午消息,美国白宫今天公布了总统奥巴马​​Facebook​​Messenger聊天机器人的源代码,协助其他政府部门开发自主的聊天机器人。白宫首......
  • 阅读开源代码时:idea中如何使用todo标记、活动模板 (史上最全)
    接下来,尼恩要带大家完成一个超级牛逼的大厂offer收割机项目——100Wqps三级组件实操,实操中,用到caffeine并且,尼恩要带大家穿透式、起底式的学习caffeine的......
  • 简述五种开源NAS存储服务器--转
    存储对于公司来说是必不可少的:数据必须被存储、检索、共享和保护。下面小编为大家介绍一下5款开源的NAS存储服务器一、云计算云计算正在蓬勃发展,比任何云存储系统都要快......
  • npm install 国内镜像 安装方式 快
    #建议不要直接使用cnpm安装以来,会有各种诡异的bug。可以通过如下操作解决npm下载速度慢的问题npminstall--registry=https://registry.npm.taobao.orghttps://se......