-
Notifications
You must be signed in to change notification settings - Fork 308
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
feat(core): add mount files when starting a container #677
base: main
Are you sure you want to change the base?
feat(core): add mount files when starting a container #677
Conversation
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 think it overlaps quite a lot with with_volume_mapping no?
the difference between mounting and copying is that one uses the linux file system, and the other uses a dedicated docker api where you can PUT a tar archive and docker engine un-tars for you inside the virtual file system of a running container, i think this addition blurs that distinction. going to leave open while we sort out a solution |
Found this wishing for copying, related comment in #676 (comment) Agree that mounting isn't copying, so the method shouldn't be called |
Do you still intend to move forward with this? I really need a way of inputting a config file into a test container. |
@oelhammouchi look at the code I wrote for |
@oelhammouchi If you're asking since you need actual copying instead of mounting, here's my current workaround. Note it relies on the container crashing when the file isn't present, which is probably common (in this case, it crashes while # 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')
def main():
...
with (
DockerContainer('gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.14.3')
.with_command(command)
.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) |
I have created the issue #665 on which I proposed to implement it.
I have read the contribution guidelines.
The feature works fine, as you can see by the tests.
But I have some doubts about my solution.
I made some decisions that you may not agree with, so I'd like to have your feedback whether or not it suits you, so that I can fix this PR if required.
core/testcontainers/core/container.py
I addedfrom docker.types.services import Mount
. I noticed that thetestcontainers
package had very few imports of thedocker
package. Is it OK to do it ? Or should it be made the same way than *volumes* (i.e. do not use the type defined in the
docker.types` package, create an untyped dict instead) ?core/tests/test_core.py
I addedfrom testcontainers.core.container import docker
, whosedocker
is thedocker
package, is it OK ?core/tests/test_core.py
test file. Is it OK to add all of them ?DockerContainer.with_copy_file_to_container
incore/testcontainers/core/container.py
) accepts its paths arguments as bothstr
andpathlib.Path
(thanks toUnion
). I think it makes for better APIs (because there is no need to convert before to call them, they handle it). But that was not the way it was done in the rest of the file. Is it OK to do it like that ? Or should I change to accept onlystr
s ?Thanks in advance 😃