Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

Hi, I'm trying to copy and mount files into a container with user and group permissions. I explained the problem in details in SO question but didn't get answers there: https://stackoverflow.com/q/67869196/1723695

First, I need to mount some static resource into a container using withClassPathResourceMapping:

container.withClassPathResourceMapping(
  "/path/to/res",
  "/etc/app/config.name",
  BindMode.READ_ONLY
)

And second, copy data to container file with copyFileToContainer:

container.copyFileToContainer(
 Transferable.of(bin /* byte[] */),
 "/var/app/resource.name"
)

Both files are used by the application in container on start, but the application is running with app:app user. How can I copy and mount resources with correct ownership?

You must be logged in to vote

Replies: 2 comments

Comment options

i imagine this never got resolved, sadly.

You must be logged in to vote
0 replies
Comment options

Use withCopyFileToContainer with Transferable to set file ownership:

@Testcontainers
class FileOwnerTest {

    @Container
    static GenericContainer<?> container = new GenericContainer<>("alpine:latest")
        .withCommand("sleep", "infinity");

    @Test
    void testFileOwnership() throws Exception {
        // Create file with specific ownership
        container.copyFileToContainer(
            Transferable.of("hello world".getBytes())
                .withOwner("myuser:mygroup"),  // sets uid:gid
            "/tmp/test.txt"
        );

        // Or copy from local filesystem with ownership
        container.copyFileToContainer(
            MountableFile.forHostPath("/local/path/config.yml")
                .withOwner("1001:1001"),  // numeric uid:gid
            "/app/config.yml"
        );

        // For Dockerfile COPY --chown behavior, build the image with:
        // COPY --chown=myuser:mygroup file.txt /app/
        // and use Testcontainers with that image
    }
}

The withOwner() method accepts "user:group" format (names or numeric IDs). The user/group must exist inside the container, or use numeric IDs that correspond to the container's /etc/passwd entries.

For older Testcontainers versions without withOwner(), run a command after copy:

container.execInContainer("chown", "-R", "1001:1001", "/app/config.yml");
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.