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 · 5 comments
Open

Copying a file from a container before starting it #125

the-mentor opened this issue Jan 15, 2025 · 5 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

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