我正在将现有的 CentOS 7 Docker 映像构建过程转换为使用 Buildah,但遇到了一个问题mknod
。
目前我使用以下 Dockerfile 来构建新镜像。因此,基本上使用镜像构建镜像以避免对构建主机的依赖。我像这样运行它:
docker build -f Dockerfile.builder -t centosbuilder:latest . && \
docker run --rm centosbuilder:latest | docker import - mycentos:latest
这是 Dockerfile:
FROM centos:7
ENV ROOTFS /image_build_root
ENV TMPDIR /tmp/image_build
WORKDIR ${TMPDIR}
RUN mkdir -p ${ROOTFS}
RUN mkdir -m 755 ${ROOTFS}/dev && \
mknod -m 600 ${ROOTFS}/dev/console c 5 1 && \
mknod -m 600 ${ROOTFS}/dev/initctl p && \
mknod -m 666 ${ROOTFS}/dev/full c 1 7 && \
mknod -m 666 ${ROOTFS}/dev/null c 1 3 && \
mknod -m 666 ${ROOTFS}/dev/ptmx c 5 2 && \
mknod -m 666 ${ROOTFS}/dev/random c 1 8 && \
mknod -m 666 ${ROOTFS}/dev/tty c 5 0 && \
mknod -m 666 ${ROOTFS}/dev/tty0 c 4 0 && \
mknod -m 666 ${ROOTFS}/dev/urandom c 1 9 && \
mknod -m 666 ${ROOTFS}/dev/zero c 1 5
RUN yum --installroot=${ROOTFS} --releasever=7 --setopt=tsflags=nodocs --setopt=group_package_types=mandatory -y groupinstall Core
RUN yum --installroot=${ROOTFS} -y clean all
RUN echo "NETWORKING=yes" > ${ROOTFS}/etc/sysconfig/network && \
echo "HOSTNAME=localhost.localdomain" > ${ROOTFS}/etc/sysconfig/network
RUN rm -rf ${ROOTFS}/usr/{{lib,share}/locale,{lib,lib64}/gconv,bin/localedef,sbin/build-locale-archive} && \
# docs and man pages
rm -rf ${ROOTFS}/usr/share/{man,doc,info,gnome/help} && \
# cracklib
rm -rf ${ROOTFS}/usr/share/cracklib && \
# i18n
rm -rf ${ROOTFS}/usr/share/i18n && \
# yum cache
rm -rf ${ROOTFS}/var/cache/yum && \
mkdir -p --mode=0755 ${ROOTFS}/var/cache/yum && \
# sln
rm -rf ${ROOTFS}/sbin/sln && \
# ldconfig
rm -rf ${ROOTFS}/etc/ld.so.cache ${ROOTFS}/var/cache/ldconfig && \
mkdir -p --mode=0755 ${ROOTFS}/var/cache/ldconfig
CMD /usr/bin/tar -cf - -C ${ROOTFS} .
使用 Buildah 时,我在这一行遇到了第一个错误:
$ buildah run "${BUILDER}" mknod -m 600 "${ROOTFS}/dev/console" c 5 1
/usr/bin/mknod: '/image_build_root/dev/console': Operation not permitted
ERRO exit status 1
我尝试以 root 身份运行构建脚本(见下文),但无济于事。我假设可能CAP_MKNOD
没有设置该功能,但即使我手动启用它,我也会收到相同的错误。
构建脚本:
#!/bin/bash -x
set -e
ROOTFS=/image_build_root
TMPDIR=/tmp/image_build
BUILDER="$(buildah from docker.io/library/centos:7)"
buildah config --workingdir "${TMPDIR}" "${BUILDER}"
buildah run "${BUILDER}" mkdir -p "${ROOTFS}"
buildah run "${BUILDER}" mkdir -m 755 "${ROOTFS}/dev"
buildah run --cap-add=CAP_MKNOD "${BUILDER}" mknod -m 600 "${ROOTFS}/dev/console" c 5 1
buildah run "${BUILDER}" mknod -m 600 "${ROOTFS}/dev/initctl" p
buildah run "${BUILDER}" mknod -m 666 "${ROOTFS}/dev/full" c 1 7
buildah run "${BUILDER}" mknod -m 666 "${ROOTFS}/dev/null" c 1 3
buildah run "${BUILDER}" mknod -m 666 "${ROOTFS}/dev/ptmx" c 5 2
buildah run "${BUILDER}" mknod -m 666 "${ROOTFS}/dev/random" c 1 8
buildah run "${BUILDER}" mknod -m 666 "${ROOTFS}/dev/tty" c 5 0
buildah run "${BUILDER}" mknod -m 666 "${ROOTFS}/dev/tty0" c 4 0
buildah run "${BUILDER}" mknod -m 666 "${ROOTFS}/dev/urandom" c 1 9
buildah run "${BUILDER}" mknod -m 666 "${ROOTFS}/dev/zero" c 1 5
buildah run "${BUILDER}" yum \
--installroot="${ROOTFS}" \
--releasever=7 \
--setopt=tsflags=nodocs \
--setopt=group_package_types=mandatory \
-y \
groupinstall Core
buildah run "${BUILDER}" yum --installroot="${ROOTFS}" -y clean all
buildah run "${BUILDER}" echo "NETWORKING=yes" > "${ROOTFS}/etc/sysconfig/network"
buildah run "${BUILDER}" echo "HOSTNAME=localhost.localdomain" > "${ROOTFS}/etc/sysconfig/network"
buildah run "${BUILDER}" rm -rf \
"${ROOTFS}/usr/{{lib,share}/locale,{lib,lib64}/gconv,bin/localedef,sbin/build-locale-archive}" \
"${ROOTFS}/usr/share/{man,doc,info,gnome/help}" \
"${ROOTFS}/usr/share/cracklib" \
"${ROOTFS}/usr/share/i18n" \
"${ROOTFS}/var/cache/yum" \
"${ROOTFS}/sbin/sln" \
"${ROOTFS}/etc/ld.so.cache" \
"${ROOTFS}/var/cache/ldconfig"
buildah run "${BUILDER}" mkdir -p --mode=0755 "${ROOTFS}/var/cache/yum"
buildah run "${BUILDER}" mkdir -p --mode=0755 "${ROOTFS}/var/cache/ldconfig"
buildah config --cmd /usr/bin/tar -cf - -C "${ROOTFS}" . "${BUILDER}"
buildah commit "${BUILDER}" centos7builder
答案1
非特权用户 (rootless) 没有足够的权限使用 mknod,内核会阻止这种情况。无论用户命名空间中还剩下多少功能,它都无法使用它。
请参阅以下问题。
无法同时使用mknod
和buildah bud/run --isolation="rootless"
。您需要忽略mknod
或使用另一个isolation
。无法混合使用它们。这太糟糕了,什么也做不了。