简体   繁体   English

exec-maven-plugin - 将目录从 docker 容器复制到主机

[英]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.我正在尝试使用 maven exec 插件将目录从正在运行的 docker 容器复制到 maven 构建期间的主机。 Directory is successfully copied when exec goal is executed from command line从命令行执行 exec 目标时成功复制目录

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.错误:没有这样的容器:路径:$(docker ps -aqf "name=my_container"):/home/user/out [ERROR] 命令执行失败。 org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1) org.apache.commons.exec.ExecuteException:进程退出并出现错误:1(退出值:1)

on maven install with plugin configured like this在 maven 上安装插件,配置如下

<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.我尝试在单独的参数标记中分隔主机“./target/user-reports”目录,导致相同的错误。 Using volume mount is not an option as build is run inside another docker container (Dind) in my CI enviroment.使用卷挂载不是一个选项,因为构建是在我的 CI 环境中的另一个 docker 容器 (Dind) 中运行的。 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. Maven 没有在此处运行 shell,因此 shell 结构,如$(...)命令替换将不起作用。 You also need to make sure to separate each "word" into a separate <argument/> .您还需要确保将每个“单词”分成单独的<argument/>

Most Docker commands that take container IDs also take container names.大多数采用容器 ID 的 Docker 命令也采用容器名称。 So you don't need to use docker ps -qf name=... to find a container's ID;所以你不需要使用docker ps -qf name=...来查找容器的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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