简体   繁体   中英

exec-maven-plugin - copy directory from docker container to host

I am attempting to utilize maven exec plugin to copy a directory from a running docker container to host during a maven build. Directory is successfully copied when exec goal is executed from command line

mvn exec:exec -Dexec.executable="docker" -Dexec.args="cp $(docker ps -aqf "name=my_container"):/home/user/out ./target/user-reports"

However fails with error

Error: No such container:path: $(docker ps -aqf "name=my_container"):/home/user/out [ERROR] Command execution failed. org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)

on maven install with plugin configured like this

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.6.0</version>
    <executions>
    <execution>
        <id>Copy user reports from container to host</id>
        <goals>
            <goal>exec</goal>
        </goals>
        <phase>post-integration-test</phase>
        <configuration>
            <executable>docker</executable>
            <arguments>
                <argument>cp</argument>
                <argument>$(docker ps -aqf "name=my_container"):/home/user/out ./target/user-reports</argument>
            </arguments>
        </configuration>
    </execution>
</plugin>

I have tried seperating host "./target/user-reports" directory in a separate argument tag resulting in the same error. Using volume mount is not an option as build is run inside another docker container (Dind) in my CI enviroment. Any input on what I am missing is much appreciated, thanks.

Maven isn't running a shell here, so shell constructs like $(...) command substitution won't work. You also need to make sure to separate each "word" into a separate <argument/> .

Most Docker commands that take container IDs also take container names. So you don't need to use docker ps -qf name=... to find a container's ID; you can just use its name directly in the command.

The combination of this should give you:

<configuration>
    <executable>docker</executable>
    <arguments>
        <argument>cp</argument>
        <argument>my_container:/home/user/out</argument>
        <argument>./target/user-reports</argument>
    </arguments>
</configuration>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM