aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/lib/docker.sh
blob: 6488bb0e26dbdd81b08ce4ae11ae7425fde57c18 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# shellcheck shell=bash
[[ -n "${BOLT_DOCKER_LOADED:-}" ]] && return
readonly BOLT_DOCKER_LOADED=1

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

# Start a PostgreSQL database container
#
# ARGUMENTS:
#   $1: Container name (default: bolt_postgresql_db)
#
# ENVIRONMENT:
#   - POSTGRES_DB (default: test_db)
#   - POSTGRES_USER (default: test_user)
#   - POSTGRES_PASSWORD (default: testpassword)
#
docker_start_postgresql() {
  local container_name="${1:-bolt_postgresql_db}"
  local postgres_db="${POSTGRES_DB:-test_db}"
  local postgres_user="${POSTGRES_USER:-test_user}"
  local postgres_password="${POSTGRES_PASSWORD:-testpassword}"

  log_info "Starting PostgreSQL container '$container_name'..."

  docker run --detach \
    --name "$container_name" \
    --publish 5432:5432 \
    --health-cmd "pg_isready -U postgres" \
    --health-interval 10s \
    --health-timeout 5s \
    --health-retries 5 \
    --env POSTGRES_DB="$postgres_db" \
    --env POSTGRES_USER="$postgres_user" \
    --env POSTGRES_PASSWORD="$postgres_password" \
    postgres:16.1-alpine3.19
}

# Start a MySQL database container
#
# ARGUMENTS:
#   $1: Container name (default: bolt_mysql_db)
#
# ENVIRONMENT:
#   - MYSQL_DATABASE (default: test_db)
#   - MYSQL_USER (default: test_user)
#   - MYSQL_PASSWORD (default: testpassword)
#   - MYSQL_ROOT_PASSWORD (default: testpassword)
#
docker_start_mysql() {
  local container_name="${1:-bolt_mysql_db}"
  local mysql_database="${MYSQL_DATABASE:-test_db}"
  local mysql_user="${MYSQL_USER:-test_user}"
  local mysql_password="${MYSQL_PASSWORD:-testpassword}"
  local mysql_root_password="${MYSQL_ROOT_PASSWORD:-testpassword}"

  log_info "Starting MySQL container '$container_name'..."

  docker run --detach \
    --name "$container_name" \
    --publish 3306:3306 \
    --health-cmd "mysqladmin ping -h localhost" \
    --health-interval 10s \
    --health-timeout 5s \
    --health-retries 5 \
    --env MYSQL_DATABASE="$mysql_database" \
    --env MYSQL_USER="$mysql_user" \
    --env MYSQL_PASSWORD="$mysql_password" \
    --env MYSQL_ROOT_PASSWORD="$mysql_root_password" \
    mysql:8.3
}

# Start a MSSQL database container
#
# ARGUMENTS:
#   $1: Container name (default: bolt_mssql_db)
#
# ENVIRONMENT:
#   - MSSQL_SA_PASSWORD (default: TestPass123!)
#
docker_start_mssql() {
  local container_name="${1:-bolt_mssql_db}"
  local mssql_sa_password="${MSSQL_SA_PASSWORD:-TestPass123!}"

  log_info "Starting MSSQL container '$container_name'..."

  docker run --detach \
    --name "$container_name" \
    --publish 1433:1433 \
    --health-cmd "/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P $mssql_sa_password -Q 'SELECT 1' -C || exit 1" \
    --health-interval 10s \
    --health-timeout 3s \
    --health-start-period 10s \
    --health-retries 10 \
    --env ACCEPT_EULA=Y \
    --env MSSQL_SA_PASSWORD="$mssql_sa_password" \
    mcr.microsoft.com/mssql/server:2022-latest
}

# Stop and remove a database container
#
# ARGUMENTS:
#   $1: Container name
#
docker_stop_container() {
  local container_name="$1"

  log_info "Stopping and removing container '$container_name'..."
  docker stop "$container_name" >/dev/null 2>&1 || true
  docker rm "$container_name" >/dev/null 2>&1 || true
}

# Wait for a container to be healthy
#
# ARGUMENTS:
#   $1: Container name
#   $2: Timeout in seconds (default: 60)
#
# RETURN:
#   0 - Container is healthy
#   1 - Container not found, unhealthy, or timeout
#
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"
  log_info "$(docker ps -a)"
  return 1
}