Skip to content

Pause container #785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
18 changes: 18 additions & 0 deletions testcontainers/src/core/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ pub enum ClientError {
StartContainer(BollardError),
#[error("failed to stop a container: {0}")]
StopContainer(BollardError),
#[error("failed to pause a container: {0}")]
PauseContainer(BollardError),
#[error("failed to unpause/resume a container: {0}")]
UnpauseContainer(BollardError),
#[error("failed to inspect a container: {0}")]
InspectContainer(BollardError),

Expand Down Expand Up @@ -176,6 +180,20 @@ impl Client {
.map_err(ClientError::Init)
}

pub(crate) async fn pause(&self, id: &str) -> Result<(), ClientError> {
self.bollard
.pause_container(id)
.await
.map_err(ClientError::PauseContainer)
}

pub(crate) async fn unpause(&self, id: &str) -> Result<(), ClientError> {
self.bollard
.unpause_container(id)
.await
.map_err(ClientError::UnpauseContainer)
}

pub(crate) async fn exec(
&self,
container_id: &str,
Expand Down
14 changes: 14 additions & 0 deletions testcontainers/src/core/containers/async_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,20 @@ where
Ok(())
}

/// Pause the container.
/// [Docker Engine API](https://docs.docker.com/reference/api/engine/version/v1.48/#tag/Container/operation/ContainerPause)
pub async fn pause(&self) -> Result<()> {
self.docker_client.pause(&self.id).await?;
Ok(())
}

/// Resume/Unpause the container.
/// [Docker Engine API](https://docs.docker.com/reference/api/engine/version/v1.48/#tag/Container/operation/ContainerUnpause)
pub async fn unpause(&self) -> Result<()> {
self.docker_client.unpause(&self.id).await?;
Ok(())
}

/// Removes the container.
pub async fn rm(mut self) -> Result<()> {
log::debug!("Deleting docker container {}", self.id);
Expand Down
12 changes: 12 additions & 0 deletions testcontainers/src/core/containers/sync_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ where
self.rt().block_on(self.async_impl().start())
}

/// Pause the container.
/// [Docker Engine API](https://docs.docker.com/reference/api/engine/version/v1.48/#tag/Container/operation/ContainerPause)
pub async fn pause(&self) -> Result<()> {
self.rt().block_on(self.async_impl().pause())
}

/// Resume/Unpause the container.
/// [Docker Engine API](https://docs.docker.com/reference/api/engine/version/v1.48/#tag/Container/operation/ContainerUnpause)
pub async fn unpause(&self) -> Result<()> {
self.rt().block_on(self.async_impl().unpause())
}

/// Removes the container.
pub fn rm(mut self) -> Result<()> {
if let Some(active) = self.inner.take() {
Expand Down