将可运行的jar与dockerfile放在同一个目录,例如:
dockerfile内容如下:
## 使用最精简的jdk版本
FROM java:8-alpine
## 设置时区
RUN apk add -U tzdata
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
## 设置输出格式
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8
## 设置作者
##指定镜像作者信息,即镜像的Author属性
MAINTAINER saleson
## 添加jar 第一个参数是源jar, 第二个参数输出jar
## ADD的用法是:ADD 宿主机文件的全路径 docker容器下的文件夹路径
ADD istio-springboot-demo-a-1.0-SNAPSHOT.jar demo-a-app.jar
## 通知 Docker 容器在运行时监听指定的网络端口.EXPOSE
##暴露的端口更像是指明了该容器提供的服务需要用到的端口.
##EXPOSE 并不会直接将端口自动和宿主机某个端口建立映射关系
## 如果 docker run,指定了自动映射 -P,那么会将所有暴露的端口随机映射到宿主机的高阶端口
EXPOSE 8080
## 指定镜像的默认入口命令,该入口命令会在启动容器时作为根命令执行
ENTRYPOINT exec java -Xmx32m -Xms32m -Xmn32m -Xss256k -jar demo-a-app.jar
执行docker build命令打包镜像
% docker build -f dockerfile -t istio-springboot-demo-a:latest .
[+] Building 2.6s (9/9) FINISHED
=> [internal] load build definition from dockerfile 0.0s
=> => transferring dockerfile: 1.22kB 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/java:8-alpine 2.4s
=> [1/4] FROM docker.io/library/java:8-alpine@sha256:d49bf8c44670834d3dade17f8b84d709e7db47f1887f671a0e098bafa9bae49f 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 64B 0.0s
=> CACHED [2/4] RUN apk add -U tzdata 0.0s
=> CACHED [3/4] RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 0.0s
=> CACHED [4/4] ADD istio-springboot-demo-a-1.0-SNAPSHOT.jar demo-a-app.jar 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:5c794ac6ebad9b3b5e581ca642589ad3aa9ff96bf3e53883e31168e39ff52bd0 0.0s
=> => naming to docker.io/library/istio-sb-demo-a 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
% docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
istio-sb-demo-a latest 5c794ac6ebad 2 minutes ago 166MB
如果有parent, 且要打包的是sub module,需要先在parent的pom.xml添加plugin:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.spotifygroupId>
<artifactId>docker-maven-pluginartifactId>
<version>1.2.2version>
plugin>
plugins>
pluginManagement>
build>
不然会运行 mvn clean package docker:build 会maven 会提示 No plugin found for prefix ‘docker’ in the current project
如下:
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.948 s
[INFO] Finished at: 2022-07-21T17:24:01+08:00
[INFO] Final Memory: 13M/54M
[INFO] ------------------------------------------------------------------------
[ERROR] No plugin found for prefix 'docker' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (/Users/saleson/.m2/repository), nexus-aliyun (http://maven.aliyun.com/nexus/content/groups/public)] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoPluginFoundForPrefixException
在sub module的pom.xml中添加docker-maven-plugin
<build>
<plugins>
<defaultGoal>spring-boot:rundefaultGoal>
<plugin>
// spring-boot-maven-plugin
plugin>
<plugin>
<groupId>com.spotifygroupId>
<artifactId>docker-maven-pluginartifactId>
<configuration>
<imageName>${project.artifactId}:latestimageName>
<dockerDirectory>${project.basedir}/dockerdockerDirectory>
<resources>
<resource>
<targetPath>/targetPath>
<directory>${project.build.directory}directory>
<include>${project.build.finalName}.jarinclude>
resource>
resources>
configuration>
plugin>
plugins>
build>
在sub module 下新增dockerfile, 路径: docker/Dockerfile
Dockerfile 内容如下:
FROM java:8-alpine
## 设置时区
RUN apk add -U tzdata
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
## 设置输出格式
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8
MAINTAINER saleson
ADD istio-springboot-demo-a-1.0-SNAPSHOT.jar demo-a-app.jar
EXPOSE 8080
ENTRYPOINT exec java -Xmx32m -Xms32m -Xmn32m -Xss256k -jar demo-a-app.jar --spring.profiles.active=dev
运行mvn 命令执行打包:
mvn clean package docker:build -DskipTests
查看打包后的镜像
% docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
istio-springboot-demo-a latest 22f5d43a2563 6 seconds ago 166MB
所有的docker 打包配置都放在maven plugin的configuration中,配置如下:
<plugin>
<groupId>com.spotifygroupId>
<artifactId>docker-maven-pluginartifactId>
<executions>
<execution>
<id>build-imageid>
<phase>packagephase>
<goals>
<goal>buildgoal>
goals>
execution>
executions>
<configuration>
<imageName>demo-${project.artifactId}:${docker.image.version}imageName>
<baseImage>java:8-alpinebaseImage>
<maintainer>salesonmaintainer>
<runs>
<run>apk add -U tzdatarun>
<run>cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtimerun>
runs>
<env>
env>
<env>
<LANG>en_US.UTF-8LANG>
<LANGUAGE>en_US:enLANGUAGE>
<LC_ALL>en_US.UTF-8LC_ALL>
env>
<labels>
<label>usage=istio-demolabel>
labels>
<entryPoint>exec java -Xmx32m -Xms32m -Xmn32m -Xss256k -jar ${project.build.finalName}.jar --spring.profiles.active=deventryPoint>
<exposes>
<expose>8082expose>
exposes>
<resources>
<resource>
<targetPath>/targetPath>
<directory>${project.build.directory}directory>
<include>${project.build.finalName}.jarinclude>
resource>
resources>
configuration>
plugin>
使用docker run 命令运行镜像,如下:
% docker run -d -p 8080:8080 --name istio-springboot-demo-a istio-springboot-demo-a:latest
1855aa699105703ad83e2f7332a4c6fe2f1a0e1fd51ebe4017e7cc2058cf773e
查看pod运行情况:
% docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8bec7d566384 istio-springboot-demo-a:latest "/bin/sh -c 'exec ja…" 5 seconds ago Up 3 seconds 0.0.0.0:8080->8080/tcp istio-springboot-demo-a