Search code examples
docker-composedockerfiledockerfile-maven-plugin

How's spotify dockerfile-maven-plugin uses "docker-compose build"?


From the docs https://github.com/spotify/dockerfile-maven, it says:

For example, a docker-compose.yml might look like:

 service-a:
   build: a/
   ports:
   - '80'

 service-b:
   build: b/
   links:
   - service-a

Now, docker-compose up and docker-compose build will work as expected.

But how to write the pom.xml file, still like as if not using compose? Set the goal to be "build"?

<plugin>
  <groupId>com.spotify</groupId>
  <artifactId>dockerfile-maven-plugin</artifactId>
  <executions>
    <execution>
      <id>default</id>
      <goals>
        <goal>build</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Solution

  • You cannot build the images starting from a docker-compose.yml file using the spotify plugin. The readme mentions that the design goal was:

    Don't do anything fancy. Dockerfiles are how you build Docker projects; that's what this plugin uses.

    That section of the docs you quoted actually says that on a maven multi-module project structure one can easily use other build tools such as docker-compose.

    Nevertheless, there are ways to build with maven from a docker-compose.yml. One is with the maven-exec

    For the file in question:

    version: "2"
    services:
      service-a:
        build: a/
        image: imga
    
      service-b:
        build: b/
        image: imgb
        links:
          - service-a
    

    the relevant part of the pom.xml would be something like the following:

    <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.6.0</version>
            <executions>
              <execution>
                <id>docker-build</id>
                <phase>package</phase>
                <goals>
                  <goal>exec</goal>
                </goals>
                <configuration>
                  <executable>docker-compose</executable>
                  <workingDirectory>${project.basedir}</workingDirectory>
                  <arguments>
                    <argument>build</argument>
                  </arguments>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    

    Another solution is to use fabric8

    <build>
        <plugins>
          <plugin>
            <groupId>io.fabric8</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>0.26.0</version>
            <executions>
              <execution>
                <id>docker-build</id>
                <phase>package</phase>
                <goals>
                  <goal>build</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <images>
                <image>
                  <external>
                    <type>compose</type>
                    <basedir>${project.basedir}</basedir>
                    <composeFile>docker-compose.yml</composeFile>
                  </external>
                </image>
              </images>
            </configuration>
          </plugin>
        </plugins>
      </build>
    

    Use the one that suits you best.