Skip to content

Commit b314116

Browse files
durable rancher setup
1 parent 5c9b359 commit b314116

2 files changed

Lines changed: 109 additions & 23 deletions

File tree

Gitlab-Templatized/.env.example

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
# These values are for the local Docker-based GitLab instance only.
33
# Copy this file to .env and fill in your values.
44

5+
# ── Fixed IP addresses (on gitlab-net 172.30.0.0/24) ──
6+
# Rancher: 172.30.0.10 → host ports 80 / 443
7+
# GitLab: 172.30.0.2 → host ports 8080 / 8443 / 2222
8+
# GitLab Runner: 172.30.0.3 → no host ports
9+
10+
# ── Rancher ──
11+
RANCHER_BOOTSTRAP_PASSWORD=your_rancher_password
12+
13+
# ── GitLab ──
14+
GITLAB_ROOT_PASSWORD=your_gitlab_root_password
515
GITLAB_RUNNER_REGISTRATION_TOKEN=your_runner_registration_token
616
GITLAB_URL=http://172.30.0.2
717
GITLAB_EXTERNAL_URL=http://localhost:8080

Gitlab-Templatized/startup-services.ps1

Lines changed: 99 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
# Simple startup script for Rancher, Docker, and GitLab
2+
#
3+
# Fixed IP addresses (on gitlab-net 172.30.0.0/24):
4+
# Rancher: 172.30.0.10 → host ports 80 / 443
5+
# GitLab: 172.30.0.2 → host ports 8080 / 8443 / 2222
6+
# GitLab Runner: 172.30.0.3 → no host ports
7+
#
8+
# Passwords are read from .env and injected on first container creation.
9+
# Rancher and GitLab data are stored in named Docker volumes so state
10+
# (including passwords) survives container recreation.
211

312
# Load environment variables from .env file
413
$envFile = Join-Path $PSScriptRoot ".env"
@@ -45,36 +54,88 @@ if (-not (Get-Process "Rancher Desktop" -ErrorAction SilentlyContinue)) {
4554
Write-Host "Docker is ready!" -ForegroundColor Green
4655
}
4756

48-
# Start Rancher
57+
# ---------------------------------------------------------------------------
58+
# Helper: start an existing stopped container, or create a new one.
59+
# Returns $true if the container was already running.
60+
# ---------------------------------------------------------------------------
61+
function Start-OrCreate {
62+
param([string]$Name)
63+
$state = docker inspect --format '{{.State.Running}}' $Name 2>&1
64+
if ($LASTEXITCODE -eq 0) {
65+
if ($state -eq 'true') {
66+
Write-Host "$Name is already running." -ForegroundColor Gray
67+
return $true
68+
}
69+
Write-Host "Starting existing $Name container..." -ForegroundColor Cyan
70+
docker start $Name | Out-Null
71+
return $true
72+
}
73+
return $false # container does not exist — caller will create it
74+
}
75+
76+
# ---------------------------------------------------------------------------
77+
# Rancher (172.30.0.10 — ports 80/443)
78+
# ---------------------------------------------------------------------------
4979
Write-Host "Starting Rancher..." -ForegroundColor Cyan
50-
docker run -d --restart=unless-stopped `
51-
--name rancher `
52-
--network gitlab-net --ip 172.30.0.10 `
53-
-p 80:80 -p 443:443 `
54-
--privileged `
55-
rancher/rancher:latest
80+
if (-not (Start-OrCreate "rancher")) {
81+
$rancherPw = $env:RANCHER_BOOTSTRAP_PASSWORD
82+
$rancherEnv = @()
83+
if ($rancherPw) { $rancherEnv = @("-e", "CATTLE_BOOTSTRAP_PASSWORD=$rancherPw") }
84+
85+
docker run -d --restart=unless-stopped `
86+
--name rancher `
87+
--network gitlab-net --ip 172.30.0.10 `
88+
-p 80:80 -p 443:443 `
89+
--privileged `
90+
-v rancher-data:/var/lib/rancher `
91+
@rancherEnv `
92+
rancher/rancher:latest | Out-Null
93+
if ($rancherPw) {
94+
Write-Host " Rancher bootstrap password set from .env" -ForegroundColor Green
95+
}
96+
}
5697

