网站首页 > 开源技术 正文
Buildah是用于通过较低级别的coreutils接口构建OCI兼容镜像的工具。与Podman相似,Buildah不依赖于Docker或CRI-O之类的守护程序,并且不需要root特权。Buildah提供了一个命令行工具,该工具可复制在Dockerfile中找到的所有命令。这样就可以从脚本语言(例如Bash)发出Buildah命令。
本教程向您展示如何:
- 使用Buildah从现有镜像开始将Web应用程序打包为容器,然后使用Podman和Docker运行您的应用程序
- 使用Buildah从头开始将Web应用程序打包为容器
先决条件
在本教程中,我们假设你对Docker或Podman有基本的了解。
- Buildah。使用以下buildah --version命令来验证是否已安装Buildah:
buildah --version
以下示例输出显示您的计算机上已安装Buildah:
buildah version 1.11.6 (image-spec 1.0.1-dev, runtime-spec 1.0.1-dev)
如果未安装Buildah,请按照Buildah安装页面上的说明进行操作。
- Podman。输入以下命令以检查系统上是否安装了Podman:
podman version
以下示例输出显示您的计算机上已安装Podman:
Version: 1.6.4
RemoteAPI Version: 1
Go Version: go1.12.12
OS/Arch: linux/amd64
有关如何安装Podman的详细信息,请参阅Podman安装说明页面。
- Docker。使用以下命令查看您的系统上是否安装了Docker:
docker --version
以下示例输出显示您的计算机上已安装Docker:
Docker version 18.06.3-ce, build d7080c1
从现有镜像开始将基于Web的应用程序打包为容器
在本部分中,您将使用Buildah从Alpine Linux镜像开始将基于Web的应用程序打包为容器。然后,您将使用Podman和Docker运行容器镜像。
Alpine Linux的大小仅为5 MB,并且缺少运行ExpressJS所需的几个先决条件。因此,您将用于apk安装这些先决条件。
- 输入以下命令以基于该alpine镜像创建新的容器镜像,并将新镜像的名称存储在名为的环境变量中container:
container=$(buildah from alpine)
Getting image source signatures
Copying blob c9b1b535fdd9 skipped: already exists
Copying config e7d92cdc71 done
Writing manifest to image destination
Storing signatures
?请注意,默认情况下,Buildah通过在名称后追加-working-container来构造容器的名称:
echo $container
alpine-working-container
您可以通过--name使用工作容器名称指定标志来覆盖默认行为。以下示例创建一个名为的容器镜像example-container:
example_container=$(buildah from --name "example-container" alpine)
echo $example_container
example-container
- 您刚刚拉出的Alpine Linux镜像的大小仅为5 MB,并且缺少Bash等基本实用程序。运行以下命令来验证新的容器镜像:
buildah run $container bash
以下输出显示容器镜像已创建,但bash尚未安装:
ERRO[0000] container_linux.go:346: starting container process caused "exec: \"bash\": executable file not found in $PATH"
container_linux.go:346: starting container process caused "exec: \"bash\": executable file not found in $PATH"
error running container: error creating container for [bash]: : exit status 1
ERRO exit status 1
- 要安装Bash,请输入buildah run命令并指定:
- 容器名称($container)
- 两个空格。之后的命令--直接传递到容器。
- 您要在容器内执行的命令(apk add bash)
buildah run $container -- apk add bash
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/community/x86_64/APKINDEX.tar.gz
(1/5) Installing ncurses-terminfo-base (6.1_p20191130-r0)
(2/5) Installing ncurses-terminfo (6.1_p20191130-r0)
(3/5) Installing ncurses-libs (6.1_p20191130-r0)
(4/5) Installing readline (8.0.1-r0)
(5/5) Installing bash (5.0.11-r1)
Executing bash-5.0.11-r1.post-install
Executing busybox-1.31.1-r9.trigger
OK: 15 MiB in 19 packages
- 与您的安装类似bash,请运行以下buildah run命令来安装node和npm:
buildah run $container -- apk add --update nodejs nodejs-npm
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/community/x86_64/APKINDEX.tar.gz
(1/8) Installing ca-certificates (20191127-r1)
(2/8) Installing c-ares (1.15.0-r0)
(3/8) Installing libgcc (9.2.0-r3)
(4/8) Installing nghttp2-libs (1.40.0-r0)
(5/8) Installing libstdc++ (9.2.0-r3)
(6/8) Installing libuv (1.34.0-r0)
(7/8) Installing nodejs (12.15.0-r1)
(8/8) Installing npm (12.15.0-r1)
Executing busybox-1.31.1-r9.trigger
Executing ca-certificates-20191127-r1.trigger
OK: 73 MiB in 27 packages
- 您可以使用该buildah config命令来设置镜像配置值。以下命令将工作目录设置为/usr/src/app/:
buildah config --workingdir /usr/src/app/ $container
- 要初始化新的JavaScript项目,请npm init -y在容器中运行以下命令:
buildah run $container -- npm init -y
Wrote to /package.json:
{
"name": "",
"version": "1.0.0",
"description": "",
"main": "index.js",
"directories": {
"lib": "lib"
},
"dependencies": {},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
- 发出以下命令来安装Express.JS:
buildah run $container -- npm install express --save
npm WARN @1.0.0 No description
npm WARN @1.0.0 No repository field.
+ express@4.17.1
added 1 package from 8 contributors and audited 126 packages in 1.553s
found 0 vulnerabilities
- 创建一个名为的文件HelloWorld.js并使用以下JavaScript源代码进行复制:
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
- 要将HelloWorld.js文件复制到容器的工作目录,请输入以下buildah copy命令:
- 容器名称($container)
- 您要复制的文件名(HelloWorld.js)
buildah copy $container HelloWorld.js
c26df5d060c589bda460c34d40c3e8f47f1e401cdf41b379247d23eca24b1c1d
?您可以通过传递目标目录的名称作为参数来将文件复制到其他容器。以下示例命令将复制HelloWorld.js到/temp目录:
buildah copy $container HelloWorld.js /temp
- 要设置容器的入口点,请输入buildah config带有--entrypoint参数的命令:
buildah config --entrypoint "node HelloWorld.js" $container
- 至此,您已经准备好使用buildah commit命令编写新镜像。它带有两个参数:
- 容器的名称($container)
- 新容器的名称(buildah-hello-world)
buildah commit $container buildah-hello-world
Getting image source signatures
Copying blob 5216338b40a7 skipped: already exists
Copying blob 821cca548ffe done
Copying config 0d9f23545e done
Writing manifest to image destination
Storing signatures
0d9f23545ed69ace9be47ed081c98b4ae182801b7fe5b7ef00a49168d65cf4e5
?如果提供的镜像名称不是以镜像仓库名称开头,则Buildah默认添加localhost到镜像名称。
- 以下命令列出了您的Buildah镜像:
buildah images
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost/buildah-hello-world latest 0d9f23545ed6 56 seconds ago 71.3 MB
使用Podman运行Buildah镜像
- 要使用Podman运行镜像,首先必须确保镜像在Podman中可见:
podman images
以下示例输出显示了在先前步骤中创建的容器镜像:
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost/buildah-hello-world latest 0d9f23545ed6 About a minute ago 71.3 MB
- buildah-hello-world通过输入podman run带有以下参数的命令来运行镜像:
- dt 指定该容器应在后台运行,并且Podman应该为其分配一个伪TTY。
- -p主机(3000)上的端口将转发到容器端口(3000),以分隔:。
- 镜像的名称(buildah-hello-world)
podman run -dt -p 3000:3000 buildah-hello-world
332d060fc0009a8088349aba672be3601b76553e5df7643d4788c917528cbd8e
- 使用podman ps命令查看正在运行的容器的列表:
podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
332d060fc000 localhost/buildah-hello-world:latest /bin/sh 23 seconds ago Up 21 seconds ago 0.0.0.0:3000->3000/tcp cool_ritchie
- 要查看正在运行的应用程序,请将浏览器指向 http://localhost:3000 。该应用程序的外观应如以下屏幕截图所示:
现在已经验证了应用程序的功能,您可以停止正在运行的容器:
podman kill 332d060fc000
332d060fc000
使用Docker运行Buildah镜像
您在上一节中构建的容器镜像与Docker兼容。在本节中,我们将引导您完成buildah-hello-world使用Docker运行镜像所需的步骤。
- 首先,您必须将镜像推送到Docker。输入buildah push指定的命令:
- 容器名称
- 使用以下格式 <TRANSPORT>:<DETAILS>。
以下示例命令使用docker-daemon传输将buildah-hello-world镜像推送到Docker:
buildah push buildah-hello-world docker-daemon:buildah-hello-world:latest
Getting image source signatures
Copying blob 5216338b40a7 done
Copying blob 821cca548ffe done
Copying config 0d9f23545e done
Writing manifest to image destination
Storing signatures
- 列出存储在本地计算机上的Docker镜像:
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
buildah-hello-world latest 0d9f23545ed6 16 minutes ago 64.5MB
- buildah-hello-world使用Docker运行容器镜像:
docker run -dt -p 3000:3000 buildah-hello-world
b0f29ff964cd84bf204b3f30f615581c4bb67c4a880aa871ce9c89db48e68720
- 几秒钟后,输入docker ps镜像以显示正在运行的容器的列表:
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b0f29ff964cd buildah-hello-world "/bin/sh -c 'node He…" 16 seconds ago Up 13 seconds 0.0.0.0:3000->3000/tcp goofy_chandrasekhar
- 要查看正在运行的应用程序,请将浏览器指向 http://localhost:3000 。该应用程序的外观应如以下屏幕截图所示:
- 使用以下命令停止正在运行的容器:
docker kill b0f29ff964cd
b0f29ff964cd
猜你喜欢
- 2024-10-27 越狱工具checkra1n更新 iOS 13.4.1系统也可以越狱了
- 2024-10-27 数据库多实例(数据库多实例数据泵写法)
- 2024-10-27 企业dns流量实时监控(企业dns流量实时监控怎么设置)
- 2024-10-27 Linux学习笔记(九)软件包管理(linux软件包管理的意义)
- 2024-10-27 如何打造自己的资产管理系统(如何打造自己的资产管理系统呢)
- 2024-10-27 BFP&XDP指南-工具链(bp工具箱)
- 2024-10-27 从零开始学习OpenWrt:刷机 + 使用 + 编译教程
- 2024-10-27 Pyenv 介绍和安装指南 - Ubuntu 24
- 2024-07-20 Linux Shell 练习小例子(linux的shell编程题目)
- 2024-07-20 通过Python扫描代码关键字并进行预警
你 发表评论:
欢迎- 最近发表
-
- 使用Python实现图片文件的加密与解密:保障隐私安全的实用方案
- 吴恩达官宣开源,yyds!(吴恩达rnn)
- Python自动化办公实战:效率提升10倍的脚本开发指南
- Android让视图显示未读消息数量(未读消息图标怎么显示)
- docker容器安装与部署,常用命令、容器卷、dockerfile,详细教程
- pytorch的一个最简单的cpp扩展(pytorch cdist)
- 如何编译.Net 6 Runtime源码(编译framework)
- 微软Windows 11将改版BSOD、强制登录账号才能安装
- windows环境下配置sphinx输出html文档
- 超详细windows安装配置WSL2(ubuntu20.04)步骤
- 标签列表
-
- jdk (81)
- putty (66)
- rufus (78)
- 内网穿透 (89)
- okhttp (70)
- powertoys (74)
- windowsterminal (81)
- netcat (65)
- ghostscript (65)
- veracrypt (65)
- asp.netcore (70)
- wrk (67)
- aspose.words (80)
- itk (80)
- ajaxfileupload.js (66)
- sqlhelper (67)
- express.js (67)
- phpmailer (67)
- xjar (70)
- redisclient (78)
- wakeonlan (66)
- tinygo (85)
- startbbs (72)
- webftp (82)
- vsvim (79)
本文暂时没有评论,来添加一个吧(●'◡'●)