Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions testcontainers/src/core/containers/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct ContainerRequest<I: Image> {
pub(crate) image_tag: Option<String>,
pub(crate) container_name: Option<String>,
pub(crate) network: Option<String>,
pub(crate) hostname: Option<String>,
pub(crate) labels: BTreeMap<String, String>,
pub(crate) env_vars: BTreeMap<String, String>,
pub(crate) hosts: BTreeMap<String, Host>,
Expand Down Expand Up @@ -217,6 +218,10 @@ impl<I: Image> ContainerRequest<I> {
self.readonly_rootfs
}

pub fn hostname(&self) -> Option<&str> {
self.hostname.as_deref()
}

/// Returns the custom health check configuration for the container.
pub fn health_check(&self) -> Option<&Healthcheck> {
self.health_check.as_ref()
Expand All @@ -237,6 +242,7 @@ impl<I: Image> From<I> for ContainerRequest<I> {
image_tag: None,
container_name: None,
network: None,
hostname: None,
labels: BTreeMap::default(),
env_vars: BTreeMap::default(),
hosts: BTreeMap::default(),
Expand Down Expand Up @@ -293,6 +299,7 @@ impl<I: Image + Debug> Debug for ContainerRequest<I> {
.field("image_tag", &self.image_tag)
.field("container_name", &self.container_name)
.field("network", &self.network)
.field("hostname", &self.hostname)
.field("labels", &self.labels)
.field("env_vars", &self.env_vars)
.field("hosts", &self.hosts)
Expand Down
9 changes: 9 additions & 0 deletions testcontainers/src/core/image/image_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ pub trait ImageExt<I: Image> {
/// Adds a host to the container.
fn with_host(self, key: impl Into<String>, value: impl Into<Host>) -> ContainerRequest<I>;

/// Configures hostname for the container.
fn with_hostname(self, hostname: impl Into<String>) -> ContainerRequest<I>;

/// Adds a mount to the container.
fn with_mount(self, mount: impl Into<Mount>) -> ContainerRequest<I>;

Expand Down Expand Up @@ -319,6 +322,12 @@ impl<RI: Into<ContainerRequest<I>>, I: Image> ImageExt<I> for RI {
container_req
}

fn with_hostname(self, hostname: impl Into<String>) -> ContainerRequest<I> {
let mut container_req = self.into();
container_req.hostname = Some(hostname.into());
container_req
}

fn with_mount(self, mount: impl Into<Mount>) -> ContainerRequest<I> {
let mut container_req = self.into();
container_req.mounts.push(mount.into());
Expand Down
1 change: 1 addition & 0 deletions testcontainers/src/runners/async_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ where
let mut config = ContainerCreateBody {
image: Some(container_req.descriptor()),
labels: Some(labels),
hostname: container_req.hostname().map(|h| h.to_string()),
host_config: Some(HostConfig {
privileged: Some(container_req.privileged()),
extra_hosts: Some(extra_hosts),
Expand Down
Loading