0. 项目版本
- Spring Boot 1.5.15.RELEASE
- Apache Maven 3.5.2
- MAC OS 10.13.6
- Java Version 1.8.0_161
1. 安装Docker
下载地址:https://download.docker.com/mac/stable/Docker.dmg (可使用迅雷等工具下载,浏览器下载太慢)
下载之后安装,启动,此处跳过。
参考 : [ Mac平台上Docker安装与使用]
2. 编写Dockerfile
在项目的src/main目录下创建文件夹:docker,新建文件:Dockerfile
内容如下:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD dandelion-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8081
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
配置信息介绍:
FROM :表示使用 Jdk8 环境 为基础镜像,如果镜像不是本地的会从 DockerHub 进行下载
VOLUME :指向了一个/tmp的目录,由于 Spring Boot 使用内置的Tomcat容器,Tomcat 默认使用/tmp作为工作目录。这个命令的效果是:在宿主机的/var/lib/docker目录下创建一个临时文件并把它链接到容器中的/tmp目录
ADD :拷贝文件并且重命名(dandelion-0.0.1-SNAPSHOT.jar为自己项目打包的jar名称)
EXPOSE :端口
ENTRYPOINT :为了缩短 Tomcat 的启动时间,添加java.security.egd的系统属性指向/dev/urandom作为 ENTRYPOINT
参考: [ Docker Dockerfile 定制镜像]
3. 修改pom.xml
- properties中增加docker.image.prefix
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!-- 新增docker镜像名称配置 -->
<docker.image.prefix>mvp</docker.image.prefix>
</properties>
- build plugins中增加docker maven plugin
<!-- Docker maven plugin -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<imageName>${docker.image.prefix}/${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>
<!-- Docker maven plugin -->
注:groupId、artifactId、version 均无需修改!!
4. 构建 Docker Image
终端进入项目目录,可先使用mvn package确认项目能否打包成功。
确认无误后,构建Docker Image: mvn package docker:build
构建成功,使用docker images
可查看镜像
➜ Desktop docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mvp/dandelion latest 91e7c33bf73d 3 hours ago 144MB
openjdk 8-jdk-alpine 20faac299d19 4 days ago 103MB
mvp/dandelion 即为本项目构建后的Image
运行Image:docker run -p 8080:8080 -t mvp/dandelion
参考: [ docker-1.12 run命令运行以及参数详解 ]
5. Linux上部署与运行
- 开发环境导出Image
sudo docker save -o mvp.tar mvp/dandelion
mvp.tar 为导出镜像的位置及名称
mvp/dandelion为要导出的镜像名
参考: [ docker images 导入导出 ] - 上传镜像tar包
可使用scp命令或上传工具上传至服务器上 - 安装Docker
yum install docker
参考: [ CentOS平台下的Docker安装与启动 ] - 导入Image
sudo docker load --input mvp.tar
- 运行Image
docker run -p 8080:8080 -t mvp/dandelion
6. 高能预警
- No plugin found for prefix ‘docker’ in the current project and in the plugin groups
在Maven的配置文件 conf/settings.xml中添加com.spotify
<pluginGroups>
<pluginGroup>com.spotify</pluginGroup>
</pluginGroups>
我的配置文件就在Maven的安装目录(解压版)
- com.spotify.docker.client.shaded.com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.spotify.docker.client.messages.RegistryAuth: no String-argument constructor/factory method to deserialize from String value (‘swarm’)
详细错误如下:
[ERROR] Failed to execute goal com.spotify:docker-maven-plugin:1.0.0:build (default-cli) on project dandelion: Exception caught: com.spotify.docker.client.shaded.com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.spotify.docker.client.messages.RegistryAuth: no String-argument constructor/factory method to deserialize from String value ('swarm')
[ERROR] at [Source: N/A; line: -1, column: -1] (through reference chain: java.util.LinkedHashMap["stackOrchestrator"])
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal com.spotify:docker-maven-plugin:1.0.0:build (default-cli) on project dandelion: Exception caught
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:213)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:309)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:194)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:107)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:955)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:290)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:194)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
起初pom.xml中docker-maven-plugin的版本是1.0.0,使用mvn package docker:build -e
构建Image出现如上异常。
百般搜索无果,后来看其他的Spring Boot构建Docker镜像有用的是0.4.3,遂修改之。
又出现com.spotify:docker-maven-plugin 报localhost:2375 Connection refused 错误,搜索后又将docker-maven-plugin版本更新成0.4.13。
参考: [com.spotify:docker-maven-plugin 报localhost:2375 Connection refused 错误]
中间还设置过一次docker host,具体报什么错为什么这么加没找到 export DOCKER_HOST=unix:///var/run/docker.sock
参考
[1] 用 Docker 构建、运行、发布一个 Spring Boot 应用
[2] Mac平台上Docker安装与使用
[3] Docker Dockerfile 定制镜像
[4] docker-1.12 run命令运行以及参数详解
[5] [docker images 导入导出
[6] CentOS平台下的Docker安装与启动
[7] Docker 删除镜像