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

时间:2017-04-02 05:25:34

标签: bash docker arm qemu alpine

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

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

我的理解是在运行apk -U add bash时,apk会更新包列表 从存储库然后安装所请求的最新版本的软件包。 然后运行安装后脚本。这些安装后脚本似乎失败了。 但是,当我构建没有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

2 个答案:

答案 0 :(得分:1)

事实证明FROM armhf/alpine:3.5并不好,FROM resin/armhf-alpine:3.5会做到这一点!我希望能够看到来自scratch的导致armhf形象出现问题的来源,但就目前而言,这是有效的!

答案 1 :(得分:1)

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

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

感谢这个答案here