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 — Feature: add DockerContainer.with_copy_file_to_container · Issue #665 · testcontainers/testcontainers-python

Feature: add DockerContainer.with_copy_file_to_container #665

New issue
Copy link
New issue
Copy link
Closed
Closed
Feature: add DockerContainer.with_copy_file_to_container#665
Copy link
Labels
🚀 enhancement🤝 contributions are welcome

Description

@Lenormju
Lenormju
opened on Aug 5, 2024
Issue body actions

What are you trying to do?

Describe the intention of the enhancement.

I'd like to add a file to the container I'm starting, without using volumes.

Why should it be done this way?

Feature-parity with the Java version : https://java.testcontainers.org/features/files/#copying-files

Other references:

It you deem it interesting to implement, as I do, I can implement it.

Reacted by Jonathan Desrocher, Simon, Victor Litoux, Carlos Juan Gómez Peñalver, Alexander Malyga, Maximilian Reimer, Ryan Hoban and Günther Grill

Activity

  1. Lenormju
    added
    🚀 enhancement
    on Aug 5, 2024
  2. Lenormju
    changed the title [-]Feature: add DockerContainer.with_copy_file_ton_container[/-] [+]Feature: add DockerContainer.with_copy_file_to_container[/+] on Aug 5, 2024
  3. Lenormju commented on Aug 5, 2024

    @Lenormju
    Lenormju
    on Aug 5, 2024
    Author
    More actions

    I have found this remark in a PR : #392 (comment)
    Indeed, this PR was not the right place to do it, but to implement it in a new one now would be perfect 😄

  4. alexanderankin
    added
    🤝 contributions are welcome
    on Aug 5, 2024
  5. anuraaga commented on Jan 30, 2025

    @anuraaga
    anuraaga
    on Jan 30, 2025
    More actions

    In case anyone lands on this and wishes for copying (I use it to pass GCP creds to cloud sql proxy container, volume mount doesn't work on Linux), I use this rough workaround, the key point is to set restart policy on failure to allow it to fail as expected on missing creds file until the copy completes.

    # Roughly based on proposed logic in https://github.com/testcontainers/testcontainers-python/pull/676/files
    def copy_file_to_container(container: DockerContainer, host_path: str, container_path: str, mode: int):
        data = BytesIO()
    
        def set_mode(tarinfo: tarfile.TarInfo):
            tarinfo.mode = mode
            return tarinfo
    
        with tarfile.open(fileobj=data, mode='w') as tar:
            tar.add(host_path, arcname=container_path, filter=set_mode)
    
        data.seek(0)
        if not container._container.put_archive('/', data):
            raise Exception('Failed to copy file to container')
    
    with (
            DockerContainer('gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.14.3')
            .with_command(
                '--address 0.0.0.0 --health-check --http-address 0.0.0.0 foo'
            )
            .with_exposed_ports(5432, 9090)
            .with_env(environment_vars.CREDENTIALS, '/creds/creds.json')
            # Currently there is no way to copy a file before container starts, so we workaround by allowing the container
            # to restart on failure, which will stop when the copy has completed.
            # https://github.com/testcontainers/testcontainers-python/pull/676#discussion_r1934888327
            .with_kwargs(restart_policy={'Name': 'on-failure', 'MaximumRetryCount': 100})
        ) as container:
            copy_file_to_container(container, creds_path, '/creds/creds.json', 0o644)
            wait_for_logs(container, 'The proxy has started successfully and is ready for new connections!')
    
  6. rhoban13 commented on Jun 16, 2025

    @rhoban13
    rhoban13
    on Jun 16, 2025
    Contributor
    More actions

    From @anuraaga comment:

    ... the key point is to set restart policy on failure to allow it to fail as expected on missing creds file until the copy completes.

    The need to restart the container I think is somewhat due to the fact that there is no way to directly create a container without also starting it, since our implementation of start calls docker_client.run. Ideally what you'd like to do here is:

    docker container create
    docker cp
    docker start

    but there's no direct way to do that here. You could potentially directly use the some methods in docker.client.containers to achieve the same as well

    Reacted by Lenormand Julien
  7. alexanderankin commented on Jun 17, 2025

    @alexanderankin
    alexanderankin
    on Jun 17, 2025 via email
    Member
    More actions
    its a known bug, this library needs to start creating containers before starting them at some point
    …
    On Mon, Jun 16, 2025 at 1:54 PM Ryan Hoban ***@***.***> wrote: *rhoban13* left a comment (testcontainers/testcontainers-python#665) <#665 (comment)> From @anuraaga <https://github.com/anuraaga> comment: ... the key point is to set restart policy on failure to allow it to fail as expected on missing creds file until the copy completes. The need to restart the container I think is somewhat due to the fact that there is no way to directly create a container without also starting it, since our implementation of start <https://github.com/testcontainers/testcontainers-python/blob/main/core/testcontainers/core/container.py#L176> calls docker_client.run. Ideally what you'd like to do here is: docker container create docker cp docker start but there's no direct way to do that here. You could potentially directly use the some methods in docker.client.containers <https://docker-py.readthedocs.io/en/stable/containers.html#module-docker.models.containers> to achieve the same as well — Reply to this email directly, view it on GitHub <#665 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/ACECGJFQRF7ERARHK7X2ICT3D4AG3AVCNFSM6AAAAABMAC4U3SVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDSNZXGUYTQMJXGY> . You are receiving this because you are subscribed to this thread.Message ID: ***@***.*** com>
    Reacted by Ryan Hoban
  8. anuraaga commented on Jun 17, 2025

    @anuraaga
    anuraaga
    on Jun 17, 2025 · Hidden as off-topic
    show commentMore actions
  9. rhoban13 commented on Jun 17, 2025

    @rhoban13
    rhoban13
    on Jun 17, 2025
    Contributor
    More actions

    The linked PR seems to be stalled with a really good point there we need to clarify the interface a bit before moving forward with it. Perhaps we should create a issue or draft PR with simply some tests clarifying that interface?

  10. Tranquility2 commented on Apr 1, 2026

    @Tranquility2
    Tranquility2
    on Apr 1, 2026
    Contributor
    More actions

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

  11. Lenormju commented on Apr 2, 2026

    @Lenormju
    Lenormju
    on Apr 2, 2026
    Author
    More actions

    @Tranquility2 thanks for your contribution, I just tested it locally (commit 0e0bb24, unreleased yet) and indeed I can copy a file into a started container. But that was not the issue I was trying to solve originally, and that I did not express precisely (because I did not know there was such nuance to providing files to a container).

    My original issue was that the app started by the container requires a file at startup. Thus, copying the file into the container after it has started does not completely solve it, because it is still timing-dependent.

    Looking into what containers offer, what I actually wanted was a bind mount. I don't recall if it was already available at the time, or if I did overlook it. But DockerContainer(image=..., volumes=[("hello.txt", "/app/hello.txt", "ro")] works perfectly.

    So my original issue has a solution (and maybe it already did), and the more general issue (of copying to/from a running container) is solved to. In my sense, the issue can be considered solved.

  12. Lenormju
    closed this as completedon Apr 2, 2026
  13. alexanderankin commented on Apr 2, 2026

    @alexanderankin
    alexanderankin
    on Apr 2, 2026 via email
    Member
    More actions
    Ok yeah so we still have to make create->copy->start work
    …
    On Wed, Apr 1, 2026, 10:00 PM Lenormand Julien ***@***.***> wrote: *Lenormju* left a comment (testcontainers/testcontainers-python#665) <#665 (comment)> @Tranquility2 <https://github.com/Tranquility2> thanks for your contribution, I just tested it locally (commit 0e0bb24 <0e0bb24>, unreleased yet) and indeed I can copy a file into a *started* container. But that was not the issue I was trying to solve originally, and that I did not express precisely (because I did not know there was such nuance to providing files to a container). My original issue was that the app started by the container requires a file at startup. Thus, copying the file into the container after it has started does not completely solve it, because it is still timing-dependent. Looking into what containers offer, what I actually wanted was a bind mount. I don't recall if it was already available at the time, or if I did overlook it. But DockerContainer(image=..., volumes=[("hello.txt", "/app/hello.txt", "ro")] works perfectly. So my original issue has a solution (and maybe it already did), and the more general issue (of copying to/from a running container) is solved to. In my sense, the issue can be considered solved. — Reply to this email directly, view it on GitHub <#665?email_source=notifications&email_token=ACECGJCUNSIUU4GW73H27AT4TXXXZA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTIMJXGQ3DKOJWGYYKM4TFMFZW63VHMNXW23LFNZ2KKZLWMVXHJLDGN5XXIZLSL5RWY2LDNM#issuecomment-4174659660>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/ACECGJEPTDI7DXSRSM5UECD4TXXXZAVCNFSM6AAAAACXJYTFUGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHM2DCNZUGY2TSNRWGA> . You are receiving this because you commented.Message ID: ***@***.***>
    Reacted by Lenormand Julien
  14. rhoban13 commented on Apr 2, 2026

    @rhoban13
    rhoban13
    on Apr 2, 2026
    Contributor
    More actions

    @alexanderankin is there an issue for splitting container create/start out of the current start method? Should we create one?

    While probably not too hard to do, I can see some careful thought being needed for backwards compatibility with current start behavior.

  15. alexanderankin commented on Apr 2, 2026

    @alexanderankin
    alexanderankin
    on Apr 2, 2026
    Member
    More actions

    @rhoban13 ok i see, im going to create an issue to centralize discussion then.

  16. alexanderankin commented on Apr 2, 2026

    @alexanderankin
    alexanderankin
    on Apr 2, 2026
    Member
    More actions

    #997

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    🚀 enhancement🤝 contributions are welcome

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      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.