Merged PR 52: Update for make sure local devbox image pulled from dockerhub

Update for make sure local devbox image pulled from dockerhub
This commit is contained in:
Tianyong Qiu 2025-03-23 14:54:24 +00:00
commit 11dcf9ae71

View File

@ -1624,6 +1624,8 @@ devbox_init_command() {
fi
fi
# Check if force init is set, if not, check if the ports are in use
if [[ -z "$FORCE_INIT" ]]; then
log_info "Checking if the ports are in use..."
# Check if the gittea, mongodb, rabbitmq, redis ports are in use
if netstat -tuln | grep -q ":3000"; then
@ -1645,8 +1647,9 @@ devbox_init_command() {
if netstat -tuln | grep -q ":6379"; then
exit_with_message " redis port 6379 is already in use, please stop the service." 1
fi
fi
log_info "Checking if the ports are in use... Done."
local devbox_full_image="${DEVBOX_REPO}/${DEVBOX_IMAGE}:${DEVBOX_TAG}"
@ -1654,6 +1657,57 @@ devbox_init_command() {
if [[ -n "$DEVBOX_REPO" && -n "$DEVBOX_IMAGE" && -n "$DEVBOX_TAG" ]]; then
if docker images --format '{{.Repository}}:{{.Tag}}' | grep -q "^${DEVBOX_REPO}/${DEVBOX_IMAGE}:${DEVBOX_TAG}\$"; then
log_info "DevBox image $devbox_full_image already exists."
# Check if the local image is not used by any container
local devbox_full_image="${DEVBOX_REPO}/${DEVBOX_IMAGE}:${DEVBOX_TAG}"
local local_image_id remote_image_id
# Get the local image ID
log_info "DevBox image $devbox_full_image already exists."
local_image_id=$(docker images --format '{{.ID}}' | grep "^${devbox_full_image}\$")
remote_image_id=$(docker images --format '{{.ID}}' "$devbox_full_image" | head -n 1)
if [[ "$local_image_id" != "$remote_image_id" ]]; then
timestamp=$(date +%Y%m%d%H%M%S)
local backup_tag="${DEVBOX_TAG}-$timestamp"
if docker tag "$local_image_id" "${DEVBOX_REPO}/${DEVBOX_IMAGE}:${backup_tag}"; then
log_info "Backup local image $local_image_id to ${DEVBOX_REPO}/${DEVBOX_IMAGE}:${backup_tag}"
else
log_warn " Failed to backup local image $local_image_id to ${DEVBOX_REPO}/${DEVBOX_IMAGE}:${backup_tag}"
fi
# Check if the local image is not used by any container
if docker ps -a --format '{{.Image}}' | grep -q "^${local_image_id}\$"; then
log_info "Local image $local_image_id is used by a container. Stopping it first..."
docker ps -a --filter "ancestor=$local_image_id" --format '{{.ID}}' | while read -r container_id; do
docker stop "$container_id" &>/dev/null || true
docker rm "$container_id" &>/dev/null || true
done
else
# Delete local image by image id
if docker rmi "$local_image_id" &>/dev/null; then
log_info "Deleted local image $local_image_id"
else
log_warn " Failed to delete local image $local_image_id"
fi
fi
# Delete local image by image id
if docker rmi "$local_image_id" &>/dev/null; then
log_info "Deleted local image $local_image_id"
else
log_warn " Failed to delete local image $local_image_id"
fi
# Pull the latest image from the remote repository
log_info "Pulling DevBox image $devbox_full_image..."
docker pull "$devbox_full_image" || {
exit_with_message " Failed to pull DevBox image $devbox_full_image, please check the image name and tag." 1
}
else
log_info "The correct version of devbox image exists and use local image."
fi
else
log_info "Pulling DevBox image $devbox_full_image..."
docker pull "$devbox_full_image"
@ -1921,10 +1975,19 @@ if [[ $USE_LOCAL_COMPONENT_VAL == true ]]; then
mkdir -p "${WORKING_HOME}/logs/${component}"
fi
done
# Check if FORCE_INIT is set, if not just docker compose up or docker-compose up --force
# Start Gitea, MongoDB, RabbitMQ and other components containers
log_info "start Gitea, MongoDB, RabbitMQ and other components containers"
if [[ -z "$FORCE_INIT" ]]; then
log_info "Starting Gitea, MongoDB, RabbitMQ and other components containers..."
$DC_CMD -f docker-compose.yaml up -d mongodb rabbitmq gitea redis "${start_components[@]}"
else
log_info "Force starting Gitea, MongoDB, RabbitMQ and other components containers..."
$DC_CMD -f docker-compose.yaml up --force-recreate -d mongodb rabbitmq gitea redis "${start_components[@]}"
fi
gitea_container_id=$(docker ps --no-trunc -a --filter "name=^freeleaps2-gitea$" --format "{{.ID}}")
echo "$gitea_container_id" > "$WORKING_HOME/.gitea-instance"
@ -1958,7 +2021,13 @@ else
log_info 'Using online components for Freeleaps services.'
echo '============================================================'
# Start Gitea, MongoDB, RabbitMQ containers
$DC_CMD -f docker-compose.yaml up -d mongodb rabbitmq redis
if [[ -z "$FORCE_INIT" ]]; then
log_info "Starting Gitea, MongoDB, RabbitMQ and other components containers..."
$DC_CMD -f docker-compose.yaml up -d gitea mongodb rabbitmq redis
else
log_info "Force starting Gitea, MongoDB, RabbitMQ and other components containers..."
$DC_CMD -f docker-compose.yaml up --force-recreate -d gitea mongodb rabbitmq redis
fi
# Save MongoDB and RabbitMQ container ids to .mongodb-instance and .rabbitmq-instance
mongo_container_id=$(docker ps -a --format '{{.Names}}' | grep "^freeleaps2-mongodb\$")