Files
markdownblog/manage_container.sh
rattatwinko 682e006e0b docker shit
2025-06-16 22:20:38 +02:00

228 lines
6.0 KiB
Bash
Executable File

#!/bin/bash
# Configuration
IMAGE_NAME="markdown-blog"
CONTAINER_NAME="markdown-blog-container"
PORT=8080
MARKDOWN_DIR="/home/rattatwinko/Documents/shit/markdownblog" # Update this to your local markdown directory
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Function to check if Docker is running
check_docker() {
if ! docker info > /dev/null 2>&1; then
echo -e "${RED}Error: Docker is not running${NC}"
exit 1
fi
}
# Function to check if container exists
container_exists() {
docker ps -a --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"
}
# Function to check if container is running
container_running() {
docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"
}
# Function to check container health
check_container_health() {
local health_status
health_status=$(docker inspect --format='{{.State.Health.Status}}' $CONTAINER_NAME 2>/dev/null)
if [ "$health_status" = "healthy" ]; then
return 0
else
return 1
fi
}
# Function to build the Docker image
build_image() {
echo -e "${YELLOW}Building Docker image...${NC}"
if ! docker build -t $IMAGE_NAME .; then
echo -e "${RED}Error: Failed to build Docker image${NC}"
exit 1
fi
echo -e "${GREEN}Docker image built successfully${NC}"
}
# Function to start the container
start_container() {
echo -e "${YELLOW}Starting container...${NC}"
# Check if container already exists
if container_exists; then
if container_running; then
echo -e "${YELLOW}Container is already running${NC}"
return
else
echo -e "${YELLOW}Container exists but is not running. Starting it...${NC}"
if ! docker start $CONTAINER_NAME; then
echo -e "${RED}Error: Failed to start existing container${NC}"
exit 1
fi
fi
else
# Create and start new container
if ! docker run -d \
--name $CONTAINER_NAME \
-p $PORT:8080 \
-v "$MARKDOWN_DIR:/markdown" \
--restart unless-stopped \
--health-cmd="wget --no-verbose --tries=1 --spider http://localhost:8080/ || exit 1" \
--health-interval=30s \
--health-timeout=30s \
--health-retries=3 \
--health-start-period=5s \
$IMAGE_NAME; then
echo -e "${RED}Error: Failed to create and start container${NC}"
exit 1
fi
fi
# Wait for container to be ready
echo -e "${YELLOW}Waiting for container to be ready...${NC}"
local max_attempts=60
local attempt=1
while [ $attempt -le $max_attempts ]; do
if check_container_health; then
echo -e "${GREEN}Container is healthy and accessible at http://localhost:$PORT${NC}"
return
fi
# Check if container is still running
if ! container_running; then
echo -e "${RED}Error: Container stopped unexpectedly${NC}"
docker logs $CONTAINER_NAME
exit 1
fi
echo -n "."
sleep 1
attempt=$((attempt + 1))
done
echo -e "${RED}Error: Container did not become healthy in time${NC}"
docker logs $CONTAINER_NAME
exit 1
}
# Function to stop the container
stop_container() {
echo -e "${YELLOW}Stopping container...${NC}"
if ! container_exists; then
echo -e "${YELLOW}Container does not exist${NC}"
return
fi
if ! docker stop $CONTAINER_NAME; then
echo -e "${RED}Error: Failed to stop container${NC}"
exit 1
fi
echo -e "${GREEN}Container stopped successfully${NC}"
}
# Function to remove the container
remove_container() {
echo -e "${YELLOW}Removing container...${NC}"
if ! container_exists; then
echo -e "${YELLOW}Container does not exist${NC}"
return
fi
if container_running; then
stop_container
fi
if ! docker rm $CONTAINER_NAME; then
echo -e "${RED}Error: Failed to remove container${NC}"
exit 1
fi
echo -e "${GREEN}Container removed successfully${NC}"
}
# Function to restart the container
restart_container() {
echo -e "${YELLOW}Restarting container...${NC}"
if ! container_exists; then
echo -e "${YELLOW}Container does not exist. Starting new container...${NC}"
start_container
return
fi
if ! docker restart $CONTAINER_NAME; then
echo -e "${RED}Error: Failed to restart container${NC}"
exit 1
fi
echo -e "${GREEN}Container restarted successfully${NC}"
}
# Function to view logs
view_logs() {
echo -e "${YELLOW}Viewing logs...${NC}"
if ! container_exists; then
echo -e "${RED}Error: Container does not exist${NC}"
exit 1
fi
docker logs -f $CONTAINER_NAME
}
# Function to show container status
show_status() {
echo -e "${YELLOW}Container Status:${NC}"
if ! container_exists; then
echo -e "${RED}Container does not exist${NC}"
return
fi
if container_running; then
local health_status
health_status=$(docker inspect --format='{{.State.Health.Status}}' $CONTAINER_NAME 2>/dev/null)
echo -e "${GREEN}Container is running${NC}"
echo "Health status: $health_status"
echo "Access the application at: http://localhost:$PORT"
else
echo -e "${YELLOW}Container exists but is not running${NC}"
fi
}
# Check Docker is running before proceeding
check_docker
# Main script logic
case "$1" in
build)
build_image
;;
start)
start_container
;;
stop)
stop_container
;;
restart)
restart_container
;;
logs)
view_logs
;;
status)
show_status
;;
remove)
remove_container
;;
*)
echo "Usage: $0 {build|start|stop|restart|logs|status|remove}"
exit 1
;;
esac
exit 0