两种方式 docker-maven-plugin 、dockerfile-maven。两者是一家公司维护的。
方式1:docker-maven-plugin
第一种方法是使用docker-maven-plugin。
瞅了下官网
We recommend that new projects use dockerfile-maven. This project will no longer have new features or accept PRs for new features. We will continue to accept bug fixes, however.
可以能正常使用,但是官方已经停止更新了。推荐使用dockerfile-maven
配置pom.xml
先添加docker-maven-plugin
,选择的版本为0.4.13
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<imageName>ox-cleaver/${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
配置Dockerfile
src/main/docker
目录(pom文件配置的路径)下创建 Dockerfile 文件
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD eureka-server-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-Djaeva.security.egd=file:/dev/./urandom","-jar","/app.jar"]
打包
打包成jar
打包成docker镜像
mvn package docker:build
➜ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ox-cleaver/eureka-server latest 62265fc9b8c1 About a minute ago 149MB
...
默认tag是latest
,修改tag(可选)
$ docker tag 614b4ea7327e ox-cleaver/config-server:19-06-12
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ox-cleaver/config-server 19-06-12 614b4ea7327e About a minute ago 133MB
ox-cleaver/config-server latest 614b4ea7327e About a minute ago 133MB
$ docker rmi ox-cleaver/config-server:latest
Untagged: ox-cleaver/config-server:latest
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ox-cleaver/config-server 19-06-12 614b4ea7327e 2 minutes ago 133MB
运行
docker run --name eureka -p 8761:8761 -t ox-cleaver/eureka-server
参考
方式2:(推荐)dockerfile-maven
dockerfile-maven
pom配置
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.10</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>ox-cleaver/${project.artifactId}</repository>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
</plugins>
</build>
配置Dockerfile
在项目根目录(pom.xml同级),添加Dockerfile
FROM openjdk:8-jdk-alpine
EXPOSE 8761
ARG JAR_FILE
ADD target/${JAR_FILE} /eureka.jar
ENTRYPOINT ["java", "-jar","/eureka.jar"]
打包成docker镜像
假设机器上已经装了docker
以下命令都可以打包成docker镜像(先打包jar包 -> 执行dokerfile):
- mvn package
- mvn dockerfile:build
其他命令
- mvn dockerfile:push
- mvn dockerfile:tag
运行
查看docker镜像,发现已经被添加上了。
➜ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ox-cleaver/eureka-server 0.0.1-SNAPSHOT 8e8d3d28caae 50 seconds ago 149MB
运行该镜像
docker run --name eureka -p 8761:8761 -t ox-cleaver/eureka-server:0.0.1-SNAPSHOT