GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: add copying files to/from container by mgorsk1 · Pull Request #676 · testcontainers/testcontainers-python

Conversation

@mgorsk1

@mgorsk1 mgorsk1 commented Aug 12, 2024

Copy link
Copy Markdown
Contributor

solves #665

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

rhoban13 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@alexanderankin

alexanderankin commented Aug 12, 2024

Copy link
Copy Markdown
Member

i usually prefer using typing.Union[str, os.PathLike] - if you wanna use strings with this as well as paths

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@codecov

codecov Bot commented Aug 12, 2024

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 32.00000% with 17 lines in your changes missing coverage. Please review.

Please upload report for BASE (main@1e43923). Learn more about missing BASE report.

Files Patch % Lines
core/testcontainers/core/container.py 32.00% 16 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #676   +/-   ##
=======================================
  Coverage        ?   76.44%           
=======================================
  Files           ?       12           
  Lines           ?      624           
  Branches        ?       96           
=======================================
  Hits            ?      477           
  Misses          ?      120           
  Partials        ?       27           

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@alexanderankin

alexanderankin commented Aug 12, 2024

Copy link
Copy Markdown
Member

one other thing to consider is that there is this great interface "Transferable" in other language implementations-- does this PR make it harder or easier to add this to this implementation as well.

the interface (found here) is basically:

class Transferable(abc.ABC):
  @abstractmethod
  def transfer_to(content_stream, destination: typing.Union[str, os.PathLike]):
    ...

where content_stream is some bytesio or something pythonic for a place where you can write bytes to.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@alexanderankin

alexanderankin commented Aug 12, 2024

Copy link
Copy Markdown
Member

when i tried to do it, i accidentally interpreted the interface a bit too widely and instead of passing just the content output stream, i passed the whole container as that first argument.

4b7db74 - misc functions, next commit doesnt make sense without reading first

bbc44ec - the commit where i add the transferable stuff

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@alexanderankin

alexanderankin commented Aug 12, 2024

Copy link
Copy Markdown
Member

so im tiny bit hesitant to merge without a plan for adding transferable on top of this

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mgorsk1

mgorsk1 commented Aug 12, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

do we need it to be 1:1 with java implementation though? any binary object now can be passed here using tempfile.NamedTemporaryObject:

with tempfile.NamedTemporaryObject() as f:

    f.write(binary_data)
    DockerContainer("nginx").with_copy_file_to_container(f.name, "/tmp/target.data").start()
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@alexanderankin

alexanderankin commented Aug 12, 2024

Copy link
Copy Markdown
Member

we dont need to be 1-1 but what if instead of a file you had a string? i think it is beneficial to not force people to create temporary files.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mgorsk1

mgorsk1 commented Aug 12, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

refactored for Transferable although creating temp files cannot be avoided, now instead of asking users for 3 extra lines we add some complexity to codebase here. working example

input:

from pathlib import Path

from testcontainers.core.container import DockerContainer, Transferable
from testcontainers.core.network import Network

c = DockerContainer('nginx')

network = Network()

c \
    .with_copy_file_to_container(Transferable(b'asd', Path('/tmp/mariusz.md1'))) \
    .with_copy_file_to_container(Transferable(Path('/Users/mariusz/Documents/Programming/testcontainers-python/README.md'),Path('/tmp/mariusz.md2'))) \
    .start()

c2 = DockerContainer('nginx')

print(c._container.exec_run('head /tmp/mariusz.md1'))
print(c._container.exec_run('head /tmp/mariusz.md2'))

output

