Skip to content

Commit

Permalink
container: attach to existing container if it exists
Browse files Browse the repository at this point in the history
  • Loading branch information
ahayzen committed Jun 16, 2023
1 parent 1ddc00f commit 0fba7df
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
18 changes: 18 additions & 0 deletions src/container/exec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@ function container_setup_create() {
)
}

function container_setup_exec() {
$PODMAN_EXEC exec \
--interactive --tty \
"$CONTAINER_ID" \
/bin/bash --login
}

function container_setup_is_running() {
$PODMAN_EXEC container inspect \
--format '{{.State.Status}}' \
"$TAG_NAME" \
2>&1 || true
}

function container_setup_running_load_id() {
CONTAINER_ID=$($PODMAN_EXEC container inspect --format '{{.Id}}' "$TAG_NAME")
}

function container_setup_start() {
CONTAINER_ID=$($PODMAN_EXEC start "$CONTAINER_ID")
}
33 changes: 23 additions & 10 deletions src/container/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,30 @@ function container_setup() {
printf "\r(%s) Building image ... " "$TAG_NAME"
container_setup_build

# Create the container with the args, this could take a while the first time with userns=keep-id
printf "\r(%s) Creating container (first time may take a while) ... " "$TAG_NAME"
container_setup_create
# Determine if the container is already running or not
if [ "$(container_setup_is_running)" == "running" ]; then
# Load container id
container_setup_running_load_id

# Start the container
printf "\r(%s) Starting container ... " "$TAG_NAME"
container_setup_start
# Clear console
printf "\r\033[0K"

# Clear console
printf "\r\033[0K"
# Attach to existing container
printf "\r(%s) Attaching to existing with: /bin/bash --login\n" "$TAG_NAME"
container_setup_exec
else
# Create the container with the args, this could take a while the first time with userns=keep-id
printf "\r(%s) Creating container (first time may take a while) ... " "$TAG_NAME"
container_setup_create

# Attach to the container
container_setup_attach
# Start the container
printf "\r(%s) Starting container ... " "$TAG_NAME"
container_setup_start

# Clear console
printf "\r\033[0K"

# Attach to the container
container_setup_attach
fi
}

0 comments on commit 0fba7df

Please sign in to comment.