我正在努力将现有的 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