ExecResult(exit_code=0, output=b'asd')
ExecResult(exit_code=0, output=b'[![Poetry](https://img.shields.io/endpoint?url=https://python-poetry.org/badge/v0.json)](https://python-poetry.org/)\n[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)\n![PyPI - Version](https://img.shields.io/pypi/v/testcontainers)\n[![PyPI - License](https://img.shields.io/pypi/l/testcontainers.svg)](https://github.com/testcontainers/testcontainers-python/blob/main/LICENSE)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/testcontainers.svg)](https://pypi.python.org/pypi/testcontainers)\n[![codecov](https://codecov.io/gh/testcontainers/testcontainers-python/branch/master/graph/badge.svg)](https://codecov.io/gh/testcontainers/testcontainers-python)\n![Core Tests](https://github.com/testcontainers/testcontainers-python/actions/workflows/ci-core.yml/badge.svg)\n![Community Tests](https://github.com/testcontainers/testcontainers-python/actions/workflows/ci-community.yml/badge.svg)\n[![Docs](https://readthedocs.org/projects/testcontainers-python/badge/?version=latest)](http://testcontainers-python.readthedocs.io/en/latest/?badge=latest)\n\n')
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Lenormju

Lenormju commented Aug 12, 2024

Copy link
Copy Markdown

@mgorsk1 I have opened #677 to solve the same issue (I did not see you already had opened one 😓).
Yours have the Transferable feature. But I have the read_only feature.
And they are not implemented the same way :

  • I am passing the paths in the HostConfig.Bind to the docker_client.create (so the file exist before the container starts)
  • You are writing the files using the docker.models.containers.Container.put_archive method (which works after the container has been started)

Are our two approaches complementary ?
Reading again the Java documentation for copying files, there does not seem to be a difference, at the API level at least, between copying to a container before or after its startup.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mgorsk1

mgorsk1 commented Aug 13, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

I think copying before can already be solved by with_volume_mapping 😅

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

mgorsk1 added 7 commits August 13, 2024 13:07
@mgorsk1
add copying files to/from container
c08ae6c
Signed-off-by: mgorsk1 <[email protected]>
@mgorsk1
rename vars
8ed3df0
Signed-off-by: mgorsk1 <[email protected]>
@mgorsk1
fix typing
e7b7990
Signed-off-by: mgorsk1 <[email protected]>
@mgorsk1
make linter happy
3040d2b
Signed-off-by: mgorsk1 <[email protected]>
@mgorsk1
unify args using Path
acd19f5
Signed-off-by: mgorsk1 <[email protected]>
@mgorsk1
refactor for transferable
40fd110
Signed-off-by: mgorsk1 <[email protected]>
@mgorsk1
add tests
a2f6094
Signed-off-by: mgorsk1 <[email protected]>
@mgorsk1
mgorsk1 force-pushed the feat/665-with-copy-file-to-container branch from e5a2325 to a2f6094 Compare August 13, 2024 11:08
mgorsk1 added 2 commits August 13, 2024 13:10
@mgorsk1
remove old network approach
fc40016
Signed-off-by: mgorsk1 <[email protected]>
@mgorsk1
remove tmppath
6f25274
Signed-off-by: mgorsk1 <[email protected]>
@alexanderankin

alexanderankin commented Aug 13, 2024

Copy link
Copy Markdown
Member

committed some code here - main...feat/665-with-copy-file-to-container

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@alexanderankin

alexanderankin commented Aug 13, 2024

Copy link
Copy Markdown
Member

rebased here https://github.com/testcontainers/testcontainers-python/compare/feat/665-with-copy-file-to-container_rebased

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@alexanderankin

alexanderankin commented Aug 13, 2024

Copy link
Copy Markdown
Member

since this is not a high priority feature this can probably wait for 1) more api refinement - feedback welcomed on my suggestions above 2) more of my free time, as I'm not sure ill be able to spend too much more time on this this week (just spent my entire week's time budget on this today).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@alexanderankin alexanderankin added the 👀 intend to merge this or some version of it label Nov 11, 2024
anuraaga
anuraaga reviewed Jan 30, 2025
View reviewed changes
Comment thread core/testcontainers/core/container.py

logger.info("Container started: %s", self._container.short_id)

for transferable in self._files:

@anuraaga anuraaga Jan 30, 2025

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found this PR noticing that copying files isn't supported, a bit surprising since it's a pretty important one and found in at least many of the other languages.

I noticed this implementation doesn't seem to match others, though this is probably because of how the general docker running is implemented. Generally, the container is created, then files copied, then the container started, so the files are present when the command runs.

