Skip to content

Feature: add DockerContainer.with_copy_file_to_container #665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Lenormju opened this issue Aug 5, 2024 · 2 comments
Open

Feature: add DockerContainer.with_copy_file_to_container #665

Lenormju opened this issue Aug 5, 2024 · 2 comments

Comments

@Lenormju
Copy link

Lenormju commented Aug 5, 2024

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.

@Lenormju Lenormju changed the title Feature: add DockerContainer.with_copy_file_ton_container Feature: add DockerContainer.with_copy_file_to_container Aug 5, 2024
@Lenormju
Copy link
Author

Lenormju commented Aug 5, 2024

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 😄

@anuraaga
Copy link

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!')

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

No branches or pull requests

3 participants