This commit is contained in:
Your Name
2025-04-21 14:54:33 +12:00
parent 0a2036cbd7
commit 50bd2e2446
13 changed files with 288 additions and 115 deletions

View File

@ -1,5 +1,7 @@
#!/bin/bash
source _dockerhelper.sh
# Print error message and exit with code 1
# Usage: die "error message"
die() {
@ -44,47 +46,11 @@ grey_end() {
echo -e -n "\033[0m"
}
# Test if Docker is installed and working on the local machine
# Returns 0 if Docker is working, 1 if there are any issues
test_docker() {
echo "Testing Docker on local machine..."
# Test Docker installation
if ! command -v docker >/dev/null 2>&1; then
die "Docker is not installed on this machine"
fi
# Test Docker daemon is running
if ! docker info >/dev/null 2>&1; then
die "Docker daemon is not running"
fi
# Test Docker can run containers
if ! docker run --rm hello-world >/dev/null 2>&1; then
die "Docker cannot run containers"
fi
echo "Docker is working correctly on local machine"
return 0
}
start_container() {
# Check if container exists and is stopped
echo "Checking container status..."
if docker ps -a --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
if docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
die "Container ${CONTAINER_NAME} is already running"
else
echo "Starting existing container ${CONTAINER_NAME}..."
grey_start
if ! docker start "${CONTAINER_NAME}"; then
die "Failed to start container ${CONTAINER_NAME}"
fi
grey_end
fi
create_and_start_container() {
if _is_container_exists $CONTAINER_NAME; then
_is_container_running $CONTAINER_NAME && return 0
_start_container $CONTAINER_NAME
else
echo "Creating and starting new container ${CONTAINER_NAME}..."
grey_start
docker run -d \
--restart unless-stopped \
@ -95,33 +61,11 @@ start_container() {
grey_end
fi
# check if the container is running
if ! docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
die "Container ${CONTAINER_NAME} is not running"
if ! _is_container_running $CONTAINER_NAME; then
die "Container ${CONTAINER_NAME} failed to start"
fi
# print the container id
ID=$(docker ps --format '{{.ID}}' --filter "name=^${CONTAINER_NAME}$")
ID=$(_get_container_id $CONTAINER_NAME)
echo "Container ${CONTAINER_NAME} is running with ID ${ID}"
return 0
}
stop_container() {
# Check if container is running
echo "Checking if container is running..."
if docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
echo "Stopping container ${CONTAINER_NAME}..."
grey_start
if ! docker stop "${CONTAINER_NAME}"; then
die "Failed to stop container ${CONTAINER_NAME}"
fi
grey_end
echo "Container ${CONTAINER_NAME} stopped successfully"
return 0
else
echo "Container ${CONTAINER_NAME} is not running"
return 1
fi
}