https://github.com/testcontainers/testcontainers-java/blob/1404c4429c0cb98fe46534bf33632d25dc5309e4/core/src/main/java/org/testcontainers/containers/GenericContainer.java#L430

Here, the files are just copied after startup so can't be used by the entrypoint - perhaps there's some use case for this but I don't know how common it is. Note that volume mounting is an alternative but doesn't always work due to e.g. permission mode issues - copying is generally more robust.

Given the behavior would be so different from other implementations of testcontainers, I think this PR can cause a lot of confusion so probably needs to rework the lifecycle like that.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Lenormju reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@rhoban13 rhoban13 May 27, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making this happen during start doesn't feel like the right interface here. While there's definitely good reasons to copy in files just after it starts, as proposed here that is the only time we can copy files into the container.

I'd propose instead creating a copy_file_into_container, which would basically be the inverse of copy_file_from_container proposed above. It could be implemented by simply calling get_archive, but provide an interface which used os.PathLike.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@anuraaga anuraaga mentioned this pull request Jan 30, 2025
feat(core): add mount files when starting a container #677
Closed
5 tasks
anuraaga
anuraaga reviewed Jan 30, 2025
View reviewed changes
Comment thread core/testcontainers/core/container.py
data = io.BytesIO()

with transferable as f, tarfile.open(fileobj=data, mode="w") as tar:
tar.add(f.input_path, arcname=f.output_path)

@anuraaga anuraaga Jan 30, 2025

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, the temporary file could be avoided if using tar.addfile - it is somewhat more tedious to set up a TarInfo but maybe worth it to skip the temp file

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@anuraaga anuraaga Jan 30, 2025

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I think all the copy_file APIs in testcontainers accept a file mode. With the current pattern, it can be something like filter=lambda tarinfo: tarinfo.mode = foo, but if switching to addfile it could be set directly to the TarInfo

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@rhoban13 rhoban13 mentioned this pull request Jun 17, 2025
Feature: add DockerContainer.with_copy_file_to_container #665
Closed
@rhoban13 rhoban13 mentioned this pull request Jul 28, 2025
feat: Transferable #852
Merged
alexanderankin pushed a commit that referenced this pull request Apr 1, 2026
@rhoban13 @Tranquility2
feat: Transferable (#852)
6301a0c
In #676 for
copying to/from `DockerContainer`, it was suggested that we should
clarify the interface & usage a bit before proceeding. This PR aims to
push that conversation forward with some test cases illustrating
proposed usages. Although here I created a `Transferable` object, in
most cases there's no need for the caller to explicitly construct a
Transferable, just pass the `bytes | Path`

**Proposed use cases:**

1. Copy into a container by passing a `TransferSpec` into the
initializer:
```
DockerContainer(... transferrables=[TransferSpec(source, destination_in_container), ...)
```

2. Copy into the container via the builder pattern, prior to starting
the container:
```
DockerContainer(...)
     .with_copy_into_container(b"some_bytes", destination_in_container)
     .with_copy_into_container(some_path, destination_in_container)
 ```

3. Copy into the container at runtime:
```
with DockerContainer(...) as container:
container.copy_into_container(b"some_bytes", destination_in_container)
    container.copy_into_container(some_path, destination_in_container)
```

---------

Co-authored-by: Roy Moore <[email protected]>
@Tranquility2 Tranquility2 mentioned this pull request Apr 1, 2026
fix(core): Refactor copy file #996
Merged
Tranquility2 added a commit that referenced this pull request Apr 1, 2026
@Tranquility2
fix(core): Refactor copy file (#996)
0e0bb24
Follow-up to #852, cleaning up the copy file feature after it landed.
(Related to #676)
@Tranquility2

Tranquility2 commented Apr 1, 2026

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?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

2 more reviewers
@anuraaga anuraaga anuraaga left review comments
@rhoban13 rhoban13 rhoban13 left review comments
Reviewers whose approvals may not affect merge requirements

At least 1 approving review is required to merge this pull request.

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

👀 intend to merge this or some version of it

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@mgorsk1 @alexanderankin @Lenormju @Tranquility2 @anuraaga @rhoban13
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.