Skip to content

fix: pass env vars via Docker exec API instead of prepending to command#920

Open
gal432 wants to merge 1 commit intotestcontainers:mainfrom
gal432:fix/containerised-compose-env-vars
Open

fix: pass env vars via Docker exec API instead of prepending to command#920
gal432 wants to merge 1 commit intotestcontainers:mainfrom
gal432:fix/containerised-compose-env-vars

Conversation

@gal432
Copy link

@gal432 gal432 commented Feb 11, 2026

Summary

  • Fixes ContainerisedComposeCli::up() failing with exit code 127 when env vars are passed via with_env(). The root cause was that env vars were prepended as KEY=VALUE strings to the exec command array, and Docker's exec API treats the first element as the binary to execute.
  • Instead of shell-wrapping with sh -c, this fix adds native env_vars support to ExecCommand and propagates it through to bollard's CreateExecOptions.env field, which is the Docker exec API's built-in mechanism for passing environment variables.
  • This also makes ExecCommand::with_env_vars() available as a public API for any user of exec(), not just the compose client.

Changes

File Change
core/image/exec.rs Added env_vars: HashMap<String, String> field and with_env_vars() builder method to ExecCommand
core/containers/async_container/raw.rs Destructure and forward env_vars from ExecCommand to the Docker client
core/client.rs Accept env_vars param in exec(), convert to KEY=VALUE format, pass to CreateExecOptions.env
compose/client/containerised.rs Stop prepending env vars to the command array; use ExecCommand::with_env_vars() instead

Test plan

  • test_compose_with_build_and_down — exercises with_env() on local client
  • test_containerised_client_mode — exercises containerised client path
  • test_with_env_vars — unit test for the new ExecCommand::with_env_vars() API
  • All other compose tests pass (test_local_docker_compose, test_local_client_mode)

Fixes #919

Made with Cursor

ContainerisedComposeCli::up() was prepending env vars as "KEY=VALUE"
strings at the beginning of the exec command array. Docker's exec API
treats the first element as the binary to run, so it tried to execute
"KEY=VALUE" as a command, resulting in exit code 127.

Instead of shell-wrapping, this fix adds env_vars support to
ExecCommand and propagates it through to bollard's CreateExecOptions.env
field, which is the native Docker exec API mechanism for passing
environment variables to an exec process.

Fixes testcontainers#919

Co-authored-by: Cursor <cursoragent@cursor.com>
@netlify
Copy link

netlify bot commented Feb 11, 2026

Deploy Preview for testcontainers-rust ready!

Name Link
🔨 Latest commit 8ba7927
🔍 Latest deploy log https://app.netlify.com/projects/testcontainers-rust/deploys/698c526c49e94a0008a78360
😎 Deploy Preview https://deploy-preview-920--testcontainers-rust.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@gal432 gal432 force-pushed the fix/containerised-compose-env-vars branch from 54238bf to 8ba7927 Compare February 11, 2026 09:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ContainerisedComposeCli::up() breaks when env vars are passed via with_env()

1 participant