1、首先在本地安装docker,步骤可参考https://www.cnblogs.com/conswin/p/11055853.html
2、在对应项目目录下增加Dockerfile文件,其内容可参考如下
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
参数讲解:
FROM <image>:<tag> 需要一个基础镜像,可以是公共的或者是私有的, 后续构建会基于此镜像,如果同一个Dockerfile中建立多个镜像时,可以使用多个FROM指令
VOLUME 配置一个具有持久化功能的目录,主机 /var/lib/docker 目录下创建了一个临时文件,并链接到容器的/tmp。改步骤是可选的,如果涉及到文件系统的应用就很有必要了。/tmp目录用来持久化到 Docker 数据文件夹,因为 Spring Boot 使用的内嵌 Tomcat 容器默认使用/tmp作为工作目录
ARG 设置编译镜像时加入的参数, ENV 是设置容器的环境变量
COPY : 只支持将本地文件复制到容器 ,还有个ADD更强大但复杂点
ENTRYPOINT 容器启动时执行的命令
EXPOSE 8080 暴露镜像端口
并在pom.xml文件中增加如下:其中docker.image.prefix,finalName标签根据项目名称自定义
<properties> <docker.image.prefix>demo</docker.image.prefix> </properties> <build> <finalName>docker-demo</finalName> <plugins> <plugin> <groupId>com.spotify</groupId> <artifactId>dockerfile-maven-plugin</artifactId> <version>1.3.6</version> <configuration> <repository>${docker.image.prefix}/${project.artifactId}</repository> <buildArgs> <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE> </buildArgs> </configuration> </plugin> </plugins> </build>
3、右击选择项目-->run as-->maven build...,输入如下命令,进行打包
install dockerfile:build
观察控制台,出现如下信息,标识打包成功
[INFO]
[INFO] Step 1/5 : FROM openjdk:8-jdk-alpine
[INFO]
[INFO] Pulling from library/openjdk
[INFO] Digest: sha256:94792824df2df33402f201713f932b58cb9de94a0cd524164a0f2283343547b3
[INFO] Status: Image is up to date for openjdk:8-jdk-alpine
[INFO] ---> a3562aa0b991
[INFO] Step 2/5 : VOLUME /tmp
[INFO]
[INFO] ---> Using cache
[INFO] ---> a748cdf82dd0
[INFO] Step 3/5 : ARG JAR_FILE
[INFO]
[INFO] ---> Using cache
[INFO] ---> 67055e1963cf
[INFO] Step 4/5 : COPY ${JAR_FILE} app.jar
[INFO]
[INFO] ---> 6caed43f4c12
[INFO] Step 5/5 : ENTRYPOINT ["java","-jar","/app.jar"]
[INFO]
[INFO] ---> Running in 07aa2ea9d650
[INFO] Removing intermediate container 07aa2ea9d650
[INFO] ---> 2bfe8f5c564f
[INFO] Successfully built 2bfe8f5c564f
[INFO] Successfully tagged yunbo/yunbo-tm:latest
[INFO]
[INFO] Detected build of image with id 2bfe8f5c564f
[INFO] Building jar: D:\YunboSpace\yunbo_online\yunbo-tm\target\docker-tm-docker-info.jar
[INFO] Successfully built yunbo/yunbo-tm:latest
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 22.996s
[INFO] Finished at: Wed Sep 25 14:42:33 CST 2019
[INFO] Final Memory: 50M/579M
[INFO] ------------------------------------------------------------------------
4、本地查看镜像,可以发现已上传至本地docker中,执行如下命令,先登录远程阿里云镜像中心,然后将镜像推送过去
docker login [email protected] registry.cn-shanghai.aliyuncs.com
docker tag 2bfe8f5c564f registry.cn-shanghai.aliyuncs.com/junjun_space/junjun_store:demo-v1.0
docker push registry.cn-shanghai.aliyuncs.com/junjun_space/junjun_store:demo-v1.0
5、登录阿里云服务,进行镜像拉取,并启动
docker pull registry.cn-shanghai.aliyuncs.com/junjun_space/junjun_store:demo-v1.0
docker run -d --name demo-server -p 8761:8761 f6099ee6f4b6