1

We are using io.fabric8:docker-maven-plugin:0.27.2 to build our docker images.

Just wondering how to pass rm into buildoptions? I want to clear all the intermediate (<none>) images after successfully building them - using the mvn docker:build command

REPOSITORY            TAG       IMAGE ID         CREATED             SIZE
myproject/baseimage   latest    baa18e544738     3 days ago          1.53GB
<none>                <none>    c98ecb5bc381     3 days ago          784MB
<none>                <none>    14d3f81c4bc0     4 days ago          533MB
<none>                <none>    9b07174fc67a     4 days ago          532MB

I tried to pass something like this.

<buildoptions>
   <rm>true</rm>
</buildoptions>

equivalent of:

docker build --rm -f Dockerfile -t myproject/baseImage:latest .

The documentation is not very clear -> http://dmp.fabric8.io/#build-configuration

Full Configuration of pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>io.fabric8</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>0.27.2</version>
            <extensions>true</extensions>
            <configuration>
                <verbose>true</verbose>
                <images>
                    <image>
                        <name>myproject/baseimage</name>
                        <build>
                            <tags>
                                <tag>latest</tag>
                            </tags>
                            <dockerFile>${project.basedir}/Dockerfile</dockerFile>
                            <buildOptions>
                                <rm>true</rm>
                            </buildOptions>
                        </build>
                    </image>
                </images>
            </configuration>
            <executions>
                <execution>
                    <id>docker:build</id>
                    <phase>package</phase>
                    <goals>
                        <goal>build</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

I tried a bunch of things including passing the same variable in maven properties. But nothing worked.

<docker.buildoptions.rm>true</docker.buildoptions.rm>

Any help is greatly appreciated!

Dinesh
  • 1,711
  • 2
  • 20
  • 41

1 Answers1

0

As mentioned in documentation, there is an enum cleanup which removes dangling (untagged) images after each build (including any containers created from them). It can be set to true, false and try which tries to remove the old image, but doesn’t fail the build if this is not possible because e.g. the image is still used by a running container. You can set it in your build configuration like:

<build>
   <from>${image}</from>
   <labels>
      <dmp.version>${project.version}</dmp.version>
      <dmp.name>${project.artifactId}</dmp.name>
   </labels>
   <assembly>
      <descriptor>assembly.xml</descriptor>
   </assembly>

   ...

   <cleanup>true</cleanup>  
</build>
Rohan Kumar
  • 5,427
  • 8
  • 25
  • 40
  • hmm... `cleanup` doesn't work. It still leaves all the images. Mine is a multi-module maven project - Each one builds its own image. Not sure if that's causing any issues. – Dinesh Dec 27 '18 at 13:38
  • ah, What happens when you do it for a single module individually? Could you please share your project if possible? – Rohan Kumar Dec 27 '18 at 14:22
  • Hi @Rohan, Ok, I'll try that. I posted another question related to fabricio8 plugin -> https://stackoverflow.com/questions/53909282/save-multiple-docker-images-into-one-tar-gz-file-with-maven-fabric8-plugin. Where I tried to describe my full project structure. Can you take a look at that? Hopefully that should give you an idea of what my project is. Unfortunately, I can't post the actual source code as it's not open source. – Dinesh Dec 27 '18 at 15:17