Dockerfile构建Springboot镜像

Dockerfile构建Springboot镜像

文章目录

  • Dockerfile构建Springboot镜像
    • 简介
    • 实例演示
      • 前期准备
        • Docker环境
        • Springboot项目
        • Dockerfile文件
      • Windows
        • 要求
        • 构建镜像
        • 启动测试
      • Linux
        • 要求
        • 构建镜像
        • 启动测试

简介

容器技术大流行的时代,也是docker大流行的时代。

此文章以一个简单的实例为基础,帮助大家学习和了解:如何使用Dockerfile指令构建一个Springboot的Java项目镜像。

提示:实例非常简单,旨在帮助大家了解基础知识,本文的知识在实际工作中肯定是不够用的,还需要自行根据实际情况去学习,感谢您的阅读。

实例演示

  • 具体操作介绍了WindowsLinux两种方式。

前期准备

Docker环境

  • Windows请安装Docker Desktop:https://docs.docker.com/desktop/install/windows-install/

  • Linux请安装Docker:https://docs.docker.com/desktop/install/linux-install/

Springboot项目

  • 源码:learn/java/docker-springboot at main · DingDangDog/learn (github.com)

  • Jar包下载地址:docker-springboot-1.0.jar.zip

  • Controller代码示例:

package io.github.dingdangdog.controller;
import io.github.dingdangdog.utils.FileUtilOm;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * say hello
 *
 * @author DingDangDog
 * @since 2022/10/4 11:13
 */
@RestController
@RequestMapping("/say")
public class SayController {
    @GetMapping("/hello")
    public String hello(String name) {
        System.out.println(name);
        // 保存文件
        FileUtilOm.saveFile("/tmp/", name, name);
        return "Hello " + name + " !";
    }
}

Dockerfile文件

  • 文件名必须为Dockerfile,无拓展名!!!请放在jar包同级目录下。
# 基础镜像
FROM openjdk:8-jdk
# 作者声明,原为MAINTAINER,变更为LABEL
# MAINTAINER DingDangDog
LABEL author="DingDangDog"
# 容器卷设置
VOLUME /tmp
# 向基础镜像内添加文件,并重命名为`docker_boot.jar`
add docker-springboot-1.0.jar docker_boot.jar
# 执行前置脚本。据说是修改文件时间?不太明白具体作用,望有大佬指点迷津!
RUN bash -c 'touch /docker_boot.jar'
# 最终执行脚本
ENTRYPOINT ["java", "-jar", "/docker_boot.jar"]
# 暴露端口
EXPOSE 10240

指令解析请参考文章:Dockerfile基础一文通

Windows

要求

  1. 前期准备

  2. Windows可以正常启动Docker服务:

如下图,运行状态绿色为正在运行
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kYndCTFA-1670767109761)(https://github.com/DingDangDog/ddd-document/blob/main/docker/images/image-20221004122548749.png?raw=true)]

构建镜像

  • 在jar包所在文件夹下打开cmd命令行窗口,运行构建命令:

    docker build -t docker_boot:1.0 .
    

    命令解析:

    • docker build:构建镜像
    • -t docker_boot:1.0-t=--tag,指定镜像名称为docker_boot,版本为1.0
    • . :指定Dockerfile文件在当前文件夹下
  • 构建成功日志如下:

    [+] Building 76.3s (9/9) FINISHED
     => [internal] load build definition from Dockerfile                                                               0.0s
     => => transferring dockerfile: 254B                                                                               0.0s
     => [internal] load .dockerignore                                                                                  0.0s
     => => transferring context: 2B                                                                                    0.0s
     => [internal] load metadata for docker.io/library/openjdk:8-jdk                                                   4.6s
     => [auth] library/openjdk:pull token for registry-1.docker.io                                                     0.0s
     => [internal] load build context                                                                                  0.1s
     => => transferring context: 19.65MB                                                                               0.1s
     => [1/3] FROM docker.io/library/openjdk:8-jdk@sha256:86e863cc57215cfb181bd319736d0baf625fe8f150577f9eb58bd937f5  70.8s
     => => resolve docker.io/library/openjdk:8-jdk@sha256:86e863cc57215cfb181bd319736d0baf625fe8f150577f9eb58bd937f54  0.0s
     => => sha256:86e863cc57215cfb181bd319736d0baf625fe8f150577f9eb58bd937f5452cb8 1.04kB / 1.04kB                     0.0s
     => => sha256:3af2ac94130765b73fc8f1b42ffc04f77996ed8210c297fcfa28ca880ff0a217 1.79kB / 1.79kB                     0.0s
     => => sha256:001c52e26ad57e3b25b439ee0052f6692e5c0f2d5d982a00a8819ace5e521452 55.00MB / 55.00MB                  24.0s
     => => sha256:2068746827ec1b043b571e4788693eab7e9b2a95301176512791f8c317a2816a 10.88MB / 10.88MB                  13.1s
     => => sha256:b273004037cc3af245d8e08cfbfa672b93ee7dcb289736c82d0b58936fb71702 7.81kB / 7.81kB                     0.0s
     => => sha256:d9d4b9b6e964657da49910b495173d6c4f0d9bc47b3b44273cf82fd32723d165 5.16MB / 5.16MB                    18.0s
     => => sha256:9daef329d35093868ef75ac8b7c6eb407fa53abbcb3a264c218c2ec7bca716e6 54.58MB / 54.58MB                  43.2s
     => => sha256:d85151f15b6683b98f21c3827ac545188b1849efb14a1049710ebc4692de3dd5 5.42MB / 5.42MB                    25.5s
     => => sha256:52a8c426d30b691c4f7e8c4b438901ddeb82ff80d4540d5bbd49986376d85cc9 210B / 210B                        25.3s
     => => extracting sha256:001c52e26ad57e3b25b439ee0052f6692e5c0f2d5d982a00a8819ace5e521452                          1.9s
     => => sha256:8754a66e005039a091c5ad0319f055be393c7123717b1f6fee8647c338ff3ceb 105.92MB / 105.92MB                68.4s
     => => extracting sha256:d9d4b9b6e964657da49910b495173d6c4f0d9bc47b3b44273cf82fd32723d165                          0.2s
     => => extracting sha256:2068746827ec1b043b571e4788693eab7e9b2a95301176512791f8c317a2816a                          0.2s
     => => extracting sha256:9daef329d35093868ef75ac8b7c6eb407fa53abbcb3a264c218c2ec7bca716e6                          2.1s
     => => extracting sha256:d85151f15b6683b98f21c3827ac545188b1849efb14a1049710ebc4692de3dd5                          0.2s
     => => extracting sha256:52a8c426d30b691c4f7e8c4b438901ddeb82ff80d4540d5bbd49986376d85cc9                          0.0s
     => => extracting sha256:8754a66e005039a091c5ad0319f055be393c7123717b1f6fee8647c338ff3ceb                          2.1s
     => [2/3] ADD docker-springboot-1.0.jar docker_boot.jar                                                            0.3s
     => [3/3] RUN bash -c 'touch /docker_boot.jar'                                                                     0.4s
     => exporting to image                                                                                             0.1s
     => => exporting layers                                                                                            0.1s
     => => writing image sha256:0fcc9c262e90d6a05213ec432924e6835647d233a64ad0747cf7dd406a2a40bd                       0.0s
     => => naming to docker.io/library/docker_boot:1.0                                                                 0.0s
    
    Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
    
  • 查看镜像

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-n3Vw2A5r-1670767109762)(https://github.com/DingDangDog/ddd-document/blob/main/docker/images/image-20221004123749354.png?raw=true)]

启动测试

  1. 启动容器

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-92J0OcAd-1670767109763)(https://github.com/DingDangDog/ddd-document/blob/main/docker/images/image-20221004124258074.png?raw=true)]

  1. 调用测试

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bNIzrXeA-1670767109764)(https://github.com/DingDangDog/ddd-document/blob/main/docker/images/image-20221004124524928.png?raw=true)]

  1. 查看本地文件:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UER0fLaz-1670767109764)(https://github.com/DingDangDog/ddd-document/blob/main/docker/images/image-20221004124636663.png?raw=true)]

Linux

要求

  • 前期准备

构建镜像

  • 在jar包所在文件夹下运行构建命令:

    docker build -t docker_boot:1.0 .
    

    解析:

    • docker build:构建镜像

    • -t docker_boot:1.0-t=--tag,指定镜像名称为docker_boot,版本为1.0

    • . :指定Dockerfile文件在当前文件夹下

  • 构建成功日志:

    Sending build context to Docker daemon  19.65MB
    Step 1/7 : FROM openjdk:8-jdk
    8-jdk: Pulling from library/openjdk
    001c52e26ad5: Pull complete 
    d9d4b9b6e964: Pull complete 
    2068746827ec: Pull complete 
    9daef329d350: Pull complete 
    d85151f15b66: Pull complete 
    52a8c426d30b: Pull complete 
    8754a66e0050: Pull complete 
    Digest: sha256:86e863cc57215cfb181bd319736d0baf625fe8f150577f9eb58bd937f5452cb8
    Status: Downloaded newer image for openjdk:8-jdk
     ---> b273004037cc
    Step 2/7 : MAINTAINER DingDangDog
     ---> Running in 6b19d92591a8
    Removing intermediate container 6b19d92591a8
     ---> 8a491c04d897
    Step 3/7 : VOLUME /tmp
     ---> Running in 696221f7868c
    Removing intermediate container 696221f7868c
     ---> ad7eff7b71e1
    Step 4/7 : add docker-springboot-1.0.jar docker_boot.jar
     ---> 77eb15fd7557
    Step 5/7 : RUN bash -c 'touch /docker_boot.jar'
     ---> Running in 0704d4fd8ab2
    Removing intermediate container 0704d4fd8ab2
     ---> aa9e4ca18365
    Step 6/7 : ENTRYPOINT ["java", "-jar", "/docker_boot.jar"]
     ---> Running in 78ae0e314f8f
    Removing intermediate container 78ae0e314f8f
     ---> cb6f8c91f76c
    Step 7/7 : EXPOSE 10240
     ---> Running in 697a7506c8c8
    Removing intermediate container 697a7506c8c8
     ---> 20bafbe3127b
    Successfully built 20bafbe3127b
    Successfully tagged docker_boot:1.0
    
  • 查看镜像docker images

启动测试

  1. 启动容器

    docker run -v /var/local/docker/docker-boot-test/tmp:/tmp -p 10240:10240 -d --name docker_boot_test docker_boot:1.0
    
  2. 调用测试

    curl http://localhost:10240/say/hello?name=ddd
    # Hello ddd !
    
  3. 查看本地文件:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yRFbSyEu-1670767109765)(https://github.com/DingDangDog/ddd-document/blob/main/docker/images/image-20221004130337329.png?raw=true)]

你可能感兴趣的:(docker,docker,Dockerfile)