From 03debbe2e8b92caee1a4d1c27e7a1254b896fb8b Mon Sep 17 00:00:00 2001 From: nosamad Date: Tue, 2 Apr 2019 18:36:38 +0200 Subject: [PATCH] fixes run in dind setup --- Makefile | 18 +++++++++++------- build/run_image.template | 34 +++++++++++++++++++++++++++------- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index 9399e94..06a4e5e 100644 --- a/Makefile +++ b/Makefile @@ -150,13 +150,16 @@ echo_if_silent = VERBOSE=1 $(VERBOSE)echo_if_silent = echo $1 $(VERBOSE)SILENT := @ -$(HOST_CONTAINER)image_run_volumes += '$(realpath $(PROJECT_ROOT)):$(CONTAINER_PROJECT_ROOT):cached' -$(HOST_CONTAINER)image_run_volumes += '$(realpath $(OUT)/$1):$(CONTAINER_OUT)/$1:delegated' +$(HOST_CONTAINER)image_run_volumes += --volume '$(realpath $(PROJECT_ROOT)):$(CONTAINER_PROJECT_ROOT):cached' +$(HOST_CONTAINER)image_run_volumes += --volume '$(realpath $(OUT)/$1):$(CONTAINER_OUT)/$1:delegated' +image_run_volumes += $(addprefix --volumes-from ,$2) image_name = $(REGISTRY_PREFIX)$(subst -,/,$1)/$(PROJECT_NAME):$(VERSION) -image_run = $(DOCKER) run $(DOCKER_RUNFLAGS) $3 \ - $(addprefix --volume ,$(call image_run_volumes,$1)) \ +image_run = $(DOCKER) run $(DOCKER_RUNFLAGS) \ + $(call image_run_volumes,$1,$(HOST_CONTAINER)) \ + $(addprefix --cgroup-parent ,$(CONTAINER_CGROUP_PARENT)) \ --workdir '$(CONTAINER_OUT)/$1/$(BUILDTYPE)' \ + $3 \ $(call image_name,$1) \ $2 @@ -214,10 +217,11 @@ wrapper_rule = \ wrapper = \ $(call echo_if_silent,generating $@) \ && sed \ + -e 's@%PROJECT_ROOT%@$(abspath $(PROJECT_ROOT))@g' \ -e 's@%DOCKER%@$(DOCKER)@g' \ - -e 's@%DOCKER_RUNFLAGS%@$(DOCKER_RUNFLAGS) $(addprefix --volume ,$(call image_run_volumes,$1))@g' \ - -e 's@%DOCKER_IMAGE%@$(call image_name,$1)@g' \ - -e 's@%DOCKER_RUNCMD%@$(notdir $@)@g' \ + -e 's@%RUNFLAGS%@$(DOCKER_RUNFLAGS) $(call image_run_volumes,$1)@g' \ + -e 's@%IMAGE%@$(call image_name,$1)@g' \ + -e 's@%RUNCMD%@$(notdir $@)@g' \ $< > $@ \ && chmod +x $@ diff --git a/build/run_image.template b/build/run_image.template index 888e0b5..d199b1e 100644 --- a/build/run_image.template +++ b/build/run_image.template @@ -2,15 +2,35 @@ set -e -DOCKER="${DOCKER:-'%DOCKER%'}" -DOCKER_IMAGE="{DOCKER_IMAGE:-'%DOCKER_IMAGE%'}" +PROJECT_ROOT="${PROJECT_ROOT:-%PROJECT_ROOT%}" -DOCKER_RUNFLAGS+=( %DOCKER_RUNFLAGS% ) -DOCKER_RUNFLAGS+=( --network host ) -DOCKER_RUNFLAGS+=( --workdir "$PWD" ) +DOCKER="${DOCKER:-%DOCKER%}" +IMAGE="${IMAGE:-%IMAGE%}" +NETWORK="${NETWORK:-host}" +HOST_CONTAINER="${HOST_CONTAINER:-"$("$PROJECT_ROOT/build/get_container_id.sh")"}" -DOCKER_RUNCMD=( %DOCKER_RUNCMD% ) +# shellcheck disable=SC2162 +read -a ADDITIONAL_RUNFLAGS <<< "$RUNFLAGS" -set -- "${DOCKER_RUNFLAGS[@]}" "%DOCKER_IMAGE%" "${DOCKER_RUNCMD[@]}" "$@" +RUNFLAGS=() + +# shellcheck disable=SC2191 +RUNFLAGS+=( %RUNFLAGS% ) + +if [[ -n "$HOST_CONTAINER" ]]; then + RUNFLAGS+=( --volumes-from "$HOST_CONTAINER" ) +fi + +RUNFLAGS+=( --network "$NETWORK" ) + +if [[ -n "$CONTAINER_CGROUP_PARENT" ]]; then + RUNFLAGS+=( --cgroup-parent "$CONTAINER_CGROUP_PARENT" ) +fi + +RUNFLAGS+=( --workdir "$PWD" ) + +RUNCMD=( %RUNCMD% ) + +set -- "${RUNFLAGS[@]}" "${ADDITIONAL_RUNFLAGS[@]}" "$IMAGE" "${RUNCMD[@]}" "$@" exec "$DOCKER" run "$@"