From a02cb9ff743c9e8b9e02ba1266e28cc3602128b2 Mon Sep 17 00:00:00 2001 From: Michal Nasiadka Date: Tue, 8 Mar 2022 13:03:27 +0000 Subject: [PATCH] containerfile: add support for Docker In some build environments Docker is already installed - and adding podman is not an option. Add a new variable to toggle this, and rename the now incorrectly titled DIB_CONTAINERFILE_PODMAN_ROOT to just ...RUNTIME_ROOT to match. Change-Id: I677e4f491b40360dceabdf4f2a9e64c7cb493dc7 --- .../elements/containerfile/README.rst | 11 +++++++---- .../containerfile/root.d/08-containerfile | 19 +++++++++++++------ roles/dib-functests/tasks/main.yaml | 2 +- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/diskimage_builder/elements/containerfile/README.rst b/diskimage_builder/elements/containerfile/README.rst index ec6d24c82..c734f39a9 100644 --- a/diskimage_builder/elements/containerfile/README.rst +++ b/diskimage_builder/elements/containerfile/README.rst @@ -8,11 +8,14 @@ Dockerfiles). Usually this element will be called via a more specific distro element which provides an environment for building a full image. This element will search active elements for a container file located in -`containerfiles/${DIB_RELEASE}`. +``containerfiles/${DIB_RELEASE}``. Alternatively, to use this element directly supply the path to a container file in the environment variable -`DIB_CONTAINERFILE_DOCKERFILE`. +``DIB_CONTAINERFILE_DOCKERFILE``. -Set ``DIB_CONTAINERFILE_PODMAN_ROOT`` to ``1`` to run `podman` as -`root`. +Set ``DIB_CONTAINERFILE_RUNTIME`` to ``docker`` to use Docker for building +images (default is ``podman``). + +Set ``DIB_CONTAINERFILE_RUNTIME_ROOT`` to ``1`` to run the runtime +(Docker or ``podman``, per above) as ``root``. diff --git a/diskimage_builder/elements/containerfile/root.d/08-containerfile b/diskimage_builder/elements/containerfile/root.d/08-containerfile index 48fd8c5b1..9dbe0d927 100755 --- a/diskimage_builder/elements/containerfile/root.d/08-containerfile +++ b/diskimage_builder/elements/containerfile/root.d/08-containerfile @@ -21,6 +21,13 @@ fi set -eu set -o pipefail +: "${DIB_CONTAINERFILE_RUNTIME:=podman}" + +# Convert the old value which was podman specific +if [[ "${DIB_CONTAINERFILE_PODMAN_ROOT:-0}" != '0' ]]; then + DIB_CONTAINERFILE_RUNTIME_ROOT=1 +fi + if [ -f ${TARGET_ROOT}/.extra_settings ] ; then . ${TARGET_ROOT}/.extra_settings fi @@ -52,7 +59,7 @@ DIB_CONTAINER_CONTEXT=${DIB_CONTAINER_CONTEXT:-${DIB_IMAGE_CACHE}/containerfile} mkdir -p $DIB_CONTAINER_CONTEXT -if [[ ${DIB_CONTAINERFILE_PODMAN_ROOT:-0} -gt 0 ]]; then +if [[ ${DIB_CONTAINERFILE_RUNTIME_ROOT:-0} -gt 0 ]]; then _sudo="sudo" else _sudo="" @@ -63,17 +70,17 @@ _podman_export_container="dib-tmp-export-$RANDOM" function podman_cleanup() { echo "Cleaning up container ${_podman_export_container}" - ${_sudo} podman rm ${_podman_export_container} || true + ${_sudo} ${DIB_CONTAINERFILE_RUNTIME} rm ${_podman_export_container} || true echo "Cleaning up build image ${_podman_build_image}" - ${_sudo} podman rmi ${_podman_build_image} || true + ${_sudo} ${DIB_CONTAINERFILE_RUNTIME} rmi ${_podman_build_image} || true } trap "podman_cleanup" EXIT -${_sudo} podman build -t ${_podman_build_image} -f $DIB_CONTAINERFILE_DOCKERFILE $DIB_CONTAINER_CONTEXT -${_sudo} podman run --name ${_podman_export_container} -d ${_podman_build_image} /bin/sh +${_sudo} ${DIB_CONTAINERFILE_RUNTIME} build -t ${_podman_build_image} -f $DIB_CONTAINERFILE_DOCKERFILE $DIB_CONTAINER_CONTEXT +${_sudo} ${DIB_CONTAINERFILE_RUNTIME} run --name ${_podman_export_container} -d ${_podman_build_image} /bin/sh # NOTE(ianw) 2021-11-10 the tar must always be sudo to write out the chroot files # as other uids -${_sudo} podman export ${_podman_export_container} | sudo tar -C $TARGET_ROOT --numeric-owner -xf - +${_sudo} ${DIB_CONTAINERFILE_RUNTIME} export ${_podman_export_container} | sudo tar -C $TARGET_ROOT --numeric-owner -xf - sudo rm -f ${TARGET_ROOT}/.extra_settings diff --git a/roles/dib-functests/tasks/main.yaml b/roles/dib-functests/tasks/main.yaml index e096706a1..4573f3c94 100644 --- a/roles/dib-functests/tasks/main.yaml +++ b/roles/dib-functests/tasks/main.yaml @@ -66,4 +66,4 @@ DIB_OS_CI_YUM_REPOS: "{{ dib_gate_mirror_repos|default(omit) }}" # NOTE(ianw) 2021-10-15 : this might be our bullseye images # having issues with non-root podman. This works for now. - DIB_CONTAINERFILE_PODMAN_ROOT: 1 + DIB_CONTAINERFILE_RUNTIME_ROOT: 1