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

狂风中的少年 提交于 2019-12-13 00:09:41

问题


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>

回答1:


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.



来源:https://stackoverflow.com/questions/57105061/hows-spotify-dockerfile-maven-plugin-uses-docker-compose-build

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!