fabric8io / docker-maven-plugin

Maven plugin for running and creating Docker images
https://dmp.fabric8.io
Apache License 2.0
1.88k stars 642 forks source link

[Question] Mimick "docker run" behaviour #1398

Open fmiguelez opened 3 years ago

fmiguelez commented 3 years ago

Description

I would like to run a container and let it return from maven, same way I would do with "docker run". However when using the plugin execution is stuck and never returns control. Is it possible to use wait/healthcheck or whatever other options to just return immediatly after container execution has completed successfully or not?

This is is an example to illustrate this.

Docker command:

C:\>docker run alpine echo "Hello world!"
Hello world!

I create now a maven project with this simple pom file:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>test</groupId>
    <artifactId>maven-docker-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>Mavend Docker Plugin Test</name>

    <build>
        <plugins>
            <plugin>
                <groupId>io.fabric8</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <configuration>
                    <images>
                        <image>
                            <name>alpine</name>
                            <alias>alpine</alias>
                            <run>
                                <cmd> 
                                    <arg>sh</arg>
                                    <arg>-c</arg>
                                    <arg>echo "Hello world!"</arg>
                                </cmd>  
                            </run>
                        </image>
                    </images>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

And try to run it:

C:\maven-docker-test>mvn docker:run
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for test:maven-docker-test:pom:0.0.1-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for io.fabric8:docker-maven-plugin is missing. @ line 16, column 12
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Mavend Docker Plugin Test 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- docker-maven-plugin:0.34.1:run (default-cli) @ maven-docker-test ---
[INFO] DOCKER> [alpine:latest] "alpine": Start container e97ecc322e01
alpine> Hello world!     

The command is stuck and never returns control unless I press CTRL-C.

Is it possible at all what I am asking for?

fmiguelez commented 3 years ago

Using wait option does what I want:

                                        <wait>
                                            <exit>0</exit>
                                            <time>600000</time>
                                        </wait>

The problem is that it does not return when container execution finishes. You have tu press CTRL-C

[INFO] DOCKER> [myimage] "ansible": Waited on exit code 0 273551 ms                                                   <---- It never returns control here. I have to press CTRL-C
[INFO] DOCKER> [myimage] "ansible": Stop and removed container 08d221c96b46 after 0 ms