使用 ARM 主机的 qemu 在 Alpine/Docker 上安装 bash

Install bash on Alpine/Docker using qemu for ARM host

我正在尝试在构建时在 x86 主机上创建一个 docker 容器 ,用于 ARM 主机上的运行时。为此,您需要使用 qemu 进行交叉编译。我也想用 alpine linux 因为图片太小了。

但是我遇到了一个只在构建时发生的异常错误 - 一个问题 安装 bash。

我的理解是运行apk -U add bash,apk更新包列表的时候 从存储库中,然后安装请求的包的最新版本。 然后运行 ​​post-install 脚本。这些 post 安装脚本似乎失败了。 但是,当我在没有 bash 的情况下构建图像然后 运行 以交互方式构建容器时 在 ARM 主机上,apk fix && apk -U add bash 成功了。这样做 然而,构建时的命令失败了。

如何在构建时添加 bash?

Dockerfile

FROM armhf/alpine:3.5

ENV CONSUL_PREFIX __CONSUL_PREFIX__
ENV CONSUL_SECRET_PREFIX __CONSUL_SECRET_PREFIX__

ENV QEMU_EXECVE 1

COPY deploy/qemu/qemu-arm-static            /usr/bin/

RUN ["qemu-arm-static","/sbin/apk","fix"]
RUN ["qemu-arm-static","/sbin/apk","add","-U","bash"]

RUN ["qemu-arm-static","/sbin/apk","-U","add", \
                       "postgresql-client",\
                       "curl","vim",\
                       "tzdata","bc"]

RUN ["qemu-arm-static","/bin/cp","usr/share/zoneinfo/America/Los_Angeles","/etc/localtime"]
RUN ["qemu-arm-static","/bin/echo","America/Los_Angeles",">","/etc/timezone"]
RUN ["qemu-arm-static","/bin/rm","-rf","/var/cache/apk/*"]
RUN ["qemu-arm-static","/bin/sh"]

COPY deploy                         /usr/local/deploy
COPY deploy/default/bashrc          /root/.bashrc
COPY deploy/default/vimrc           /root/.vimrc

COPY src                            /src

生成日志/错误

@C02NN3NBG3QT:dev-resources $ ./publish-image
+ : router-logs
+ : quay.io
+ : quay.io/skilbjo/router-logs
+ : skilbjo@github.com
++ echo router-logs
++ tr - _
+ : router_logs/config
++ echo router-logs
++ tr - _
+ : router_logs/secrets
+ cat ../deploy/default/Dockerfile
+ sed 's;__CONSUL_PREFIX__;router_logs/config;'
+ sed 's;__CONSUL_SECRET_PREFIX__;router_logs/secrets;'
+ IMAGE_TAG=dev
+ cd ..
++ git rev-parse HEAD
+ echo 0a865e3918d584b4377fad9afe9ba28a1dbe5968
+ docker build --rm -t quay.io/skilbjo/router-logs:dev .
Sending build context to Docker daemon 8.713 MB
Step 1 : FROM armhf/alpine:3.5
 ---> 3ddfeafc01f0
Step 2 : ENV CONSUL_PREFIX router_logs/config
 ---> Using cache
 ---> e2aae782f6d8
Step 3 : ENV CONSUL_SECRET_PREFIX router_logs/secrets
 ---> Using cache
 ---> 71c863da2558
Step 4 : ENV QEMU_EXECVE 1
 ---> Using cache
 ---> a7e80415d0d4
Step 5 : COPY deploy/qemu/qemu-arm-static /usr/bin/
 ---> Using cache
 ---> 265df9b6575f
Step 6 : RUN qemu-arm-static /sbin/apk fix
 ---> Using cache
 ---> def74ac67891
Step 7 : RUN qemu-arm-static /sbin/apk add -U bash
 ---> Running in 6f62d2ecd6b3
fetch http://dl-cdn.alpinelinux.org/alpine/v3.5/main/armhf/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.5/community/armhf/APKINDEX.tar.gz
(1/5) Installing ncurses-terminfo-base (6.0-r7)
(2/5) Installing ncurses-terminfo (6.0-r7)
(3/5) Installing ncurses-libs (6.0-r7)
(4/5) Installing readline (6.3.008-r4)
(5/5) Installing bash (4.3.46-r5)
Executing bash-4.3.46-r5.post-install
ERROR: bash-4.3.46-r5.post-install: script exited with error 1
Executing busybox-1.25.1-r0.trigger
ERROR: busybox-1.25.1-r0.trigger: script exited with error 1
1 errors; 7 MiB in 16 packages
The command 'qemu-arm-static /sbin/apk add -U bash' returned a non-zero code: 1

项目回购在这里:https://github.com/skilbjo/router-logs

原来 FROM armhf/alpine:3.5 不好,FROM resin/armhf-alpine:3.5 就可以了!我很想看到来自 scratch 的命令导致 armhf 图像被破坏,但现在,这有效!

我在使用 Buildx 的多架构选项时遇到了类似的错误。由于以下命令,它已得到修复:

docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
docker buildx rm builder
docker buildx create --name builder --driver docker-container --use
docker buildx inspect --bootstrap

感谢这个回答正确