Redo install with docker container
This commit is contained in:
109
install.sh
109
install.sh
@@ -1,40 +1,103 @@
|
||||
#!/bin/bash
|
||||
|
||||
# This script creates a config.json file with predefined settings.
|
||||
# You can extend this script to include other installation requirements,
|
||||
# such as installing dependencies or setting up your environment.
|
||||
# Define ANSI color codes
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[0;33m' # Added yellow for warnings/info
|
||||
NC='\033[0m' # No Color - resets text to default color
|
||||
|
||||
# Function to check if Docker is installed and running
|
||||
check_docker_status() {
|
||||
echo -e "${YELLOW}Checking Docker status...${NC}"
|
||||
if ! command -v docker &> /dev/null; then
|
||||
echo -e "${RED}Error: Docker is not installed. Please install Docker to proceed.${NC}"
|
||||
echo -e "${RED}You can find installation instructions at: https://docs.docker.com/get-docker/${NC}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! docker info &> /dev/null; then
|
||||
echo -e "${RED}Error: Docker daemon is not running. Please start Docker and try again.${NC}"
|
||||
exit 1
|
||||
fi
|
||||
echo -e "${GREEN}Docker is installed and running.${NC}"
|
||||
}
|
||||
|
||||
create_config_json() {
|
||||
echo "Creating config.json file..."
|
||||
echo -e "${GREEN}Creating config.json file...${NC}"
|
||||
|
||||
# Define the JSON content directly as a multi-line string
|
||||
local config_content='{
|
||||
"SERVER_WS_URI": "ws://drb-sock.vpn.cusano.net",
|
||||
"SERVER_API_URL": "https://drb-api.vpn.cusano.net",
|
||||
"CLIENT_API_URL": "http://localhost:8001",
|
||||
"nickname": ""
|
||||
}'
|
||||
|
||||
# Create the data dir
|
||||
mkdir -p ./data/
|
||||
"SERVER_WS_URI": "ws://drb-sock.vpn.cusano.net",
|
||||
"SERVER_API_URL": "https://drb-api.vpn.cusano.net",
|
||||
"CLIENT_API_URL": "http://localhost:8001",
|
||||
"nickname": ""
|
||||
}'
|
||||
|
||||
# Write the content to config.json
|
||||
# The 'echo' command with a multi-line string is used to write the content.
|
||||
echo "$config_content" > ./data/config.json
|
||||
echo "$config_content" > config.json
|
||||
|
||||
# Check if the file was successfully created
|
||||
if [ -f "./data/config.json" ]; then
|
||||
echo "Successfully created './data/config.json'."
|
||||
echo "Content of config.json:"
|
||||
cat ./data/config.json
|
||||
if [ -f "config.json" ]; then
|
||||
echo -e "${GREEN}Successfully created 'config.json'.${NC}"
|
||||
echo -e "${GREEN}Content of config.json:${NC}"
|
||||
cat config.json
|
||||
else
|
||||
echo "Error: Failed to create './data/config.json'."
|
||||
echo -e "${RED}Error: Failed to create 'config.json'.${NC}"
|
||||
exit 1 # Exit with an error code if file creation fails
|
||||
fi
|
||||
}
|
||||
|
||||
# Call the function to create the config.json file
|
||||
create_config_json
|
||||
echo "--- Config Created ---"
|
||||
start_docker_container() {
|
||||
local container_name="websocket-client-app-container" # A unique name for your container
|
||||
local image_name="websocket-client-app" # From your Makefile
|
||||
|
||||
echo "--- Script finished ---"
|
||||
echo -e "${GREEN}Building Docker image '${image_name}'...${NC}"
|
||||
# Build the Docker image from the current directory
|
||||
if ! docker build -t "${image_name}" .; then
|
||||
echo -e "${RED}Error: Failed to build Docker image '${image_name}'. Please check your Dockerfile and context.${NC}"
|
||||
exit 1
|
||||
fi
|
||||
echo -e "${GREEN}Docker image '${image_name}' built successfully.${NC}"
|
||||
|
||||
echo -e "${GREEN}Starting Docker container...${NC}"
|
||||
|
||||
# Stop and remove existing container if it's running/exists
|
||||
if docker ps -a --format '{{.Names}}' | grep -q "^${container_name}$"; then
|
||||
echo -e "${YELLOW}Container '${container_name}' already exists. Stopping and removing it...${NC}"
|
||||
docker stop "${container_name}" &> /dev/null
|
||||
docker rm "${container_name}" &> /dev/null
|
||||
if [ $? -ne 0 ]; then
|
||||
echo -e "${RED}Warning: Could not stop/remove existing container '${container_name}'. It might not be running.${NC}"
|
||||
fi
|
||||
fi
|
||||
|
||||
echo -e "${GREEN}Starting new container '${container_name}' from image '${image_name}' with restart policy 'unless-stopped'.${NC}"
|
||||
# Run the container
|
||||
if docker run -d \
|
||||
--name "${container_name}" \
|
||||
--restart unless-stopped \
|
||||
-v "$PWD/data":/data \
|
||||
--network=host \
|
||||
"${image_name}"; then
|
||||
echo -e "${GREEN}Docker container '${container_name}' started successfully in detached mode.${NC}"
|
||||
echo -e "${GREEN}It is configured to restart automatically on daemon startup or if it exits (unless stopped manually).${NC}"
|
||||
echo -e "${GREEN}You can check its status with: docker ps -f name=${container_name}${NC}"
|
||||
echo -e "${GREEN}You can view its logs with: docker logs -f ${container_name}${NC}"
|
||||
else
|
||||
echo -e "${RED}Error: Failed to start Docker container '${container_name}'.${NC}"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# --- Main script execution ---
|
||||
|
||||
# 1. Check Docker status
|
||||
check_docker_status
|
||||
|
||||
# 2. Create config.json
|
||||
create_config_json
|
||||
|
||||
# 3. Build and Start Docker container
|
||||
start_docker_container
|
||||
|
||||
echo -e "${GREEN}--- All installation and startup steps finished ---${NC}"
|
||||
Reference in New Issue
Block a user