Docker部署Spring-boot项目的示例代码

2020-06-17 06:52:40易采站长站整理
,这里配置了docker配置文件的目录,所以需要再
src/main
下面创建docker文件夹,同时创建
Dockerfile
文件。

目录机构如图:

docker配置文件结构.png

编辑

Dockerfile


FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD spring-docker.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

FROM 表示以Java8为基础镜像

VOLUME 表示挂载目录

ADD 拷贝打包文件并重命名为

app.jar

ENTRYPOINT 根据下面的官方文档解释大致是为了缩短tomcat启动时间而添加的一个系统属性。

We added a

VOLUME
pointing to
/tmp
because that is where a Spring Boot application creates working directories for Tomcat by default. The effect is to create a temporary file on your host under
/var/lib/docker
and link it to the container under
/tmp
. This step is optional for the simple app that we wrote here but can be necessary for other Spring Boot applications if they need to actually write in the filesystem.

To reduce Tomcat startup time we added a system property pointing to “/dev/urandom” as a source of entropy. This is not necessary with more recent versions of Spring Boot, if you use the “standard” version of Tomcat (or any other web server).

配置完成!

四、Docker启动Spring-boot

进入

module
执行:


$ mvn package docker:build

[INFO] Scanning for projects...

...

---> Running in e1f8aba72bdf
Removing intermediate container e1f8aba72bdf
---> 36a61c09f09a
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built 36a61c09f09a
Successfully tagged springboot/spring-docker:latest
[INFO] Built springboot/spring-docker
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.367 s
[INFO] Finished at: 2018-12-17T20:48:21+08:00
[INFO] ------------------------------------------------------------------------

查看镜像


$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
springboot/spring-docker latest 36a61c09f09a 2 minutes ago 123MB