I'm using testcontainers in Java tests. To configure application in container I need to put and mount configuration files: some files are static, so I'm mounting them using withClassPathResourceMapping when creating new container:
container.withClassPathResourceMapping(
"/path/to/res",
"/etc/app/config.name",
BindMode.READ_ONLY
)
Other files are generated dynamically and could be overridden by app in container, so I'm copying the content to container using copyFileToContainer after container start:
container.copyFileToContainer(
Transferable.of(bin /* byte[] */),
"/var/app/resource.name"
)
The app is running in container as app:app user and group which are defined in the Dockerfile.
And I have two similar problems here:
withClassPathResourceMappingoperation creates missed directory if not found, e.g. classpath mapping for"/etc/app/config.name"creates"/etc/app/directory. But it creates these directories asroot:rootuser, so app can't create new files in this directory later- the files which are copied into container using
copyFileToContainerare not read-only and can be modified by app. ButcopyFileToContainercreates files asroot:rootuser, so app can't write to these files.
I tried to exec chown -R /path after container start but this command fails, since the exec user is not a root.
What is the correct way to setting owner and permissions in testcontainers?