aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/lib/docker.sh
blob: 8f841bcc994e2b7eb425ce4e652f092401c59ae8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# shellcheck shell=bash
[[ -n "${BOLT_DOCKER_LOADED:-}" ]] && return
readonly BOLT_DOCKER_LOADED=1

source "$(dirname "${BASH_SOURCE[0]}")/log.sh"

docker_compose_up() {
  local compose_file="$1"
  log_info "Starting services from '$compose_file'..."
  docker compose --file "$compose_file" up --detach
}

docker_compose_down() {
  local compose_file="$1"
  log_info "Stopping services from '$compose_file'..."
  docker compose --file "$compose_file" down
}

docker_wait_for_healthy_container() {
  local container_name="$1"
  local timeout="${2:-60}"
  local elapsed=0

  log_info "Waiting for container '$container_name' to be healthy..."
  while [[ $elapsed -lt $timeout ]]; do
    local status
    status=$(docker inspect -f '{{.State.Health.Status}}' "$container_name" 2>/dev/null || echo "not_found")

    case "$status" in
    "healthy")
      log_success "Container '$container_name' is healthy!"
      return 0
      ;;
    "not_found")
      log_error "Error: Container '$container_name' not found"
      return 1
      ;;
    "unhealthy")
      log_error "Error: Container '$container_name' is unhealthy"
      return 1
      ;;
    *)
      log_progress
      sleep 1
      ((elapsed++))
      ;;
    esac
  done

  log_progress_end
  log_error "Timeout: Container '$container_name' did not become healthy within ${timeout} seconds"
  return 1
}