Dockerfile构建SpringBoot项目

【1】将SpringBoot项目打包

  • jar包-->用JDK运行(我们这里打成jar包)
  • war包-->用Tomcat运行

MySQL的url配置

1.useSSL=false  MySQL 8.0 以上版本不需要建立 SSL 连接的,需要显示关闭
2.allowPublicKeyRetrieval=true 允许客户端从服务器获取公钥。
3.serverTimezone=UTC 设置时区,mysql驱动8.0+也要指定时区,不然也会报一些错

如果使用的mysql是8.0+,建议最好把以上三个参数设置下,避免发生一些莫名错误。

【2】Linux服务器创建一个空目录

mkdir spring-boot-start

【3】将jar上传到Linux服务器的该目录下

工具随意:我用Xftp

【4】基于Docker要构建一个MySQL应用

SpringBoot项目需要使用MySQL数据库

docker run -d \
-p 3306:3306 \
--name mysql-li \
--restart=always \
-v hospital-mysql:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=010401 \
mysql:8.0.16

【5】编写Dockerfile文件

基于JDK构建的镜像,如果是war包,则要基于Tomcat构建镜像

将jar放入容器的根目录

容器启动后在根目录执行java -jar keshe.jar指令

FROM khipu/openjdk17-alpine:latest
ADD ./keshe.jar /keshe.jar
ENTRYPOINT ["java","-jar","keshe.jar"]

【6】构建Docker镜像

[root@localhost spring-boot-start]# docker build -t keshe:1.0 .

Sending build context to Docker daemon  33.02MB
Step 1/3 : FROM khipu/openjdk17-alpine:latest
 ---> 47006fef220c
Step 2/3 : ADD ./keshe.jar /keshe.jar
 ---> 07952ad22493
Step 3/3 : ENTRYPOINT ["java","-jar","keshe.jar"]
 ---> Running in 939a654ed742
Removing intermediate container 939a654ed742
 ---> e09b1e0f15d8
Successfully built e09b1e0f15d8
Successfully tagged keshe:1.0

【7】查看生成的Docker镜像

[root@localhost spring-boot-start]# docker images

REPOSITORY               TAG               IMAGE ID       CREATED         SIZE
keshe                    1.0               5404f7d20f3b   2 minutes ago   397MB

【8】启动Docker容器

[root@localhost spring-boot-start]# docker run --name keshe-lixianhe -p 8080:8080 keshe:1.0

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.5)

2022-11-19 09:09:51.578  INFO 1 --- [           main] com.lixianhe.SpringbootStartApplication  : Starting SpringbootStartApplication using Java 17.0.5 on c4504d233430 with PID 1 (/keshe.jar started by root in /)
2022-11-19 09:09:51.599  INFO 1 --- [           main] com.lixianhe.SpringbootStartApplication  : No active profile set, falling back to 1 default profile: "default"
2022-11-19 09:09:56.774  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-11-19 09:09:56.815  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-11-19 09:09:56.815  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.68]
2022-11-19 09:09:57.069  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-11-19 09:09:57.069  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 5207 ms
2022-11-19 09:09:57.441  INFO 1 --- [           main] c.a.d.s.b.a.DruidDataSourceAutoConfigure : Init DruidDataSource
2022-11-19 09:09:57.912  INFO 1 --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
 _ _   |_  _ _|_. ___ _ |    _ 
| | |\/|_)(_| | |_\  |_)||_|_\ 
     /               |         
                        3.4.2 
2022-11-19 09:10:00.218  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-11-19 09:10:00.273  INFO 1 --- [           main] com.lixianhe.SpringbootStartApplication  : Started SpringbootStartApplication in 10.694 seconds (JVM running for 11.928)

【9】访问项目: 成功

Dockerfile构建SpringBoot项目_第1张图片

【10】使用docker-compose

在Dockerfile的同级目录下创建docker-compose.yml文件,内容如下

version: "3"
services:
  mysql:
    image: mysql:8.0.16
    container_name: keshe-mysql
    networks:
      - keshe
    ports:
      - "3306:3306"
    environment:
      - "MYSQL_ROOT_PASSWORD=010401"
    volumes:
      - "hospital-mysql:/var/lib/mysql"
  spring-boot-start:
    image: keshe:1.0
    container_name: keshe-lixianhe
    networks:
      - keshe
    ports:
      - "8080:8080"
    depends_on:
      - mysql
volumes:
    hospital-mysql: {}

networks:
  keshe:

在同一个网桥下且不是docker0,可以使用容器名称代替IP地址,如下

url: jdbc:mysql://keshe-mysql:3306/db2?allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai

【11】运行docker-compose

指令:docker-compose up -d

你可能感兴趣的:(云原生,spring,boot,docker)