Skip to content

Commit

Permalink
feat: add prefixing to docker context for avoiding collision
Browse files Browse the repository at this point in the history
  • Loading branch information
JGiola committed Oct 27, 2023
1 parent 5372fdd commit 30a35a5
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions containers/docker/docker_helpers.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#!/usr/bin/env bash

docker_buildx_ctx_name='pipelines'
docker_builder_name='pipelines'

docker_login() {
local username="${1}"
local password="${2}"
Expand Down Expand Up @@ -54,9 +51,10 @@ docker_create_sbom_and_sign_image() {
}

setup_docker_context() {
local prefix=${1:-pipeline}
# to avoid weird situation where the context or builder are not
# properly deleted between runs we ensure to clean them up
cleanup_docker_context || true
cleanup_docker_context "${1}" || true

# In order for `docker buildx create` to work, we need to replace DOCKER_HOST with a Docker context.
# Otherwise, we get the following error:
Expand All @@ -65,17 +63,18 @@ setup_docker_context() {
if [ -n "${DOCKER_CERT_PATH}" ]; then
docker="host=${DOCKER_HOST},ca=${DOCKER_CERT_PATH}/ca.pem,cert=${DOCKER_CERT_PATH}/cert.pem,key=${DOCKER_CERT_PATH}/key.pem"
fi
docker context create "${docker_buildx_ctx_name}" \
--default-stack-orchestrator=swarm \
docker context create "${prefix}-context" \
--description "Pipelines buildx Docker context" \
--docker "${docker}"

docker buildx create --use --name "${docker_builder_name}" "${docker_buildx_ctx_name}"
docker buildx create --bootstrap --use --name "${prefix}-builder" "${prefix}-context"
}

cleanup_docker_context() {
local prefix=${1:-pipeline}

set +e
docker buildx rm "${docker_builder_name}" >/dev/null 2>&1
docker context rm -f "${docker_buildx_ctx_name}" >/dev/null 2>&1
docker buildx rm -f "${prefix}-builder" >/dev/null 2>&1
docker context rm -f "${prefix}-context" >/dev/null 2>&1
set -e
}

0 comments on commit 30a35a5

Please sign in to comment.