Skip to content
This repository was archived by the owner on Jan 27, 2026. It is now read-only.

Commit 735dd3e

Browse files
authored
imp(worker): host nw mode with ability to switch networking config (#577)
* host nw mode with ability to switch networking config
1 parent 0fe349a commit 735dd3e

3 files changed

Lines changed: 31 additions & 2 deletions

File tree

crates/worker/src/cli/command.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ pub enum Commands {
113113
/// Storage path for worker data (overrides automatic selection)
114114
#[arg(long)]
115115
storage_path: Option<String>,
116+
117+
/// Disable host network mode
118+
#[arg(long, default_value = "false")]
119+
disable_host_network_mode: bool,
116120
},
117121
Check {},
118122

@@ -192,6 +196,7 @@ pub async fn execute_command(
192196
loki_url: _,
193197
log_level: _,
194198
storage_path,
199+
disable_host_network_mode,
195200
} => {
196201
if *disable_state_storing && !(*no_auto_recover) {
197202
Console::user_error(
@@ -449,6 +454,7 @@ pub async fn execute_command(
449454
.address()
450455
.to_string(),
451456
state.get_p2p_seed(),
457+
*disable_host_network_mode,
452458
));
453459

454460
let bridge_cancellation_token = cancellation_token.clone();

crates/worker/src/docker/docker_manager.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ pub struct ContainerDetails {
4646
pub struct DockerManager {
4747
docker: Docker,
4848
storage_path: String,
49+
/// Controls whether to use host network mode for containers.
50+
///
51+
/// Currently defaults to host mode (when false) to work around performance issues
52+
/// with Docker bridge networking on certain cloud providers. This is a trade-off
53+
/// between security isolation and performance.
54+
///
55+
/// TODO: Investigate root cause of bridge network performance degradation and
56+
/// implement a more optimal solution that maintains security isolation.
57+
disable_host_network_mode: bool,
4958
}
5059

5160
impl DockerManager {
@@ -128,7 +137,7 @@ impl DockerManager {
128137
}
129138

130139
/// Create a new DockerManager instance
131-
pub fn new(storage_path: String) -> Result<Self, DockerError> {
140+
pub fn new(storage_path: String, disable_host_network_mode: bool) -> Result<Self, DockerError> {
132141
let docker = match Docker::connect_with_unix_defaults() {
133142
Ok(docker) => docker,
134143
Err(e) => {
@@ -159,6 +168,7 @@ impl DockerManager {
159168
Ok(Self {
160169
docker,
161170
storage_path,
171+
disable_host_network_mode,
162172
})
163173
}
164174

@@ -385,6 +395,12 @@ impl DockerManager {
385395
Some(binds)
386396
};
387397

398+
let network_mode = if self.disable_host_network_mode {
399+
"bridge".to_string()
400+
} else {
401+
"host".to_string()
402+
};
403+
388404
let host_config = if gpu.is_some() {
389405
let gpu = gpu.unwrap();
390406
let device_ids = match &gpu.indices {
@@ -399,6 +415,7 @@ impl DockerManager {
399415
};
400416

401417
Some(HostConfig {
418+
network_mode: Some(network_mode),
402419
extra_hosts: Some(vec!["host.docker.internal:host-gateway".into()]),
403420
device_requests: Some(vec![DeviceRequest {
404421
driver: Some("nvidia".into()),
@@ -417,6 +434,7 @@ impl DockerManager {
417434
})
418435
} else {
419436
Some(HostConfig {
437+
network_mode: Some(network_mode),
420438
extra_hosts: Some(vec!["host.docker.internal:host-gateway".into()]),
421439
binds: volume_binds,
422440
restart_policy: Some(bollard::models::RestartPolicy {

crates/worker/src/docker/service.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const TASK_PREFIX: &str = "prime-task";
3131
const RESTART_INTERVAL_SECONDS: i64 = 10;
3232

3333
impl DockerService {
34+
#[allow(clippy::too_many_arguments)]
3435
pub fn new(
3536
cancellation_token: CancellationToken,
3637
gpu: Option<GpuSpecs>,
@@ -39,8 +40,10 @@ impl DockerService {
3940
storage_path: String,
4041
node_address: String,
4142
p2p_seed: Option<u64>,
43+
disable_host_network_mode: bool,
4244
) -> Self {
43-
let docker_manager = Arc::new(DockerManager::new(storage_path).unwrap());
45+
let docker_manager =
46+
Arc::new(DockerManager::new(storage_path, disable_host_network_mode).unwrap());
4447
Self {
4548
docker_manager,
4649
cancellation_token,
@@ -429,6 +432,7 @@ mod tests {
429432
"/tmp/test-storage".to_string(),
430433
Address::ZERO.to_string(),
431434
None,
435+
false,
432436
);
433437
let task = Task {
434438
image: "ubuntu:latest".to_string(),
@@ -477,6 +481,7 @@ mod tests {
477481
"/tmp/test-storage".to_string(),
478482
Address::ZERO.to_string(),
479483
Some(12345), // p2p_seed for testing
484+
false,
480485
);
481486

482487
// Test command argument replacement

0 commit comments

Comments
 (0)