Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: support with_copy_to#976

Merged
alexanderankin merged 2 commits into
testcontainers:main from
guenhter:with-copy-to
Apr 29, 2026
Merged

feat: support with_copy_to #976
alexanderankin merged 2 commits into
testcontainers:main from
guenhter:with-copy-to

Conversation

@guenhter

@guenhter guenhter commented Mar 4, 2026
edited
Loading

Copy link
Copy Markdown
Contributor

This PR supports copying files into the container before startup time:

DockerContainer("alpine")
 .with_command(["cat", "/tmp/copied.txt"])
 .with_copy_into_container(src, "/tmp/copied.txt")

To support this, I had to change how the container is started: run is not longer used, but instead create and start are used now, in order to be able to make the copy before the container is actually running.

Inspired by testcontainers/testcontainers-rs#730 where I did exactly the same feature for the rust implementation of testcontainers :)


No tests are failing

> uv run pytest -v core/tests
...
======================================= 316 passed, 1 skipped in 160.87s (0:02:40) ========================================

guenhter commented Mar 4, 2026
edited
Loading

Copy link
Copy Markdown
Contributor Author

Solves #665

Copy link
Copy Markdown
Contributor

I think this is the same use case as #852

guenhter commented Mar 11, 2026
edited
Loading

Copy link
Copy Markdown
Contributor Author

Oh yeah. Look at that. You are absolutely right. I haven't seen yours.
I guess the only difference is, that mine is copying the files before the container gets started, to make files available at start time, isn't it?

Comment thread core/testcontainers/core/docker_client.py Outdated

Copy link
Copy Markdown
Member

still needed after #852 ?

guenhter commented Apr 1, 2026

Copy link
Copy Markdown
Contributor Author

I'll rebase and bring in the modified startup handling in order to be able to copy in files before the container is started.

Copy link
Copy Markdown
Contributor

Hi, just a quick checkup, is this still relevant now that we have container.copy_into_container(my_file, "/tmp/my_file.txt") ready?

guenhter commented Apr 2, 2026
edited
Loading

Copy link
Copy Markdown
Contributor Author

@Tranquility2

At the moment, the following tests is not possible:

def test_copy_into_startup_file(transferable: Transferable):
 destination_in_container = "/tmp/my_file"
 container = DockerContainer("bash", command=f"cat {destination_in_container}")
 container.with_copy_into_container(transferable, destination_in_container)
 with container:
 exit_code = container._container.wait()
 stdout, _ = container.get_logs()
 assert exit_code["StatusCode"] == 0
 assert stdout.decode() == "hello world"

This is what I ment with

I guess the only difference is, that mine is copying the files before the container gets started, to make files available at start time, isn't it?

Who it works now: when the container is started, the file is copied into the started container.
How is should be: the container should be created, then the file is copied into and then the container gets started having all the files available at start time.

This is often crucial like when you want to copy in a config file which is used by the entrypoint script or even more: if you copy in the actual entrypoint script the container should start with.

Tranquility2 reacted with thumbs up emoji

guenhter commented Apr 2, 2026

Copy link
Copy Markdown
Contributor Author

@rhoban13 I've updated this PR based on your changes. Maybe you can have a quick glimpse if it seems ok for you.

rhoban13 commented Apr 2, 2026

Copy link
Copy Markdown
Contributor

Ah - I see the use case, I suspect this will be a much larger piece of work.

You're introducing here the notion of createing a container prior to starting it, which I think is a good idea, but feels like a different piece or work. I believe @alexanderankin has talked about the need do this, but quick glance I'm not seeing any issues tracking that work.

2 immediate workarounds I can think of would be:

  1. Copy the files into an image and pass that image into the Container
  2. Mount instead of copying

Copy link
Copy Markdown
Member

imo its ok if we start implementing create before start here (this is not a soc2 workplace where each PR needs an approved ticket), but the wait method idk if we want that at all, and image pulling would probably need a follow up to account for things like private mirrors, image pull policy (never, ifabsent, always), etc. other than that it looks ok, right?

Copy link
Copy Markdown
Member

ok so lets centralize the discussion about creating before starting #997 because maybe there are some nuances to discuss although im not sure what they are, my approach was going to be to release some release candidates before the next minor version to test this stuff out

rhoban13 and Tranquility2 reacted with thumbs up emoji

codecov Bot commented Apr 29, 2026
edited
Loading

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.33333% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.97%. Comparing base (23aca0b) to head (6659ff5).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
core/testcontainers/core/container.py 77.77% 1 Missing and 1 partial ⚠️
core/testcontainers/core/docker_client.py 86.66% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@ Coverage Diff @@
## main #976 +/- ##
==========================================
- Coverage 83.05% 82.97% -0.09% 
==========================================
 Files 16 16 
 Lines 1700 1721 +21 
 Branches 186 189 +3 
==========================================
+ Hits 1412 1428 +16 
- Misses 234 236 +2 
- Partials 54 57 +3 

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@alexanderankin alexanderankin merged commit 59ec1ce into testcontainers:main Apr 29, 2026
7 of 8 checks passed
alexanderankin added a commit that referenced this pull request Apr 30, 2026
🤖 I have created a release *beep* *boop*
---
##
[4.15.0-rc2](testcontainers-v4.15.0-rc.1...testcontainers-v4.15.0-rc2)
(2026年04月30日)
### Features
* **core:** support TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX
([#961](#961))
([be9a0a6](be9a0a6))
* **mongodb:** Add Atlas Local for MongoDb
([#873](#873))
([73aeb43](73aeb43))
* support with_copy_to
([#976](#976))
([59ec1ce](59ec1ce))
* **valkey:** add Valkey module
([#947](#947))
([fc09dc1](fc09dc1))
### Bug Fixes
* **azurite:** use `HttpWaitStrategy` instead of deprecated
`wait_container_is_ready`
([#1003](#1003))
([9fe6b07](9fe6b07)),
closes
[#874](#874)
* fix pr
[#961](#961)
([#1011](#1011))
([8eff908](8eff908))
---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
Co-authored-by: David Ankin <daveankin@gmail.com>
@guenhter guenhter deleted the with-copy-to branch April 30, 2026 04:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /