基础环境
- 已成功安装Docker,本地可以正常使用。
- 本地安装有Java编辑器,本文中使用的是IDEA。
进入正题
1. 请确保开启Docker远程API访问
开启方式请参考DooD:Docker+Jenkins
配置成功后再docker所在的服务器可以看到2375端口已经开启
2. 配置本地环境变量
如下图所示,配置环境变量:DOCKER_HOST,值为tcp://IP:Port;此处的IP为Docker所安装的服务器的IP
3. 构建一个SpringBoot项目
-
此处我使用的是IDEA新建的Spring Boot项目,如下图
-
项目目录结构如下
4. pom.xml引入dockerfile-maven-plugin插件
<properties>
<docker.image.prefix>dockerlgf</docker.image.prefix>
<com.spotify.version>1.4.4</com.spotify.version>
</properties>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>${com.spotify.version}</version>
<configuration>
<!-- docker.image.prefix一般配置的是private registry或者自己的dockerhub仓库地址-->
<repository>${docker.image.prefix}/${project.artifactId}</repository>
<!-- 配置的构建的Docker image的tag -->
<tag>${project.version}</tag>
<buildArgs>
<!-- 配置了一个参数JAR_FILE,在根据Dockerfile构建image会用到,下文会看到 -->
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
5. 为第三步新建的项目编写一个简单的Dockerfile
# 基础镜像:openjdk:8-jdk-alpine
FROM openjdk:8-jdk-alpine
# 挂载目录,默认的安装jar包位置:/tmp
VOLUME /tmp
# 上一步在pom.xml指定的JAR_FILE参数
ARG JAR_FILE
# 将上一步的jar包copy到镜像中的根目录,并重命名为app.jar
COPY ${JAR_FILE} app.jar
# 启动jar包
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
6. 使用maven命令构建一个Docker镜像
- 运行
mvn clean package dockerfile:build
来构建一个Docker image,控制台输出如下
[INFO] --- dockerfile-maven-plugin:1.4.4:build (default-cli) @ spring-boot-docker ---
[INFO] Building Docker context E:\Java\code\spring-boot-docker
[INFO]
[INFO] Image will be built as dockerlgf/spring-boot-docker:0.0.1-SNAPSHOT
[INFO]
[INFO] Step 1/5 : FROM openjdk:8-jdk-alpine
[INFO]
[INFO] Pulling from library/openjdk
[INFO] Image 4fe2ade4980c: Pulling fs layer
[INFO] Image 6fc58a8d4ae4: Pulling fs layer
[INFO] Image ef87ded15917: Pulling fs layer
[INFO] Image 6fc58a8d4ae4: Downloading
[INFO] Image 6fc58a8d4ae4: Verifying Checksum
[INFO] Image 6fc58a8d4ae4: Download complete
[INFO] Image 4fe2ade4980c: Downloading
[INFO] Image 4fe2ade4980c: Verifying Checksum
[INFO] Image 4fe2ade4980c: Download complete
[INFO] Image 4fe2ade4980c: Extracting
[INFO] Image ef87ded15917: Downloading
[INFO] Image 4fe2ade4980c: Pull complete
[INFO] Image 6fc58a8d4ae4: Extracting
[INFO] Image 6fc58a8d4ae4: Pull complete
[INFO] Image ef87ded15917: Download complete
[INFO] Image ef87ded15917: Extracting
[INFO] Image ef87ded15917: Pull complete
[INFO] Digest: sha256:b18e45570b6f59bf80c15c78d7f0daff1e18e9c19069c323613297057095fda6
[INFO] Status: Downloaded newer image for openjdk:8-jdk-alpine
[INFO] ---> 97bc1352afde
[INFO] Step 2/5 : VOLUME /tmp
[INFO]
[INFO] ---> Running in 814c0afe6c60
[INFO] Removing intermediate container 814c0afe6c60
[INFO] ---> 7754756ce600
[INFO] Step 3/5 : ARG JAR_FILE
[INFO]
[INFO] ---> Running in 97f2dc95ae80
[INFO] Removing intermediate container 97f2dc95ae80
[INFO] ---> ec17923a3707
[INFO] Step 4/5 : COPY ${JAR_FILE} app.jar
[INFO]
[INFO] ---> ec366faad7ad
[INFO] Step 5/5 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
[INFO]
[INFO] ---> Running in 44152e10f872
[INFO] Removing intermediate container 44152e10f872
[INFO] ---> 31f6c6947a0b
[INFO] Successfully built 31f6c6947a0b
[INFO] Successfully tagged dockerlgf/spring-boot-docker:0.0.1-SNAPSHOT
[INFO]
[INFO] Detected build of image with id 31f6c6947a0b
[INFO] Building jar: E:\Java\code\spring-boot-docker\target\spring-boot-docker-0.0.1-SNAPSHOT-docker-info.jar
[INFO] Successfully built dockerlgf/spring-boot-docker:0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
-
在Docker安装的服务器上,已经可以看到最新构建的镜像
7. 配置Maven命令默认执行dockerfile命令
-
在上一步我们看到需要执行dockerfile相关的maven命令才可以执行,为了方便,我们可以将不同的dockerfile命令绑定到不同的maven phase.如下图所示。
- 绑定方式是在pom.xml中添加相应的配置,如下所示
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>${dockerfile-maven-version}</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>${docker.image.prefix}/${project.artifactId}</repository>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
此时,执行mvn package == mvn package + dockerfile:build
执行 mvn deploy == mvn deploy +dockerfile:push
8. push镜像到dockerhub或private registry
官网讲到有两种方式
- 第一种:Authenticating with maven pom.xml:从1.3.XX版本以后,至此在pom.xml中直接配置username和password来完成push到dockerhub或者private registry,或者在命令行执行时指定mvn goal -Ddockerfile.username=... -Ddockerfile.password=...
目前我只测试成功了这一种方式,pom.xml中配置如下
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>${com.spotify.version}</version>
<configuration>
<!-- dockerhub或者private registry账号 -->
<username>dockerlgf</username>
<!-- dockerhub或者private registry密码 -->
<password>xxxxxx</password>
<useMavenSettingsForAuth>true</useMavenSettingsForAuth>
<repository>${docker.image.prefix}/${project.artifactId}</repository>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
</plugins>
</build>
- 第二种:Authenticating with maven settings.xml:从1.3.6版本以后,可以通过在maven settings.xml配置server信息,pom.xml只需要添加下面的配置
pom.xml
<configuration>
<!-- dockerhub或者private registry地址 -->
<repository>docker-repo.example.com:8080/organization/image</repository>
<tag>latest</tag>
<useMavenSettingsForAuth>true</useMavenSettingsForAuth>
</configuration>
maven settings.xml
<servers>
<server>
<id>docker-repo.example.com:8080</id>
<username>me</username>
<password>mypassword</password>
</server>
</servers>