arg image on dev

This commit is contained in:
Tomas Mirchev 2025-02-25 10:49:53 +01:00
parent a1e843a29d
commit 0785551fd6

View File

@ -1,41 +1,61 @@
# --rm: automatically remove the container when it exits #!/bin/bash
# Check if a container name was provided set -e
if [ $# -eq 0 ]; then
echo "Usage: $0 <container_name>" REGISTRY="registry.tomastm.com"
usage() {
echo "Usage: $0 -i <image> <name>"
exit 1 exit 1
}
# Parse arguments
while getopts ":i:" opt; do
case ${opt} in
i )
IMAGE="${OPTARG}" # User-provided image (without registry)
FULL_IMAGE_NAME="${REGISTRY}/${IMAGE}" # Full registry image path
;;
\? )
usage
;;
esac
done
shift "$((OPTIND -1))"
if [ -z "$FULL_IMAGE_NAME" ] || [ -z "$1" ]; then
usage
fi fi
IMAGE="web-stack" NAME="$1"
NAME="${IMAGE}-$1" CONTAINER_NAME="${IMAGE}-${NAME}"
# Function to exec into the container
exec_into_container() { exec_into_container() {
docker exec --detach-keys "ctrl-q,ctrl-p" -it $NAME /bin/zsh docker exec --detach-keys "ctrl-q,ctrl-p" -it "$CONTAINER_NAME" /bin/zsh
} }
# Check if the container exists # Check if the container exists
if [ "$(docker ps -a -q -f name=$NAME)" ]; then if [ "$(docker ps -a -q -f "name=$CONTAINER_NAME")" ]; then
# Container exists, start it if it's not running # Container exists, start it if it's not running
if [ ! "$(docker ps -q -f name=$NAME)" ]; then if [ ! "$(docker ps -q -f "name=$CONTAINER_NAME")" ]; then
echo "Container $NAME exists but is not running. Starting it..." echo "Container $CONTAINER_NAME exists but is not running. Starting it..."
docker start $NAME docker start "$CONTAINER_NAME"
else else
echo "Container $NAME is already running." echo "Container $CONTAINER_NAME is already running."
fi fi
else else
echo "Container $NAME does not exist. Creating and running it in detached mode..." echo "Container $CONTAINER_NAME does not exist. Creating and running it in detached mode..."
docker run -d \ docker run -d \
--detach-keys "ctrl-q,ctrl-p" \ --detach-keys "ctrl-q,ctrl-p" \
--network host \ --network host \
-v $HOME/.ssh:/home/dev/.ssh \ -v "$HOME/.ssh:/home/dev/.ssh" \
-v $PWD:/workspace \ -v "$PWD:/workspace" \
--name $NAME \ --name "$CONTAINER_NAME" \
--init \ --init \
$IMAGE \ "$FULL_IMAGE_NAME" \
tail -f /dev/null tail -f /dev/null
fi fi
# Exec into the container echo "Executing into container $CONTAINER_NAME..."
echo "Executing into container $NAME..."
exec_into_container exec_into_container