From 72723c4ca77dbef9e4c23621e17d225c78b04fe0 Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Tue, 2 Nov 2021 11:38:29 -0700 Subject: [PATCH] stdlib/docker: #Run checks if the container exists and can optionnally recreate it Signed-off-by: Sam Alba --- stdlib/docker/docker.cue | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/stdlib/docker/docker.cue b/stdlib/docker/docker.cue index b2c9882b..1697f0c0 100644 --- a/stdlib/docker/docker.cue +++ b/stdlib/docker/docker.cue @@ -200,6 +200,9 @@ import ( // Container name name?: dagger.#Input & {string} + // Recreate container? + recreate: dagger.#Input & {bool | *true} + // Image registry registry?: { target: string @@ -216,6 +219,16 @@ import ( if [ ! -z "$CONTAINER_NAME" ]; then OPTS="$OPTS --name $CONTAINER_NAME" + docker inspect "$CONTAINER_NAME" >/dev/null && { + # Container already exists + if [ ! -z "$CONTAINER_RECREATE" ]; then + echo "Replacing container $CONTAINER_NAME" + docker stop "$CONTAINER_NAME" + docker rm "$CONTAINER_NAME" + else + echo "$CONTAINER_NAME already exists" + fi + } fi if [ ! -z "$CONTAINER_PORTS" ]; then @@ -240,6 +253,10 @@ import ( CONTAINER_NAME: name } + if recreate { + CONTAINER_RECREATE: "true" + } + if ports != _|_ { CONTAINER_PORTS: strings.Join(ports, " -p ") }