Skip to content
Merged
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
26 changes: 26 additions & 0 deletions docs/features/docker_compose.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,32 @@ compose.up().await?;
- Consistent compose version across environments
- Useful for CI/CD where Docker CLI might not be available

If your compose files use relative paths for bind mounts, set an explicit project directory so
docker compose resolves those paths against the host location:

```rust
use testcontainers::compose::{ContainerisedComposeOptions, DockerCompose};

let options = ContainerisedComposeOptions::new(&["/home/me/app/docker-compose.yml"])
.with_project_directory("/home/me/app");

let mut compose = DockerCompose::with_containerised_client(options).await?;
compose.up().await?;
```

### Auto Client

Tries the local `docker compose` CLI first and falls back to the containerised client:

```rust
use testcontainers::compose::{AutoComposeOptions, DockerCompose};

let options = AutoComposeOptions::new(&["docker-compose.yml"]);

let mut compose = DockerCompose::with_auto_client(options).await?;
compose.up().await?;
```

## Configuration Options

### Environment Variables
Expand Down
6 changes: 3 additions & 3 deletions testcontainers/src/compose/client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{fmt, path::PathBuf};

use crate::compose::error::Result;
use crate::compose::{error::Result, ContainerisedComposeOptions};

pub(super) mod containerised;
pub(super) mod local;
Expand All @@ -15,9 +15,9 @@ impl ComposeClient {
ComposeClient::Local(local::LocalComposeCli::new(compose_files))
}

pub(super) async fn new_containerised(compose_files: Vec<PathBuf>) -> Result<Self> {
pub(super) async fn new_containerised(options: ContainerisedComposeOptions) -> Result<Self> {
Ok(ComposeClient::Containerised(Box::new(
containerised::ContainerisedComposeCli::new(compose_files).await?,
containerised::ContainerisedComposeCli::new(options).await?,
)))
}
}
Expand Down
50 changes: 29 additions & 21 deletions testcontainers/src/compose/client/containerised.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::path::PathBuf;

use crate::{
compose::{
client::{ComposeInterface, DownCommand, UpCommand},
error::Result,
ContainerisedComposeOptions,
},
core::{CmdWaitFor, ExecCommand, Mount},
core::{CmdWaitFor, ExecCommand},
images::docker_cli::DockerCli,
runners::AsyncRunner,
ContainerAsync, ContainerRequest, ImageExt,
Expand All @@ -14,32 +13,33 @@ use crate::{
pub(crate) struct ContainerisedComposeCli {
container: ContainerAsync<DockerCli>,
compose_files_in_container: Vec<String>,
project_directory: Option<String>,
}

impl ContainerisedComposeCli {
pub(super) async fn new(compose_files: Vec<PathBuf>) -> Result<Self> {
pub(super) async fn new(options: ContainerisedComposeOptions) -> Result<Self> {
let (compose_files, project_directory) = options.into_parts();
let mut image = ContainerRequest::from(DockerCli::new("/var/run/docker.sock"));

let compose_files_in_container: Vec<String> = compose_files
.iter()
.enumerate()
.map(|(i, _)| format!("/docker-compose-{i}.yml"))
.collect();
let mounts: Vec<_> = compose_files
.iter()
for (path, file_name) in compose_files
.into_iter()
.zip(compose_files_in_container.iter())
.filter_map(|(path, file_name)| path.to_str().map(|p| Mount::bind_mount(p, file_name)))
.collect();

for mount in mounts {
image = image.with_mount(mount);
{
image = image.with_copy_to(file_name, path);
}

let container = image.start().await?;
let project_directory = project_directory.map(|path| path.to_string_lossy().into_owned());

Ok(Self {
container,
compose_files_in_container,
project_directory,
})
}
}
Expand All @@ -52,12 +52,15 @@ impl ComposeInterface for ContainerisedComposeCli {
cmd_parts.push(format!("{}={}", key, value));
}

cmd_parts.extend([
"docker".to_string(),
"compose".to_string(),
"--project-name".to_string(),
command.project_name.clone(),
]);
cmd_parts.extend(["docker".to_string(), "compose".to_string()]);

if let Some(project_directory) = &self.project_directory {
cmd_parts.push("--project-directory".to_string());
cmd_parts.push(project_directory.clone());
}

cmd_parts.push("--project-name".to_string());
cmd_parts.push(command.project_name.clone());

for file in &self.compose_files_in_container {
cmd_parts.push("-f".to_string());
Expand Down Expand Up @@ -87,13 +90,18 @@ impl ComposeInterface for ContainerisedComposeCli {
}

async fn down(&self, command: DownCommand) -> Result<()> {
let mut cmd = vec![
"docker".to_string(),
"compose".to_string(),
let mut cmd = vec!["docker".to_string(), "compose".to_string()];

if let Some(project_directory) = &self.project_directory {
cmd.push("--project-directory".to_string());
cmd.push(project_directory.clone());
}

cmd.extend([
"--project-name".to_string(),
command.project_name.clone(),
"down".to_string(),
];
]);

if command.volumes {
cmd.push("--volumes".to_string());
Expand Down
Loading
Loading