Skip to content

Commit 39e4724

Browse files
committed
fix: use container ID for challenge container naming suffix
Previously, the suffix was derived from HOSTNAME which could be the VM hostname (e.g. 'computeinstance-e00w82rt7k2bcv7ezf') instead of the Docker container ID. Now the suffix is determined by: 1. VALIDATOR_NAME env var (explicit override) 2. Detected Docker container ID (from cgroup/mountinfo) 3. Short hash of hostname (fallback for non-Docker) This ensures challenge containers have consistent, short names like 'challenge-term-challenge-4133b3431b1c' instead of very long names derived from cloud VM hostnames.
1 parent 2adda1a commit 39e4724

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

crates/challenge-orchestrator/src/docker.rs

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,39 @@ impl DockerClient {
158158
anyhow::bail!("Not running in a Docker container or unable to determine container ID")
159159
}
160160

161+
/// Get a suitable suffix for container naming
162+
/// Priority: VALIDATOR_NAME env var > detected container ID > short hash of hostname
163+
fn get_validator_suffix() -> String {
164+
// 1. Check for explicit VALIDATOR_NAME override
165+
if let Ok(name) = std::env::var("VALIDATOR_NAME") {
166+
if !name.is_empty() {
167+
return name.to_lowercase().replace(['-', ' ', '_'], "");
168+
}
169+
}
170+
171+
// 2. Try to detect container ID (works when running in Docker)
172+
if let Ok(container_id) = Self::get_container_id_static() {
173+
// Container IDs are 12+ hex chars, use first 12
174+
let suffix = if container_id.len() > 12 {
175+
&container_id[..12]
176+
} else {
177+
&container_id
178+
};
179+
return suffix.to_lowercase();
180+
}
181+
182+
// 3. Fall back to short hash of hostname (for non-Docker environments)
183+
let hostname = std::env::var("HOSTNAME")
184+
.unwrap_or_else(|_| format!("{:x}", std::process::id()));
185+
186+
// Create a short hash of the hostname for uniqueness using std hash
187+
use std::collections::hash_map::DefaultHasher;
188+
use std::hash::{Hash, Hasher};
189+
let mut hasher = DefaultHasher::new();
190+
hostname.hash(&mut hasher);
191+
format!("{:012x}", hasher.finish()) // 12 hex chars
192+
}
193+
161194
/// Ensure the Docker network exists
162195
pub async fn ensure_network(&self) -> anyhow::Result<()> {
163196
let networks = self.docker.list_networks::<String>(None).await?;
@@ -384,18 +417,19 @@ impl DockerClient {
384417
// Ensure network exists
385418
self.ensure_network().await?;
386419

387-
// Generate container name with validator identifier for dev mode
388-
// In dev mode with shared Docker socket, each validator needs unique container names
389-
let validator_suffix = std::env::var("VALIDATOR_NAME")
390-
.or_else(|_| std::env::var("HOSTNAME"))
391-
.unwrap_or_else(|_| format!("{:x}", std::process::id()));
420+
// Generate container name with validator identifier
421+
// Use container ID if running in Docker, otherwise fall back to VALIDATOR_NAME or short hostname hash
422+
let validator_suffix = Self::get_validator_suffix();
392423
let container_name = format!(
393424
"challenge-{}-{}",
394425
config.name.to_lowercase().replace(' ', "-"),
395426
validator_suffix
396-
.to_lowercase()
397-
.replace("-", "")
398-
.replace(" ", "")
427+
);
428+
429+
info!(
430+
container_name = %container_name,
431+
validator_suffix = %validator_suffix,
432+
"Generated challenge container name"
399433
);
400434

401435
// Remove existing container if any

0 commit comments

Comments
 (0)