57-
# Start GitLab
98+
# ---------------------------------------------------------------------------
99+
# GitLab (172.30.0.2 — ports 8080/8443/2222)
100+
# ---------------------------------------------------------------------------
58101
Write-Host "Starting GitLab..." -ForegroundColor Cyan
59-
docker run -d --restart=unless-stopped `
60-
--name gitlab `
61-
--network gitlab-net --ip 172.30.0.2 `
62-
-p 8080:80 -p 8443:443 -p 2222:22 `
63-
-v gitlab-config:/etc/gitlab `
64-
-v gitlab-logs:/var/log/gitlab `
65-
-v gitlab-data:/var/opt/gitlab `
66-
gitlab/gitlab-ce:latest
102+
if (-not (Start-OrCreate "gitlab")) {
103+
$gitlabPw = $env:GITLAB_ROOT_PASSWORD
104+
$gitlabUrl = $env:GITLAB_EXTERNAL_URL
105+
$gitlabEnv = @()
106+
if ($gitlabPw) { $gitlabEnv += @("-e", "GITLAB_ROOT_PASSWORD=$gitlabPw") }
107+
if ($gitlabUrl) { $gitlabEnv += @("-e", "GITLAB_OMNIBUS_CONFIG=external_url '$gitlabUrl'") }
67108

68-
# Start GitLab Runner
109+
docker run -d --restart=unless-stopped `
110+
--name gitlab `
111+
--network gitlab-net --ip 172.30.0.2 `
112+
-p 8080:80 -p 8443:443 -p 2222:22 `
113+
-v gitlab-config:/etc/gitlab `
114+
-v gitlab-logs:/var/log/gitlab `
115+
-v gitlab-data:/var/opt/gitlab `
116+
@gitlabEnv `
117+
gitlab/gitlab-ce:latest | Out-Null
118+
if ($gitlabPw) {
119+
Write-Host " GitLab root password set from .env" -ForegroundColor Green
120+
}
121+
}
122+
123+
# ---------------------------------------------------------------------------
124+
# GitLab Runner (172.30.0.3)
125+
# ---------------------------------------------------------------------------
69126
Write-Host "Starting GitLab Runner..." -ForegroundColor Cyan
70-
docker run -d --restart=unless-stopped `
71-
--name gitlab-runner `
72-
--network gitlab-net --ip 172.30.0.3 `
73-
-v gitlab-runner-config:/etc/gitlab-runner `
74-
-v //var/run/docker.sock:/var/run/docker.sock `
75-
gitlab/gitlab-runner:latest
127+
if (-not (Start-OrCreate "gitlab-runner")) {
128+
docker run -d --restart=unless-stopped `
129+
--name gitlab-runner `
130+
--network gitlab-net --ip 172.30.0.3 `
131+
-v gitlab-runner-config:/etc/gitlab-runner `
132+
-v //var/run/docker.sock:/var/run/docker.sock `
133+
gitlab/gitlab-runner:latest | Out-Null
134+
}
76135

136+
# ---------------------------------------------------------------------------
77137
# Register runner if not already registered
138+
# ---------------------------------------------------------------------------
78139
$runnerConfig = docker exec gitlab-runner cat /etc/gitlab-runner/config.toml 2>&1
79140
if ($runnerConfig -notmatch '\[\[runners\]\]') {
80141
$token = $env:GITLAB_RUNNER_REGISTRATION_TOKEN
@@ -98,6 +159,21 @@ if ($runnerConfig -notmatch '\[\[runners\]\]') {
98159
}
99160
}
100161

162+
# ---------------------------------------------------------------------------
163+
# Summary
164+
# ---------------------------------------------------------------------------
165+
Write-Host ""
166+
Write-Host "========================================" -ForegroundColor Green
167+
Write-Host " All services started!" -ForegroundColor Green
168+
Write-Host "========================================" -ForegroundColor Green
169+
Write-Host " Rancher: https://localhost (172.30.0.10)" -ForegroundColor White
170+
Write-Host " GitLab: http://localhost:8080 (172.30.0.2)" -ForegroundColor White
171+
Write-Host " Runner: connected on gitlab-net (172.30.0.3)" -ForegroundColor White
172+
Write-Host ""
173+
Write-Host " GitLab user: root" -ForegroundColor White
174+
Write-Host " Passwords: see .env file" -ForegroundColor White
175+
Write-Host "========================================" -ForegroundColor Green
176+
101177
Write-Host ""
102178
Write-Host "Done! Services are starting up..." -ForegroundColor Green
103179
Write-Host ""

0 commit comments

Comments
 (0)