Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Docker Build Script for Maintainability and Error Handling #989

Merged
merged 6 commits into from
Oct 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions Docker/dev/build_images.sh
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@

#!/bin/bash

# Change directory to root Server directory for correct Docker Context
cd "$(dirname "$0")"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Yo, dawg! Let's beef up that directory change, ya feel me?

Aight, so check it. We're switchin' directories like it's all good, but what if it ain't? We gotta handle that potential failure, you know what I'm sayin'?

Let's drop this hot fix:

-cd "$(dirname "$0")"
+cd "$(dirname "$0")" || { echo "Couldn't slide into the script's crib. I'm out!"; exit 1; }

This way, if the CD player skips, we bounce outta here with style, you dig?

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
cd "$(dirname "$0")"
cd "$(dirname "$0")" || { echo "Couldn't slide into the script's crib. I'm out!"; exit 1; }
🧰 Tools
🪛 Shellcheck

[warning] 4-4: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

(SC2164)

cd ../..

#Client
client="./Docker/dev/client.Dockerfile"

# MongoDB
mongoDB="./Docker/dev/mongoDB.Dockerfile"

# Redis
redis="./Docker/dev/redis.Dockerfile"

# Server
server="./Docker/dev/server.Dockerfile"

docker build -f $client -t uptime_client .
docker build -f $mongoDB -t uptime_database_mongo .
docker build -f $redis -t uptime_redis .
docker build -f $server -t uptime_server .

echo "All images built"
# Define an array of services and their Dockerfiles
declare -A services=(
["uptime_client"]="./Docker/dev/client.Dockerfile"
["uptime_database_mongo"]="./Docker/dev/mongoDB.Dockerfile"
["uptime_redis"]="./Docker/dev/redis.Dockerfile"
["uptime_server"]="./Docker/dev/server.Dockerfile"
)

# Loop through each service and build the corresponding image
for service in "${!services[@]}"; do
docker build -f "${services[$service]}" -t "$service" .

## Check if the build succeeded
if [ $? -ne 0 ]; then
echo "Error building $service image. Exiting..."
exit 1
fi
done

echo "All images built successfully"