Created
November 8, 2023 17:27
-
-
Save BTabaku/133b792ff0bb3c10a108bed059cd646a to your computer and use it in GitHub Desktop.
Docker initializer script to auto-deploy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Get the current working directory | |
current_dir=$(pwd) | |
echo "Starting deployment..." | |
echo $current_dir | |
# Define the Docker image and container details | |
image_name="op-mediadl-os" | |
container_name="op-mediadl-container" | |
port_mapping="5069:5000" | |
volume_mapping="~/downloads:/app/downloads" | |
# Define the absolute paths to the Docker Compose files | |
declare -a compose_files=( | |
"$current_dir/op-mediadl-frontend/docker-compose.yml" | |
) | |
# Function to run docker-compose commands | |
run_docker_compose() { | |
local file=$1 | |
echo "Running docker-compose command for $file" | |
docker-compose -f "$file" down && docker-compose -f "$file" up -d --build | |
} | |
# Function to build and run Docker image | |
build_and_run_docker() { | |
# Check if the Docker image exists and remove it | |
if [ "$(docker images -q $image_name 2> /dev/null)" != "" ]; then | |
echo "Removing existing Docker image $image_name" | |
docker rmi $image_name | |
fi | |
echo "Building Docker image $image_name" | |
docker build -t $image_name . | |
# Check if the Docker container exists and remove it | |
if [ "$(docker ps -aq -f name=$container_name 2> /dev/null)" != "" ]; then | |
echo "Removing existing Docker container $container_name" | |
docker rm -f $container_name | |
fi | |
echo "Running Docker container $container_name" | |
docker run -d --name $container_name -p $port_mapping -v $volume_mapping --user $(id -u):$(id -g) $image_name | |
} | |
# Loop through the Docker Compose files and run the desired command | |
for file in "${compose_files[@]}" | |
do | |
run_docker_compose "$file" || { echo "Deployment failed for $file"; exit 1; } | |
done | |
# Build and run Docker image | |
build_and_run_docker | |
echo "Deployment finished successfully!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment