这是我参与11月更文挑战的第1天,活动详情查看:2021最后一次更文挑战
准备工作:
安装docker
配置系统环境变量:
DOCKER_HOST=tcp://<host>:2375
1.通过docker-maven-plugin部署
官方git:github.com/spotify/doc…
国内镜像仓库建议配置在maven的setting.xml里面。一劳永逸
1.1.编辑pom文件
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<imageName>eureka</imageName><!-- 配置镜像名称 -->
<baseImage>java</baseImage><!-- 配置依赖的镜像,官方给的`java`,会自动去拉取这个镜像,亲测java8环境可直接用 -->
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint><!-- 运行时默认会执行的指令:执行这个jar包 -->
<!-- copy the service's jar file from target into the root directory of the image -->
<resources>
<resource><!-- 配置jar包的存放路径 -->
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
1.2.执行maven指令:mvn clean package docker:build
1.3.运行
docker run -it -d -p 0.0.0.0:8761:8761 eureka
2.通过Dockerfile Maven部署
在
docker-maven-plugin的github上明确说明了推荐我们使用这个
We recommend you use dockerfile-maven instead.
The future of docker-maven-plugin
This plugin was the initial Maven plugin used at Spotify for building Docker images out of Java services. It was initially created in 2014 when we first began experimenting with Docker. This plugin is capable of generating a
Dockerfilefor you based on configuration in the pom.xml file for things like theFROMimage, resources to add withADD/COPY, etc.Over time at Spotify we have realized that the simplest way to build a Docker image from a Java project is to have the developer write the
Dockerfile. The behavior of this plugin around generating Dockerfiles, copying your project directory to a "staging" directory to use as the Docker build context, etc., ultimately led to a lot of unnecessary confusion with our users that stemmed from introducing extra abstractions and a need for configuration on top of what Docker is providing.This led to the creation of a second Maven plugin for building docker images, dockerfile-maven, which we think offers a simpler mental model of working with Docker from Maven, for all of the reasons outlined in dockerfile-maven's README.
2.1.编辑pom文件
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.13</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>eureka</repository>
<!-- <tag>${project.version}</tag>-->
<tag>latest</tag>
<buildArgs>
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
2.2.编辑Dockerfile
FROM openjdk:8-jre
#MAINTAINER David Flemström <dflemstr@spotify.com>
ENTRYPOINT ["java", "-jar", "/usr/share/myservice/myservice.jar"]
# Add Maven dependencies (not shaded into the artifact; Docker-cached)
#ADD target/lib /usr/share/myservice/lib
# Add the service itself
ARG JAR_FILE
ADD target/${JAR_FILE} /usr/share/myservice/myservice.jar
2.3.执行maven指令:mvn clean package
2.4.运行
docker run -it -d -p 0.0.0.0:8761:8761 eureka