The java (springboot) packaged mirror docker

Introduction: To docker run java (jar package) program, it is packaged into a docker program should mirror image (hereinafter referred to as mirroring), can be understood as the first image is the jar package

 

Packing required program code, java packaging environment itself (including jdk and maven), and docker, thus in this paper installed above ambient environment (particularly installed Docker) packaged

 

First of all to modify the code


 

Use ewater framework development program, code files need to be modified in order to be packaged into a docker mirror

 

In this path of adding a file named Dockerfile

 

 

Says:

FROM openjdk:8-jdk-alpine

VOLUME /tmp

ADD ewater-0.0.1.jar app.jar

ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

PS: on line 3 "ewater-0.0.1.jar" Yaoan maven package actually modify the output file

 

 

 

Modify pom file

 

 

<docker.image.prefix>springio</docker.image.prefix>

 

 

 

            <plugin>

                <groupId>com.spotify</groupId>

                <artifactId>docker-maven-plugin</artifactId>

                <version>0.4.14</version>

                <configuration>

                    <imageName>${docker.image.prefix}/${project.artifactId}</imageName>

                    <!--指定docker镜像的版本号-->

                    <imageTags>

                        <!--使用maven项目的版本号-->

                        <imageTag>${project.version}</imageTag>

                        <imageTag>latest</imageTag>

                    </imageTags>

                    <dockerDirectory>src/main/docker</dockerDirectory>

                    <resources>

                        <resource>

                            <targetPath>/</targetPath>

                            <directory>${project.build.directory}</directory>

                            <include>${project.build.finalName}.jar</include>

                        </resource>

                    </resources>

                </configuration>

            </plugin>

 

 

修改代码完毕,接原文


 

 

然后把代码拷到打包的电脑,cd进代码目录(pom文件所在目录)

-P dev-docker

运行mvn package -P dev-docker docker:build,把程序打包为镜像。第一次打包会久一些

PS:参数-P是指定打包使用的profile名称,不一定需要

PS:成功的样子

 

 

 

运行docker image ls,列出所有本地镜像,可以看到刚才打包的

 

 

PS:为什么一个镜像有两行?先不展开,注意他们tag不一样

 

镜像打包到此完成

 


 

下面还扩展一下:在docker以镜像方式运行java程序

 

Dokcer可以单独使用,k8s不是必须的,在有k8s之前docker就是单独使用

 

本文假设java程序的镜像已经打包好且在本地,运行的是以下镜像

 

 

 

运行docker run -p 20801:20801 -t springio/ewater,启动名为springio/ewater的镜像

端口:两个20801代表http端口,因为本java程序是springboot,springmvc开发,有http请求功能,所以需要端口

PS:启动时有命令行输出,内容跟直接运行jar包差不多

 

 

 

到此启动成功,可以在浏览器访问里面的http请求来测试

PS:ip是master1的ip,端口是docker run指定的端口

 

 

 

扩展:如何停止运行中的镜像?

首先科普个新名称——容器(container),镜像运行时叫容器,一个镜像可以开启运行多个容器(就像一个jar包可以同时在多个tomcat运行)

因此问题应该是如何停止运行中的容器

 

运行docker ps,可见所有运行中的容器

 

 

 

CONTAINER ID是容易的唯一id

 

然后有以下的命令:

停止运行容器(fca333c9ce5f代表容器id)

docker stop fca333c9ce5f

删除容器

docker rm fca333c9ce5

Guess you like

Origin www.cnblogs.com/cannel/p/11104224.html