spotify dockerfile-maven-plugin 如何使用 "docker-compose build"?

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

来自文档 https://github.com/spotify/dockerfile-maven,它说:

例如,docker-compose.yml 可能如下所示:

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

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

现在,docker-compose up 和 docker-compose build 将按预期工作。

但是pom.xml文件怎么写,还是跟没用compose似的?将目标设置为 "build"?

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

您不能使用 spotify 插件从 docker-compose.yml 文件开始构建图像。自述文件提到设计目标是:

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

您引用的那部分文档实际上是说在 Maven multi-module 项目结构上,可以轻松地使用其他构建工具,例如 docker-compose

不过,有一些方法可以从 docker-compose.yml 使用 Maven 进行构建。一个是 maven-exec

对于有问题的文件:

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

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

pom.xml 的相关部分如下所示:

<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>

另一个解决方案是使用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>

使用最适合您的那个。