Skip to content

Need capability to copy files from running container to host #752

Closed
@testphreak

Description

@testphreak

Description

Unable to copy files in the target directory after tests have completed from container to host.

If this is already currently possible with the plugin, please let me know.

Info

  • d-m-p version : 0.20.1
  • Maven version (mvn -v) :
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T08:41:47-08:00)
Maven home: /Users/axiom/tools/JavaLib/main/build/apache-maven-3.3.9
Java version: 1.8.0_92, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_92.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.10.5", arch: "x86_64", family: "mac"
  • Docker version : 17.03.1-ce, build c6d412e

  • If it's a feature request, what is your use case :

The use case I have is I'd like to copy the test results in my target directory in the post-integration-test phase from within the running container to the host.

Would be great if we could do something like this -

                 <executions>
                    <execution>
                        <id>copy</id>
                        <phase>post-integration-test</phase>
                        <configuration>
                            <fromContainerDirectory>./target/results/</fromContainerDirectory>
                            <toHostDirectory>./target/results</toHostDirectory>
                        </configuration>
                        <goals>
                            <goal>cp</goal>
                        </goals>
                    </execution>
                </executions>

Activity

testphreak

testphreak commented on Apr 5, 2017

@testphreak
Author

I just realized that the solution I proposed above won't work in my case. Please disregard it.

How can I keep the container running after maven exits?

I would like to copy the files I need from the container before stopping the container.

docker cp <containerId>:/file/path/within/container /host/path/target
Gengar003

Gengar003 commented on Apr 5, 2017

@Gengar003

I think the best solution is to use a docker volume mount to share the directory inside your container that contains the files you want, with the host machine.

The d-m-p documentation is here: https://dmp.fabric8.io/#start-volumes

Your XML section might look like this:

...
<volumes>
	<bind>
		<volume>${project.basedir}/target:${container.work.dir}/target/results</volume>
	</bind>
</volumes>
...

Then, when your running container writes to its target/results directory, Docker will automatically write those files to your host machine's target/results directory, and when the container stops, those files will still be there for you.

This will work with the existing docker-maven-plugin.

testphreak

testphreak commented on Apr 6, 2017

@testphreak
Author

Thanks @Gengar003! That suggestion definitely put me on the right track!

NiasSt90

NiasSt90 commented on Mar 15, 2019

@NiasSt90

with volume-bind mounts you have another problem if your container is running under a $user account.
Because the $user account can't write into the target/results directory.
Therefore a copy-goal will be useful.

In my case i want to measure code-coverage with a jacoco agent and pull the results out of the container to merge them with other jacoco-results.

Gengar003

Gengar003 commented on Mar 15, 2019

@Gengar003

I work around that by adding a chmod a+rwx target/results. I do that by having a script in my project that reads something like:

testrun.sh

the-actual-command-i-want --some-arg

chmod a+rwx target/results

Then when I start a container that will write to target/results, I also:

  1. bind-mount in testrun.sh
  2. have it run testrun.sh instead of the actual command

This leaves the contents of the target/results directory world-accessible, so that no matter which user was used inside the container, the host will be able to read them.

This does assume that the container you're running can chmod. If not, you could (although this starts to get clumsy) run another container after the first - something simple like alpine - and just have it run chmod a+rwx target/results as root.

mabrarov

mabrarov commented on Oct 20, 2020

@mabrarov
Contributor

This feature is still looking must have for me (I'm using builder image and need to copy built binaries from container to the maven project) because I use remote Docker instance and solution suggested in that comment doesn't work for such case while docker cp command works fine. Is it possible to reopen this feature request (issue) and implement it?

Note: I cannot use multi-stage build due to old version of Docker (RHEL 7)

mabrarov

mabrarov commented on Oct 25, 2020

@mabrarov
Contributor

I started implementation of this feature in master...mabrarov:copy_mojo.

Working example with usage of new version of docker-maven-plugin can be found in feature/docker-maven-plugin-copy-mojo branch of mabrarov/maven-docker-builder.

Volunteers wanted to complete and polish the changes. What's missing at the moment:

  • io.fabric8.maven.docker.config.handler.property.PropertyConfigHandler class needs to be extended - need to implement loading of io.fabric8.maven.docker.config.CopyConfiguration from given io.fabric8.maven.docker.config.handler.property.ValueProvider.
    Unit tests and integration tests.
    Samples. I plan to copy modified (simplified) mabrarov/maven-docker-builder as one of the samples.
    Documentation for the new copy goal.
    All commits are not signed which is required according to https://github.com/fabric8io/docker-maven-plugin/blob/master/CONTRIBUTING.md
    There may be a need of deeper refactoring of existing code to cleanup / make architecture more straightforward.

Here is one more (more complicated and closer to real project) sample using docker builder pattern and copy goal of d-m-p: https://github.com/mabrarov/redis-builder

mabrarov

mabrarov commented on Oct 26, 2020

@mabrarov
Contributor

@testphreak,

Could you please reopen this issue? I don't like duplicates, so prefer to not open a new issue, just because the old closed one was found not needed at the time it was closed.

Thank you.

testphreak

testphreak commented on Oct 26, 2020

@testphreak
Author

@mabrarov reopened

mabrarov

mabrarov commented on Nov 13, 2020

@mabrarov
Contributor

I opened pull request #1405. Though it lacks some of the things, but it already works for me in mabrarov/redis-builder.

mabrarov

mabrarov commented on Nov 13, 2020

@mabrarov
Contributor

I suggest to look at mabrarov/redis-builder for those who wants to try copy goal implemented in pull request #1405. mabrarov/redis-builder contains travis/settings.xml Maven configuration file which points to Bintray repository where d-m-p built from the pull request #1405 can be found:

<plugin>
    <groupId>io.fabric8</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>0.34-dev</version>
</plugin>
qalinn

qalinn commented on Mar 1, 2021

@qalinn

When this will be released?

mabrarov

mabrarov commented on Mar 1, 2021

@mabrarov
Contributor

Hi @qalinn,

When this will be released?

Refer to #1412 (comment). I can work on pull request #1405 and complete it, but it doesn't look like it will be accepted before the mentioned cleanup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @mabrarov@Gengar003@NiasSt90@qalinn@testphreak

    Issue actions

      Need capability to copy files from running container to host · Issue #752 · fabric8io/docker-maven-plugin