64 lines
1.6 KiB
Bash
Executable File
64 lines
1.6 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
set -e
|
|
|
|
REGISTRY="registry.tomastm.com"
|
|
|
|
usage() {
|
|
echo "Usage: $0 -i <image> <name>"
|
|
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
|
|
|
|
NAME="$1"
|
|
CONTAINER_NAME="${IMAGE}-${NAME}"
|
|
|
|
exec_into_container() {
|
|
docker exec --detach-keys "ctrl-q,ctrl-p" -it "$CONTAINER_NAME" bash -c "tmux new -A -s $NAME"
|
|
}
|
|
|
|
# Check if the container exists
|
|
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=$CONTAINER_NAME")" ]; then
|
|
echo "Container $CONTAINER_NAME exists but is not running. Starting it..."
|
|
docker start "$CONTAINER_NAME"
|
|
else
|
|
echo "Container $CONTAINER_NAME is already running."
|
|
fi
|
|
else
|
|
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" \
|
|
-v /var/run/docker.sock:/var/run/docker.sock \
|
|
--name "$CONTAINER_NAME" \
|
|
--init \
|
|
"$FULL_IMAGE_NAME" \
|
|
sleep infinity
|
|
# tail -f /dev/null
|
|
fi
|
|
|
|
echo "Executing into container $CONTAINER_NAME..."
|
|
exec_into_container
|