Skip to content
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

Copying a file from a container before starting it #125

Open
the-mentor opened this issue Jan 15, 2025 · 9 comments
Open

Copying a file from a container before starting it #125

the-mentor opened this issue Jan 15, 2025 · 9 comments
Labels
enhancement New feature or request

Comments

@the-mentor
Copy link
Contributor

the-mentor commented Jan 15, 2025

We are evaluating KubeDock as a way to run testcontainers-java in a pipeline on our existing k8s infrastructure.

Our developers say that when they are using testcontainers-java they use the feature of reading data from a container image prior to starting it so they can modify it and push it to the container either before starting it or after.

Is this something that can be implemented in KubeDock ?
My guess is that k8s won't have a mechanism for it so maybe KubeDock can download the image itself and extract the file from it?

EDIT:
I did some additional research on this feature and it looks like the tool crane supports exporting the container image to a tar and then we can use tar to extract a specific file/folder etc

Example:

# export the ubuntu docker image to ubuntu.tar
crane export ubuntu ubuntu.tar

# we can list the content of the tar with this command
tar -tvf ./ubuntu.tar

# we can export the content of the a specific file using this command
tar -zxvf ./ubuntu.tar var/log/alternatives.log

I assume its possible to read and extract from the tar file using go.
as far as using crane we can either install it the KubeDock image and call it or maybe look at its code since its written in go and implement a similar feature directly into KubeDock

Thanks
DM

@joyrex2001 joyrex2001 added the enhancement New feature or request label Jan 15, 2025
@joyrex2001
Copy link
Owner

Our developers say that when they are using testcontainers-java they use the feature of reading data from a container image prior to starting it so they can modify it and push it to the container either before starting it or after.

Do you have an example implementation, or reference to the documentation on how they do this?

@the-mentor
Copy link
Contributor Author

Hi @joyrex2001 thank you for the quick reply

Here is an example code

import org.testcontainers.couchbase.CouchbaseContainer;

public class TestContainersTest {
    private static CouchbaseContainer couchbase;

    @BeforeAll
    public static void setup() {
        couchbase = new CouchbaseContainer() {
            @Override
            public void containerIsCreated(String containerId) {
                super.containerIsCreated(containerId);
                copyFileFromContainer("/var/log/alternatives.log", "x2.log.throwaway");
            }
        };
        couchbase.start();
    }

    @AfterAll
    public static void teardown() {
        couchbase.stop();
    }

    @Test
    public void testCouchbase() throws Exception {

    }
}

In a broader context they say they use the below lines of code to copy the file then modify it somehow and then copy it back before starting the container.
targetContainer.copyFileFromContainer(containerFilePath, tempLocalStartupFile.toString());
and
targetContainer.copyFileToContainer(MountableFile.forHostPath(tempLocalStartupFile.toPath()), containerFilePath);

I hope it helps explain the use case

@the-mentor
Copy link
Contributor Author

Another way to replicate the issue using the docker cli commands with the real docker engine

# create a stopped container
docker create --name my_nginx nginx:latest

# copy a file from the stopped container
docker cp my_nginx /docker-entrypoint.sh ./test.sh

# start the container 
docker start my_nginx

@mavogel
Copy link

mavogel commented Jan 17, 2025

addition to #53

@the-mentor
Copy link
Contributor Author

@mavogel it's similar to #53 but not the same

@the-mentor
Copy link
Contributor Author

@joyrex2001 what do you think about this and the suggestion I made above ?

@joyrex2001
Copy link
Owner

I did some additional research on this feature and it looks like the tool crane supports exporting the container image to a tar and then we can use tar to extract a specific file/folder etc

@joyrex2001 what do you think about this and the suggestion I made above ?

I have doubts if this is the best approach, but I don't see an alternative either. Thinking in a more concrete implementation, the approach would be, if container is not yet running, copy directly via from the image, if it is started, business as usual, right?

@the-mentor
Copy link
Contributor Author

@joyrex2001 thinking through the problem it seems there are two approaches to solve this

  1. Spin up a temp pod and copy the file from it and then destroy it.

  2. Download the container image and then extract the file from it.

The downsides I see with the first approach are
a. Copying happens at the context of the user so you can't copy something the application doesn't have access to
b. Since you start the temp container you might copy files after they are modified if something modifies them during runtime.
So this approach might work but won't be a one to one comparison with what docker does.

Let me know what you think about these two approaches.

@joyrex2001
Copy link
Owner

I think option 1 isn't actually an option, for the reasons you state, but also it might not even start, or have unwanted side-effects. So, option 2 looks like the only option that would work; which is, if a container is not yet started, but a copy (archive) command is done, it will use this approach (and otherwise it would do as-is).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants