Skip to content

Commit 805e89a

Browse files
committed
Validating port
1 parent 43e70cd commit 805e89a

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ fn get_ruku_config(log: &Logger, repo: &str, server_config: &ServerConfig) -> Ru
189189
std::process::exit(1);
190190
});
191191

192+
// Validate the config
192193
match config.validate() {
193194
Ok(_) => (),
194195
Err(e) => {

src/model.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
use bollard::container::ListContainersOptions;
2+
use bollard::Docker;
3+
use futures::executor::block_on;
14
use port_selector::is_free;
25
use serde::Deserialize;
6+
use std::collections::HashMap;
37
use validator::{Validate, ValidationError};
48

59
#[derive(Debug, Validate, Deserialize)]
@@ -56,8 +60,28 @@ fn validate_providers(providers: &[String]) -> Result<(), ValidationError> {
5660
}
5761

5862
fn validate_port(port: u16) -> Result<(), ValidationError> {
63+
// First check if the port is being used by our own container
64+
if let Ok(docker) = Docker::connect_with_local_defaults() {
65+
let mut filters = HashMap::new();
66+
filters.insert("publish".to_string(), vec![port.to_string()]);
67+
68+
let options = Some(ListContainersOptions {
69+
all: true,
70+
filters,
71+
..Default::default()
72+
});
73+
74+
if let Ok(containers) = block_on(docker.list_containers(options)) {
75+
// If we found containers using this port, it's okay - we'll handle it in the Container::run
76+
if !containers.is_empty() {
77+
return Ok(());
78+
}
79+
}
80+
}
81+
82+
// If we get here, check if the port is free for other applications
5983
if !is_free(port) {
60-
return Err(ValidationError::new("port is already in use"));
84+
return Err(ValidationError::new("port is already in use by another application"));
6185
}
6286
Ok(())
6387
}

0 commit comments

Comments
 (0)