pub struct DockerClient { /* private fields */ }Expand description
Client for executing Docker CLI commands
This client wraps Docker CLI operations using our CommandExecutor collaborator,
enabling testability and consistency with other external tool clients (Ansible,
OpenTofu, LXD). Each Docker subcommand is exposed as a separate method.
§Architecture
The client uses CommandExecutor as a collaborator for actual command execution,
following the same pattern as AnsibleClient, TofuClient, and LxdClient.
§Example
use torrust_tracker_deployer_lib::adapters::docker::DockerClient;
let docker = DockerClient::new();
// Build an image
docker.build_image("docker/app", "my-app", "latest")?;
// Check if it exists
let exists = docker.image_exists("my-app", "latest")?;
assert!(exists);Implementations§
Source§impl DockerClient
impl DockerClient
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new Docker client
§Example
use torrust_tracker_deployer_lib::adapters::docker::DockerClient;
let docker = DockerClient::new();Sourcepub fn build_image<P: AsRef<Path>>(
&self,
dockerfile_dir: P,
image_name: &str,
image_tag: &str,
) -> Result<String, DockerError>
pub fn build_image<P: AsRef<Path>>( &self, dockerfile_dir: P, image_name: &str, image_tag: &str, ) -> Result<String, DockerError>
Build a Docker image from a Dockerfile directory
Executes docker build -t <name>:<tag> <path> to build an image.
§Arguments
dockerfile_dir- Path to directory containing the Dockerfileimage_name- Name for the Docker image (e.g., “my-ssh-server”)image_tag- Tag for the image (e.g., “latest”)
§Returns
The build output on success
§Errors
Returns DockerError::BuildFailed if the build command fails
§Example
docker.build_image("docker/ssh-server", "my-ssh", "latest")?;Sourcepub fn list_images(
&self,
repository: Option<&str>,
) -> Result<Vec<String>, DockerError>
pub fn list_images( &self, repository: Option<&str>, ) -> Result<Vec<String>, DockerError>
List Docker images with optional repository filter
Executes docker images with formatting to get structured output.
§Arguments
repository- Optional repository name to filter by
§Returns
A vector of image information strings in format: “repository:tag|id|size”
§Errors
Returns DockerError::ListImagesFailed if the command fails
§Example
// List all images
let all_images = docker.list_images(None)?;
// List specific repository
let ubuntu_images = docker.list_images(Some("ubuntu"))?;Sourcepub fn list_containers(&self, all: bool) -> Result<Vec<String>, DockerError>
pub fn list_containers(&self, all: bool) -> Result<Vec<String>, DockerError>
List Docker containers
Executes docker ps with formatting to get structured output.
§Arguments
all- If true, shows all containers (including stopped ones)
§Returns
A vector of container information strings in format: “id|name|status”
§Errors
Returns DockerError::ListContainersFailed if the command fails
§Example
// List only running containers
let running = docker.list_containers(false)?;
// List all containers
let all_containers = docker.list_containers(true)?;Sourcepub fn get_container_logs(
&self,
container_id: &str,
) -> Result<String, DockerError>
pub fn get_container_logs( &self, container_id: &str, ) -> Result<String, DockerError>
Get logs from a Docker container
Executes docker logs <container-id> to retrieve container logs.
§Arguments
container_id- ID or name of the container
§Returns
The container’s logs as a string
§Errors
Returns DockerError::GetLogsFailed if the command fails
§Example
let logs = docker.get_container_logs("my-container")?;
println!("Container logs:\n{}", logs);Sourcepub fn image_exists(
&self,
image_name: &str,
image_tag: &str,
) -> Result<bool, DockerError>
pub fn image_exists( &self, image_name: &str, image_tag: &str, ) -> Result<bool, DockerError>
Check if a Docker image exists locally
Uses list_images to check for the presence of a specific image.
§Arguments
image_name- Name of the imageimage_tag- Tag of the image
§Returns
true if the image exists, false otherwise
§Errors
Returns DockerError::ListImagesFailed if the command fails
§Example
if docker.image_exists("ubuntu", "latest")? {
println!("Ubuntu image is available");
}Trait Implementations§
Source§impl Debug for DockerClient
impl Debug for DockerClient
Auto Trait Implementations§
impl Freeze for DockerClient
impl RefUnwindSafe for DockerClient
impl Send for DockerClient
impl Sync for DockerClient
impl Unpin for DockerClient
impl UnsafeUnpin for DockerClient
impl UnwindSafe for DockerClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request