Docker 部署 SpringBoot 的两种方法

  • 1.手工方式

  • 1.1.准备Springboot jar项目

  • 1.2.编写Dockerfile

  • 1.3.构建容器

  • 1.4.运行容器

  • 1.5.查看运行日志

  • 1.6.访问测试

  • 2.Docker远程连接并且使用idea一键部署

  • 2.1.配置docker远程连接端口

  • 2.2.使用idea连接到docker

  • 2.3.docker-maven-plugin 介绍


一、手工方式

1.1.准备Springboot jar项目

将项目打包成jar

1.2.编写Dockerfile

FROM java:8
VOLUME /tmp
ADD elk-web-1.0-SNAPSHOT.jar elk.jar
EXPOSE 8080
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/elk.jar"]

FROM:表示基础镜像,即运行环境
VOLUME /tmp创建/tmp目录并持久化到Docker数据文件夹,因为Spring Boot使用的内嵌Tomcat容器默认使用/tmp作为工作目录
ADD:拷贝文件并且重命名(ADD elk-web-1.0-SNAPSHOT.jar elk.jar 将应用jar包复制到/elk.jar)
EXPOSE:并不是真正的发布端口,即创建docker image的人员告诉容器布署人员容器应该映射哪个端口给外界
ENTRYPOINT:容器启动时运行的命令,相当于我们在命令行中输入java -jar xxxx.jar,为了缩短 Tomcat 的启动时间,添加java.security.egd的系统属性指向/dev/urandom作为ENTRYPOINT

1.3.构建容器

[root@VM_0_15_centos elk]# docker build -t elk .
Sending build context to Docker daemon 14.43 MB
Step 1/5 : FROM java:8
Trying to pull repository docker.io/library/java ...
8: Pulling from docker.io/library/java
5040bd298390: Pull complete
fce5728aad85: Pull complete
76610ec20bf5: Pull complete
60170fec2151: Pull complete
e98f73de8f0d: Pull complete
11f7af24ed9c: Pull complete
49e2d6393f32: Pull complete
bb9cdec9c7f3: Pull complete
Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
Status: Downloaded newer image for docker.io/java:8
 ---> d23bdf5b1b1b
Step 2/5 : VOLUME /tmp
 ---> Running in 0aec2dc2f98c
 ---> a52e844f25d4
Removing intermediate container 0aec2dc2f98c
Step 3/5 : ADD elk-web-1.0-SNAPSHOT.jar elk.jar
 ---> 3ba2f4fdddda
Removing intermediate container 860a0f748a23
Step 4/5 : EXPOSE 8080
 ---> Running in 1d3331cc2be6
 ---> e9ac33d26ce0
Removing intermediate container 1d3331cc2be6
Step 5/5 : ENTRYPOINT java -Djava.security.egd=file:/dev/./urandom -jar /elk.jar
 ---> Running in d354f8ee2af5
 ---> 8937e1ade6c7
Removing intermediate container d354f8ee2af5
Successfully built 8937e1ade6c7

1.4.运行容器

docker run -di --name 容器名称 -p 8080:8080 镜像名称

其中-d表示后台运行容器,这也就自然地解决的Spring Boot不支持后台运行应用程序的问题。-p 8080:8080表示将容器内部的8080端口映射到宿主机器的8080端口,这样就可以通过宿主机器直接访问应 用。--name 给容器取一个容易记住的名字方便日后管理。

[root@VM_0_15_centos elk]# docker run -di --name myspringboot -p 8080:8080 8937e1ade6c7
04d6b2c347950a10c95a039c94a3e51d717e516dd8c3c742e3197687dfcf5523
[root@VM_0_15_centos elk]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
04d6b2c34795        8937e1ade6c7        "java -Djava.secur..."   8 seconds ago       Up 7 seconds        0.0.0.0:8080->8080/tcp   myspringboot
[root@VM_0_15_centos elk]#

[1.5.查看运行日志

docker logs -f --tail=100 容器名称

[root@VM_0_15_centos elk]# docker logs -f --tail=100 04d6b2c34795
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.4.RELEASE)
2019-12-29 07:42:58.982  INFO 1 --- [           main] c.b.ElkExampleSpringBootApplication      : Starting ElkExampleSpringBootApplication v1.0-SNAPSHOT on 04d6b2c34795 with PID 1 (/elk.jar started by root in /)
2019-12-29 07:42:58.999  INFO 1 --- [           main] c.b.ElkExampleSpringBootApplication      : No active profile set, falling back to default profiles: default
2019-12-29 07:42:59.243  INFO 1 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5a2e4553: startup date [Sun Dec 29 07:42:59 UTC 2019]; root of context hierarchy
2019-12-29 07:43:03.652  INFO 1 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2019-12-29 07:43:03.699  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-12-29 07:43:03.714  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.15
2019-12-29 07:43:04.012  INFO 1 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-12-29 07:43:04.012  INFO 1 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 4772 ms
2019-12-29 07:43:04.449  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2019-12-29 07:43:04.470  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2019-12-29 07:43:04.470  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2019-12-29 07:43:04.471  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2019-12-29 07:43:04.471  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2019-12-29 07:43:05.534  INFO 1 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5a2e4553: startup date [Sun Dec 29 07:42:59 UTC 2019]; root of context hierarchy
2019-12-29 07:43:05.765  INFO 1 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/exception]}" onto public java.lang.String com.bruceliu.controller.ELKController.exception()
2019-12-29 07:43:05.766  INFO 1 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/elkdemo]}" onto public java.lang.String com.bruceliu.controller.ELKController.helloWorld()
2019-12-29 07:43:05.772  INFO 1 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2019-12-29 07:43:05.780  INFO 1 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2019-12-29 07:43:05.869  INFO 1 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-12-29 07:43:05.869  INFO 1 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-12-29 07:43:05.984  INFO 1 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-12-29 07:43:06.387  INFO 1 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2019-12-29 07:43:06.537  INFO 1 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2019-12-29 07:43:06.562  INFO 1 --- [           main] c.b.ElkExampleSpringBootApplication      : Started ElkExampleSpringBootApplication in 8.771 seconds (JVM running for 9.832)

1.6.访问测试

图片

二、Docker远程连接并且使用idea一键部署

2.1.配置docker远程连接端口

首先编辑我们服务器上的docker文件

vim /usr/lib/systemd/system/docker.service

修改以ExecStart开头的行(centos 7):添加

-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock \
图片

修改后保存文件,然后重启docker

systemctl daemon-reload
service docker restart

重启之后测试远程连接是否正常,这里的2375是之前配置的端口

curl http://localhost:2375/version

看到返回信息基本上就没有问题了.

[root@VM_0_15_centos elk]# curl http://localhost:2375/version
{"Version":"1.13.1","ApiVersion":"1.26","MinAPIVersion":"1.12","GitCommit":"7f2769b/1.13.1","GoVersion":"go1.10.3","Os":"linux","Arch":"amd64","KernelVersion":"3.10.0-957.21.3.el7.x86_64","BuildTime":"2019-09-15T14:06:47.565778468+00:00","PkgVersion":"docker-1.13.1-103.git7f2769b.el7.centos.x86_64"}

然后开启端口,或者关闭防火墙,二者选其一即可

firewall-cmd --zone=public --add-port=2375/tcp --permanent
chkconfig iptables off

然后打开浏览器测试将之前的localhost修改为你的ip

图片

2.2.使用idea连接到docker

首先下载docker插件,idea 自带了docker插件。如果没有插件可以选择安装docker插件

图片

然后配置docker地址,在你的File | Settings | Build, Execution, Deployment | Docker

图片

配置完成链接之后,出现了框中的内容即可.

图片

链接成功之后会列出容器和镜像!

配置阿里云镜像加速器:

图片

2.3.docker-maven-plugin 介绍

在我们持续集成过程中,项目工程一般使用 Maven 编译打包,然后生成镜像,通过镜像上线,能够大大提供上线效率,同时能够快速动态扩容,快速回滚,着实很方便。docker-maven-plugin 插件就是为了帮助我们在Maven工程中,通过简单的配置,自动生成镜像并推送到仓库中。

pom.xml:

<build>

        <finalName>${project.artifactId}</finalName>

        <plugins>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
            <!-- 跳过单元测试 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>

            <!--使用docker-maven-plugin插件-->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.0.0</version>
                <!--将插件绑定在某个phase执行-->
                <executions>
                    <execution>
                        <id>build-image</id>
                        <!--用户只需执行mvn package ,就会自动执行mvn docker:build-->
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!--指定生成的镜像名-->
                    <imageName>bruceliu/${project.artifactId}</imageName>
                    <!--指定标签-->
                    <imageTags>
                        <imageTag>latest</imageTag>
                    </imageTags>
                    <!--指定基础镜像jdk1.8-->
                    <baseImage>java</baseImage>
                    <!--镜像制作人本人信息-->
                    <maintainer>bruceliu@email.com</maintainer>
                    <!--切换到ROOT目录-->
                    <workdir>/ROOT</workdir>
                    <cmd>["java", "-version"]</cmd>
                    <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
                    <!--指定远程 docker api地址-->
                    <dockerHost>http://122.51.50.249:2375</dockerHost>
                    <!-- 这里是复制 jar 包到 docker 容器指定目录配置 -->
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <!--jar 包所在的路径  此处配置的 即对应 target 目录-->
                            <directory>${project.build.directory}</directory>
                            <!--用于指定需要复制的文件 需要包含的 jar包 ,这里对应的是 Dockerfile中添加的文件名 -->
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>

        </plugins>
    </build>

执行Maven打包命令:

图片
G:\softDevelopment\JDK8\bin\java -Dmaven.multiModuleProjectDirectory=E:\workspace2017\elk-web -Dmaven.home=E:\Maven20190910\apache-maven-3.6.1 -Dclassworlds.conf=E:\Maven20190910\apache-maven-3.6.1\bin\m2.conf "-javaagent:G:\idea2017\IntelliJ IDEA 2017.3.1\lib\idea_rt.jar=49260:G:\idea2017\IntelliJ IDEA 2017.3.1\bin" -Dfile.encoding=UTF-8 -classpath E:\Maven20190910\apache-maven-3.6.1\boot\plexus-classworlds-2.6.0.jar org.codehaus.classworlds.Launcher -Didea.version=2017.3.7 -s E:\Maven20190910\apache-maven-3.6.1\conf\settings.xml -Dmaven.repo.local=E:\Maven20190910\repository package
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.bruceliu.elk.demo:elk-web:jar:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.(groupId:artifactId)' must be unique but found duplicate declaration of plugin org.springframework.boot:spring-boot-maven-plugin @ line 36, column 21
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] -------------------< com.bruceliu.elk.demo:elk-web >--------------------
[INFO] Building elk-web 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ elk-web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ elk-web ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to E:\workspace2017\elk-web\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ elk-web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\workspace2017\elk-web\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ elk-web ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ elk-web ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ elk-web ---
[INFO] Building jar: E:\workspace2017\elk-web\target\elk-web.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:1.5.4.RELEASE:repackage (default) @ elk-web ---
[INFO]
[INFO] --- docker-maven-plugin:1.0.0:build (build-image) @ elk-web ---
[INFO] Using authentication suppliers: [ConfigFileRegistryAuthSupplier]
[INFO] Copying E:\workspace2017\elk-web\target\elk-web.jar -> E:\workspace2017\elk-web\target\docker\elk-web.jar
[INFO] Building image bruceliu/elk-web
Step 1/6 : FROM java
 ---> d23bdf5b1b1b
Step 2/6 : MAINTAINER bruceliu@email.com
 ---> Running in 787e4786fbd4
 ---> 4d4519f52fda
Removing intermediate container 787e4786fbd4
Step 3/6 : WORKDIR /ROOT
 ---> f40dcbc9a9eb
Removing intermediate container 7fa6bbc9d1df
Step 4/6 : ADD /elk-web.jar //
 ---> c7f1107ae3d4
Removing intermediate container f370558f1a38
Step 5/6 : ENTRYPOINT java -jar /elk-web.jar
 ---> Running in e4480ced0829
 ---> b634ca5fa5ad
Removing intermediate container e4480ced0829
Step 6/6 : CMD java -version
 ---> Running in cc6a064ef921
 ---> cf9a5d50326b
Removing intermediate container cc6a064ef921
Successfully built cf9a5d50326b
[INFO] Built bruceliu/elk-web
[INFO] Tagging bruceliu/elk-web with latest
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  10.329 s
[INFO] Finished at: 2019-12-29T22:44:06+08:00
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,921评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,635评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,393评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,836评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,833评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,685评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,043评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,694评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,671评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,670评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,779评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,424评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,027评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,984评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,214评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,108评论 2 351
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,517评论 2 343

推荐阅读更多精彩内容