我尝试使用Fabric8 docker-maven-plugin,虽然我成功配置了单个模块的插件并运行docker:build docker:start
Maven目标而不使用docker-compose.yml,但是我需要外部化端口并链接不同的模块,因此我打算使用docker-compose.yml。以下是我的项目结构。
--kp-parent
|
--- docker-compose.yml
--- pom.xml
|
---- rest1
| |
| -- .maven-dockerignore
| -- pom.xml
| -- Dockerfile
---- rest2
| |
| -- .maven-dockerignore
| -- pom.xml
| -- Dockerfile
以下是我的配置
Dockerfile [rest1和rest2使用相同的文件,除了不同的端口]
FROM openjdk:8-jdk-alpine
MAINTAINER 'Karthik Prasad'
ARG IMAGE_VERSION
ARG JAR_FILE
ENV JAVA_OPTS=""
LABEL version = IMAGE_VERSION
VOLUME /tmp
ADD /maven/${JAR_FILE}.jar app.jar
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
EXPOSE 8000
.maven-dockerignore [在两个子模块中相同的文件]
target/**
pom.xml [除了artifcatid和项目名称外,rest1和rest2 pom文件都相同]
<?xml version="1.0" encoding="UTF-8"?>
<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>
<artifactId>rest2</artifactId>
<packaging>jar</packaging>
<name>rest2</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.kp.swasthik</groupId>
<artifactId>kp-docker-multimodule-fabric8-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- https://mvnrepository.com/artifact/io.fabric8/docker-maven-plugin -->
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<skip>false</skip>
<images>
<image>
<external>
<type>compose</type>
<basedir>../</basedir>
<ignoreBuild>true</ignoreBuild>
</external>
</image>
</images>
</configuration>
</plugin>
</plugins>
</build>
</project>
父pom.xml
<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>com.kp.swasthik</groupId>
<artifactId>kp-docker-multimodule-fabric8-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>kp-docker-multimodule-fabric8-parent</name>
<description>Microservice Parent Pom file</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<modules>
<module>rest1</module>
<module>rest2</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<docker.image.prefix>kp-ms</docker.image.prefix>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.22.1</version>
<configuration>
<skip>true</skip>
<images>
<image>
<alias>${project.artifactId}</alias>
<name>${docker.image.prefix}/${project.artifactId}:${project.version}</name>
<build>
<dockerFileDir>${project.basedir}</dockerFileDir>
<assembly>
<inline>
<id>default</id>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</inline>
</assembly>
</build>
</image>
</images>
<buildArgs>
<IMAGE_VERSION>${project.version}</IMAGE_VERSION>
<JAR_FILE>${project.artifactId}-${project.version}</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
最后是docker-compose.yml
version: '2'
services:
rest1:
image: kp-ms/rest1:0.0.1-SNAPSHOT
ports:
- 8000:8000
rest2:
image: kp-ms/rest2:0.0.1-SNAPSHOT
ports:
- 8001:8001
links:
- rest1
当我运行docker:build docker:start
时,我收到以下错误。
[ERROR] Failed to execute goal io.fabric8:docker-maven-plugin:0.22.1:start (default-cli) on project rest1: I/O Error: Unable to pull 'kp-ms/rest2:0.0.1-SNAPSHOT' : repository kp-ms/rest2 not found: does not exist or no pull access (Not Found: 404) -> [Help 1]
[ERROR]
但是如果我删除docker-compose.yml中的rest2部分,那么构建就可以了,我能够找到容器成功启动。
我注意到的另一个问题是即使在docker-compose.yml中没有给出图像名称,构建失败,错误图像为null。但是我不确定为什么我需要提供图像不能从插件配置插件映射,因为我提供了别名。您可以注意到我正在尝试动态生成图像名称。