1. 构建spring boot微服务项目

    image.png

    1. 打包应用

    image.png

    1. 下载插件Docker 编写dockerfile文件时会有提示

    image.png

    1. 编写dockerfile

    新建Dockerfile文件
    image.png
    HelloController.java

    1. package com.example.demo.controller;
    2. import org.springframework.web.bind.annotation.RequestMapping;
    3. import org.springframework.web.bind.annotation.RestController;
    4. /**
    5. * @author lwc
    6. * @date 2021/6/15 14:26
    7. */
    8. @RestController
    9. public class HelloController {
    10. @RequestMapping("/hello")
    11. public String hello(){
    12. return "hello world!";
    13. }
    14. }

    Dockerfile 文件内容

    1. FROM java:8
    2. COPY *.jar /app.jar
    3. CMD ["--server.port=8080"]
    4. EXPOSE 8080
    5. ENTRYPOINT ["java","-jar","/app.jar"]
    1. 构建镜像

    将jar包和Dockerfile文件拷贝搭配Linux里
    打包

    1. [root@localhost idea]# ll
    2. 总用量 16924
    3. -rwxr-xr-x. 1 root root 17322059 6 15 15:03 demo-0.0.1-SNAPSHOT.jar
    4. -rwxr-xr-x. 1 root root 122 6 15 15:03 Dockerfile
    5. [root@localhost idea]# docker build -t demo .
    6. Sending build context to Docker daemon 17.33MB
    7. Step 1/5 : FROM java:8
    8. 8: Pulling from library/java
    9. 5040bd298390: Pull complete
    10. fce5728aad85: Pull complete
    11. 76610ec20bf5: Pull complete
    12. 60170fec2151: Pull complete
    13. e98f73de8f0d: Pull complete
    14. 11f7af24ed9c: Pull complete
    15. 49e2d6393f32: Pull complete
    16. bb9cdec9c7f3: Pull complete
    17. Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
    18. Status: Downloaded newer image for java:8
    19. ---> d23bdf5b1b1b
    20. Step 2/5 : COPY *.jar /app.jar
    21. ---> 9dc5ea8ee18b
    22. Step 3/5 : CMD ["--server.port=8080--"]
    23. ---> Running in d6d78510fd83
    24. Removing intermediate container d6d78510fd83
    25. ---> 06d47f8d73d7
    26. Step 4/5 : EXPOSE 8080
    27. ---> Running in 59b21172a8ab
    28. Removing intermediate container 59b21172a8ab
    29. ---> 8aa69ebbc81d
    30. Step 5/5 : ENTRYPOINT ["java",".jar","/app.jar"]
    31. ---> Running in f3679964c87b
    32. Removing intermediate container f3679964c87b
    33. ---> 05d657eb87a6
    34. Successfully built 05d657eb87a6
    35. Successfully tagged demo:latest
    36. [root@localhost idea]# docker images
    37. REPOSITORY TAG IMAGE ID CREATED SIZE
    38. demo latest 05d657eb87a6 57 seconds ago 661MB
    39. java 8 d23bdf5b1b1b 4 years ago 643MB
    1. 发布
      # 运行
      [root@localhost idea]# docker run -d -p 8080:8080 demo
      ca4b72bcd87c42b43560bc502ff154bc6f0c7476f8f277d67e8008f501171805
      [root@localhost idea]# docker ps
      CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                                       NAMES
      ca4b72bcd87c   demo      "java -jar /app.jar …"   7 seconds ago   Up 6 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   charming_moser
      [root@localhost idea]#
      
      image.png
      以后使用Docker后,给别人交付镜像就可以了。

    注意:
    这里只是简单演示了将项目整体打包构建的镜像的过程,如果需要将项目切分成很多个layer ,多个项目的依赖可以进行复用,让我们发布的镜像大小精简可以参考这篇博客https://phauer.com/2019/no-fat-jar-in-docker-image/