当前位置 博文首页 > 阳阳的博客:Docker构建SpringBoot应用

    阳阳的博客:Docker构建SpringBoot应用

    作者:[db:作者] 时间:2021-08-15 10:04

    1.基于Dockerfile构建SpringBoot镜像

    1.1准备工作


    ? ? ? ?将SpringBoot项目通过maven打成jar包:? mvn clean package?

    1.2使用Dockerfile构建镜像

    • step1 在存放jar所在目录下创建Dockerfile文件
    touch Dockerfile
    • step2 编辑Dockerfile增加以下内容
    FROM java:8
    MAINTAINER ?niugang<863263957@qq.com>
    RUN mkdir -p /opt/springapp
    ADD ? demo-0.0.1.jar ?/opt/springapp
    EXPOSE 8088
    ENTRYPOINT ["java","-jar","/opt/springapp/demo-0.0.1.jar"]
    • step3 构建镜像
    docker build -t springbootdemo:1.0 .
    • step4 启动容器
    docker run -d ?-p 8088:8088 --name sb springbootdemo:1.0?

    在启动容器是可以添加数据卷,程序日志映射到宿主机,方便后期排查问题

    注意:在启动过程中,一直起不起来,然后通过前台启动查看了日志。

    报了如下错误:WARNING: IPv4 forwarding is disabled. Networking will not work.

    解决:需要做如下配置

    vim/etc/sysctl.conf
    
    net.ipv4.ip_forward=1 #添加这段代码

    重启network服务

    systemctl restart network && systemctl restart docker

    查看是否修改成功 (备注:返回1,就是成功)

    [root@docker-node2 ~]# sysctl net.ipv4.ip_forward
    net.ipv4.ip_forward = 1
    • step5 调用你的springboot应用,验证其是否正确

    2.使用Maven插件构建Docker镜像

    Maven是一个强大的项目管理与构建工具。如果可以使用Maven构建Docker镜像,以下几款Maven的Docker插件比较常用。

    插件名称 官方地址? docker-maven-plugin https://github.com/spotify/docker-maven-plugin

    上面插件可以从github看到此插件已经不再推荐了。推荐使用

    https://github.com/spotify/dockerfile-maven 具体操作步骤上面有介绍

    docker-maven-plugin https://github.com/fabric8io/docker-maven-plugin
    docker-maven-plugin https://github.com/bibryam/docker-maven-plugin

    2.1直接配置方式

    • step 1

    在你的springboot应用的pom.xml中增加如下配置

    ?<plugin>
    ? ? ? ? ? ? ? ? <groupId>com.spotify</groupId>
    ? ? ? ? ? ? ? ? <artifactId>docker-maven-plugin</artifactId>
    ? ? ? ? ? ? ? ? <version>0.4.13</version>
    ? ? ? ? ? ? ? ? <configuration>
    ? ? ? ? ? ? ? ? ? ? <!--用于指定镜像名称-->
    ? ? ? ? ? ? ? ? ? ? <imageName>niugang/springboot:3.0</imageName>
    ? ? ? ? ? ? ? ? ? ? <!--用于指定基础镜像,类似于Dockerfile中的FROM指令。-->
    ? ? ? ? ? ? ? ? ? ? <baseImage>java</baseImage>
    ? ? ? ? ? ? ? ? ? ? <!--类似于Dockerfile的ENTRYPOINT指令-->
    ? ? ? ? ? ? ? ? ? ? <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
    ? ? ? ? ? ? ? ? ? ? <resources>
    ? ? ? ? ? ? ? ? ? ? ? ? <resource>
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? <!--将打包后的资源文件复制到该目录-->
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? <targetPath>/</targetPath>
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? <!--需要复制的文件所在目录,maven打包的应用jar包保存在target目录下面-->
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? <directory>${project.build.directory}</directory>
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? <!--需要复制的文件,打包好的应用jar包。-->
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? <include>${project.build.finalName}.jar</include>
    ? ? ? ? ? ? ? ? ? ? ? ? </resource> ??
    ? ? ? ? ? ? ? ? ? ? </resources>
    ? ? ? ? ? ? ? ? </configuration>
    ? ? ? ? ? ? </plugin>

    简要说明一下插件的配置:

    1. imageName:用于指定镜像名称,其中niugang是仓库名称,springboot是镜像名称,3.0是标签名称
    2. baseImage:用于指定基础镜像,类似于Dockerfile中的FROM指令。
    3. entrypoint:类似于Dockerfile的ENTRYPOINT指令。
    4. resources.resource.directory:用于指定需要复制的根目录,${project.build.directory}表示target目录。
    5. resources.resource.include:用于指定需要复制的文件。${project.build.finalName}.jar指的是打包后的jar包文件。
    • step2 进入代码路径下,执行如下命令
    mvn clean package docker:build
    • step3 查看镜像

    • step4 启动容器
    docker run ?-d -p 8088:8088 --name myspringboot niugang/springboot:3.0
    • step5 访问测试

    2.2 读取Dockerfile进行构建

    在2.1中只需要在pom.xm中配置,执行命令就可以生成docker镜像。在很多时候希望通过Dockerfile生成Docker镜像。

    • step1 在项目src/main/resources目录下新建docker文件夹,在文件夹下新建Dockerfile文件,增加如下内容
    FROM java:8
    MAINTAINER niugang<863263957@qq.com>
    RUN ?mkdir -p /docker/images
    ADD demo-0.0.1-SNAPSHOT.jar /docker/images
    ENTRYPOINT ["java","-jar","/docker/images/demo-0.0.1-SNAPSHOT.jar"]
    • step2 pom.xml修改为如下

    ?<plugin>
    ? ? ? ? ? ? ? ? <groupId>com.spotify</groupId>
    ? ? ? ? ? ? ? ? <artifactId>docker-maven-plugin</artifactId>
    ? ? ? ? ? ? ? ? <version>0.4.13</version>
    ? ? ? ? ? ? ? ? <configuration>
    ? ? ? ? ? ? ? ? ? ? <!--用于指定镜像名称-->
    ? ? ? ? ? ? ? ? ? ? <imageName>niugang/springboot:4.0</imageName> ? ? ? ? ? ? <dockerDirectory>${project.basedir}/src/main/resources/docker</dockerDirectory>
    ? ? ? ? ? ? ? ? ? ? <resources>
    ? ? ? ? ? ? ? ? ? ? ? ? <resource>
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? <!--将打包后的资源文件复制到该目录-->
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? <targetPath>/</targetPath>
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? <!--需要复制的文件所在目录,maven打包的应用jar包保存在target目录下面-->
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? <directory>${project.build.directory}</directory>
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? <!--需要复制的文件,打包好的应用jar包。-->
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? <include>${project.build.finalName}.jar</include>
    ? ? ? ? ? ? ? ? ? ? ? ? </resource> ??
    ? ? ? ? ? ? ? ? ? ? </resources>
    ? ? ? ? ? ? ? ? </configuration>
    ? ? ? ? ? ? </plugin>?

    可以看到,不再制定baseImage和entrypoint,而是使用dockerDirectory指定Dockerfile所在路径。即时指定了baseImage和entrypoint也将被忽略。

    • step3 进入代码路径下,执行如下命令
    mvn clean package docker:build

    查看镜像

    docker images
    • step4 启动容器
    docker run ?-d -p 8088:8088 --name myspringboot niugang/springboot:4.0
    • step5 访问测试

    2.3 将插件绑定在某个phase执行

    很多场景下,我们有这样的需求,执行例如 mvn clean package 时,插件就自动为我们构建Docker镜像。要想实现这点,我们只需将插件的goal绑定在某个phase即可。

    phase和goal可以这样理解:maven命令格式是: mvn phase:goal ,例如 mvnpackagedocker:build 。那么,package和docker都是phase,build则是goal。

    • step1 编辑pom.xml 内容如下
    <plugin>
    ? ? ? ? ? ? ? ? <groupId>com.spotify</groupId>
    ? ? ? ? ? ? ? ? <artifactId>docker-maven-plugin</artifactId>
    ? ? ? ? ? ? ? ? <version>0.4.13</version>
    ? ? ? ? ? ? ? ?<executions>
    ? ? ? ? ? ? ? ? ? ?<execution>
    ? ? ? ? ? ? ? ? ? ? ? ?<id>build-images</id>
    ? ? ? ? ? ? ? ? ? ? ? ?<phase>package</phase>
    ? ? ? ? ? ? ? ? ? ? ? ?<goals>
    ? ? ? ? ? ? ? ? ? ? ? ? ? ?<goal>build</goal>
    ? ? ? ? ? ? ? ? ? ? ? ?</goals>
    ? ? ? ? ? ? ? ? ? ?</execution>
    ? ? ? ? ? ? ? ?</executions>
    ? ? ? ? ? ? ? ? <configuration>
    ? ? ? ? ? ? ? ? ? ? <!--用于指定镜像名称-->
    ? ? ? ? ? ? ? ? ? ? <imageName>niugang/springboot:5.0</imageName> ? ? ? ? ? ? ? ? ? ?<dockerDirectory>${project.basedir}/src/main/resources/docker</dockerDirectory>
    ? ? ? ? ? ? ? ? ? ? <resources>
    ? ? ? ? ? ? ? ? ? ? ? ? <resource>
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? <!--将打包后的资源文件复制到该目录-->
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? <targetPath>/</targetPath>
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? <!--需要复制的文件所在目录,maven打包的应用jar包保存在target目录下面-->
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? <directory>${project.build.directory}</directory>
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? <!--需要复制的文件,打包好的应用jar包。-->
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? <include>${project.build.finalName}.jar</include>
    ? ? ? ? ? ? ? ? ? ? ? ? </resource>?
    ? ? ? ? ? ? ? ? ? ? </resources>
    ? ? ? ? ? ? ? ? </configuration>
    ? ? ? ? ? ? </plugin>
    • step2进入代码路径下,执行如下命令
    mvn clean package
    • step3 查看镜像

    由配置可知,我们只需添加如下配置:

    <executions>
    ? <execution>
    ? ? <id>build-image</id>
    ? ? <phase>package</phase>
    ? ? <goals>
    ? ? ? <goal>build</goal>
    ? ? </goals>
    ? </execution>
    </executions>

    就可将插件绑定在package这个phase上。也就是说,用户只需执行 mvn package ,就会自动执行 mvn docker:build 。当然,读者也可按照需求,将插件绑定到其他的phase。

    cs