diff --git a/config/shared/bin/dev b/config/shared/bin/dev index c2444bc..40ac48c 100755 --- a/config/shared/bin/dev +++ b/config/shared/bin/dev @@ -1,41 +1,61 @@ -# --rm: automatically remove the container when it exits +#!/bin/bash -# Check if a container name was provided -if [ $# -eq 0 ]; then - echo "Usage: $0 " +set -e + +REGISTRY="registry.tomastm.com" + +usage() { + echo "Usage: $0 -i " 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 -IMAGE="web-stack" -NAME="${IMAGE}-$1" +NAME="$1" +CONTAINER_NAME="${IMAGE}-${NAME}" -# Function to exec into the 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 -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 - if [ ! "$(docker ps -q -f name=$NAME)" ]; then - echo "Container $NAME exists but is not running. Starting it..." - docker start $NAME + if [ ! "$(docker ps -q -f "name=$CONTAINER_NAME")" ]; then + echo "Container $CONTAINER_NAME exists but is not running. Starting it..." + docker start "$CONTAINER_NAME" else - echo "Container $NAME is already running." + echo "Container $CONTAINER_NAME is already running." fi 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 \ --detach-keys "ctrl-q,ctrl-p" \ --network host \ - -v $HOME/.ssh:/home/dev/.ssh \ - -v $PWD:/workspace \ - --name $NAME \ + -v "$HOME/.ssh:/home/dev/.ssh" \ + -v "$PWD:/workspace" \ + --name "$CONTAINER_NAME" \ --init \ - $IMAGE \ + "$FULL_IMAGE_NAME" \ tail -f /dev/null fi -# Exec into the container -echo "Executing into container $NAME..." +echo "Executing into container $CONTAINER_NAME..." exec_into_container