【问题标题】:exec-maven-plugin - copy directory from docker container to hostexec-maven-plugin - 将目录从 docker 容器复制到主机
【发布时间】:2021-03-21 22:19:53
【问题描述】:

我正在尝试使用 maven exec 插件在 maven 构建期间将目录从正在运行的 docker 容器复制到主机。从命令行执行 exec 目标时成功复制目录

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

但是由于错误而失败

错误:没有这样的容器:路径:$(docker ps -aqf "name=my_container"):/home/user/out [ERROR] 命令 执行失败。 org.apache.commons.exec.ExecuteException:进程 退出错误:1(退出值:1)

使用这样配置的插件在 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>

我尝试在单独的参数标签中分隔主机“./target/user-reports”目录,导致同样的错误。 使用卷挂载不是一个选项,因为构建是在我的 CI 环境中的另一个 docker 容器 (Dind) 中运行的。非常感谢您对我所缺少的任何输入,谢谢。

【问题讨论】:

    标签: docker maven


    【解决方案1】:

    Maven 在这里没有运行 shell,所以像 $(...) 命令替换这样的 shell 结构将不起作用。您还需要确保将每个“单词”分成单独的&lt;argument/&gt;。

    大多数采用容器 ID 的 Docker 命令也采用容器名称。所以你不需要使用docker ps -qf name=... 来查找容器的ID;你可以直接在命令中使用它的名字。

    这组合应该给你:

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

    【讨论】:

    • 非常感谢您清理问题。 +1
    猜你喜欢
    • 2018-08-29
    • 2021-04-03
    • 2018-03-25
    • 2014-03-29
    • 2015-07-08
    • 2013-03-19
    • 2019-10-07
    相关资源
    最近更新 更多