fussybeaver / bollard

Docker daemon API in Rust
Apache License 2.0
863 stars 131 forks source link

Create container with a specified ip, skip assigned ip #318

Closed groche97 closed 1 year ago

groche97 commented 1 year ago

I'm trying to create a container with a specified ip, but apparently the configuration does not apply

` let docker = match Docker::connect_with_local_defaults() { Ok(d) => d, Err(e) => panic!("error:{}",e.to_string()), };

let options = Some(CreateContainerOptions{
    name: "my-new-container",
    platform: None,
});
let endpoint = EndpointSettings {
    ip_address: Some(String::from("172.17.0.5"));,
    ..Default::default()
};
let mut hash = HashMap::new();
hash.insert("bridge", endpoint);
let net = NetworkingConfig {
    endpoints_config: hash
};
let config = Config {
    image: Some("my_image"),
    networking_config: Some(net),
    ..Default::default()
};

match docker.create_container(options, config).await{
    Ok(r) => println!("work1"),
    Err(e) => println!("error1:{}",e.to_string()),
}
match docker.start_container("my-new-container", None::<StartContainerOptions<String>>).await {
    Ok(r) => println!("work2"),
    Err(e) => println!("error2:{}",e.to_string()),
};

This is my code and run perfectly, but not use the specified ip int he new container

groche97 commented 1 year ago

Sorry I'm not using the correct EndpointSettings correctly, works with:

let endpoint = EndpointSettings { ipam_config: Some(EndpointIpamConfig{ ipv4_address: ip.clone(), ..Default::default() }), ..Default::default() };

instead of:

let endpoint = EndpointSettings { ip_address: ip.clone(), ..Default::default() };