若依(前后端分离)项目构建docker 镜像
1. 构建好ruoyi-admin.jar包,上传到服务器项目目录下
2. 创建conf目录将若依项目(Spring boot )配置文件修改好,上传存入conf目录
注意:这里的地址不能写127.0.0.1和localhost 这两个地址是容器内部的地址,容器(ruoyi-admin.jar访问mysql、redis)之间相互访问需要 宿主机ip +映射出的端口号
修改配置文件参数,数据库ip和账号密码
修改端口号,这个修改不修改都无所谓,docker run时端口映射时修改也可以的
redis ip和端口修改
输入目录 rz 上传
退出conf 目录,在/home/ruoyi 目录下,已传统的方式java -jar 方式执行看看能不能启动成功
java -jar ruoyi-admin.jar --spring.config.location=conf/application.yml,conf/application-druid.yml
[root@mojin ruoyi]# java -jar ruoyi-admin.jar --spring.config.location=conf/application.yml,conf/application-druid.yml
Application Version: 3.8.5
Spring Boot Version: 2.5.14
// _ooOoo_ //
// o8888888o //
// 88" . "88 //
// (| ^_^ |) //
// O\ = /O //
// ____/`---'\____ //
// .' \\| |// `. //
// / \\||| : |||// \ //
// / _||||| -:- |||||- \ //
// | | \\\ - /// | | //
// | \_| ''\---/'' | | //
// \ .-\__ `-` ___/-. / //
// ___`. .' /--.--\ `. . ___ //
// ."" '< `.___\_<|>_/___.' >'"". //
// | | : `- \`.;`\ _ /`;.`/ - ` : | | //
// \ \ `-. \_ __\ /__ _/ .-` / / //
// ========`-.____`-.___\_____/___.-`____.-'======== //
// `=---=' //
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //
// 佛祖保佑 永不宕机 永无BUG //
19:52:20.608 [background-preinit] INFO o.h.v.i.util.Version - [<clinit>,21] - HV000001: Hibernate Validator 6.2.3.Final
19:52:20.622 [main] INFO c.r.RuoYiApplication - [logStarting,55] - Starting RuoYiApplication using Java 1.8.0_181 on mojin with PID 31304 (/home/ruoyi/ruoyi-admin.jar started by root in /home/ruoyi)
19:52:20.624 [main] DEBUG c.r.RuoYiApplication - [logStarting,56] - Running with Spring Boot v2.5.14, Spring v5.3.20
19:52:20.625 [main] INFO c.r.RuoYiApplication - [logStartupProfileInfo,686] - The following 1 profile is active: "druid"
19:52:28.074 [main] INFO o.a.c.h.Http11NioProtocol - [log,173] - Initializing ProtocolHandler ["http-nio-8025"]
19:52:28.085 [main] INFO o.a.c.c.StandardService - [log,173] - Starting service [Tomcat]
19:52:28.086 [main] INFO o.a.c.c.StandardEngine - [log,173] - Starting Servlet engine: [Apache Tomcat/9.0.63]
19:52:28.302 [main] INFO o.a.c.c.C.[.[.[/] - [log,173] - Initializing Spring embedded WebApplicationContext
19:52:29.820 [main] DEBUG c.r.f.s.f.JwtAuthenticationTokenFilter - [init,242] - Filter 'jwtAuthenticationTokenFilter' configured for use
19:52:34.555 [main] INFO c.a.d.p.DruidDataSource - [init,996] - {dataSource-1} inited
19:52:34.572 [main] DEBUG c.r.s.m.S.selectConfigList - [debug,137] - ==> Preparing: select config_id, config_name, config_key, config_value, config_type, create_by, create_time, update_by, update_time, remark from sys_config
19:52:34.612 [main] DEBUG c.r.s.m.S.selectConfigList - [debug,137] - ==> Parameters:
19:52:34.645 [main] DEBUG c.r.s.m.S.selectConfigList - [debug,137] - <== Total: 6
19:52:36.720 [main] DEBUG c.r.s.m.S.selectDictDataList - [debug,137] - ==> Preparing: select dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time, remark from sys_dict_data WHERE status = ? order by dict_sort asc
19:52:36.751 [main] DEBUG c.r.s.m.S.selectDictDataList - [debug,137] - ==> Parameters: 0(String)
19:52:36.811 [main] DEBUG c.r.s.m.S.selectDictDataList - [debug,137] - <== Total: 29
19:52:37.578 [main] INFO o.q.i.StdSchedulerFactory - [instantiate,1220] - Using default implementation for ThreadExecutor
19:52:37.615 [main] INFO o.q.c.SchedulerSignalerImpl - [<init>,61] - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
19:52:37.615 [main] INFO o.q.c.QuartzScheduler - [<init>,229] - Quartz Scheduler v.2.3.2 created.
19:52:37.619 [main] INFO o.q.s.RAMJobStore - [initialize,155] - RAMJobStore initialized.
19:52:37.622 [main] INFO o.q.c.QuartzScheduler - [initialize,294] - Scheduler meta-data: Quartz Scheduler (v2.3.2) 'quartzScheduler' with instanceId 'NON_CLUSTERED'
Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
NOT STARTED.
Currently in standby mode.
Number of jobs executed: 0
Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.
19:52:37.622 [main] INFO o.q.i.StdSchedulerFactory - [instantiate,1374] - Quartz scheduler 'quartzScheduler' initialized from an externally provided properties instance.
19:52:37.623 [main] INFO o.q.i.StdSchedulerFactory - [instantiate,1378] - Quartz scheduler version: 2.3.2
19:52:37.624 [main] INFO o.q.c.QuartzScheduler - [setJobFactory,2293] - JobFactory set to: org.springframework.scheduling.quartz.SpringBeanJobFactory@2a39fe6a
19:52:37.690 [main] DEBUG c.r.q.m.S.selectJobAll - [debug,137] - ==> Preparing: select job_id, job_name, job_group, invoke_target, cron_expression, misfire_policy, concurrent, status, create_by, create_time, remark from sys_job
19:52:37.692 [main] DEBUG c.r.q.m.S.selectJobAll - [debug,137] - ==> Parameters:
19:52:37.697 [main] DEBUG c.r.q.m.S.selectJobAll - [debug,137] - <== Total: 3
19:52:41.485 [main] INFO o.a.c.h.Http11NioProtocol - [log,173] - Starting ProtocolHandler ["http-nio-8025"]
19:52:42.497 [main] INFO o.q.c.QuartzScheduler - [start,547] - Scheduler quartzScheduler_$_NON_CLUSTERED started.
19:52:42.525 [main] INFO c.r.RuoYiApplication - [logStarted,61] - Started RuoYiApplication in 23.409 seconds (JVM running for 24.861)
(♥◠‿◠)ノ゙ 若依启动成功 ლ(´ڡ`ლ)゙
.-------. ____ __
| _ _ \ \ \ / /
| ( ' ) | \ _. / '
|(_ o _) / _( )_ .'
| (_,_).' __ ___(_ o _)'
| |\ \ | || |(_,_)'
| | \ `' /| `-' /
| | \ / \ /
''-' `'-' `-..-'
启动成功完成
3. 编写Dockerfile
FROM openjdk:8
MAINTAINER mojin
VOLUME /tmp
RUN mkdir -p /home/ruoyi/conf
ADD ruoyi-admin.jar /home/ruoyi/ruoyi-admin.jar
ADD conf /home/ruoyi/conf
EXPOSE 8025
RUN bash -c 'touch /home/ruoyi/ruoyi-admin.jar'
ENTRYPOINT ["java", "-Xms256m", "-Xmx256m", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/home/ruoyi/ruoyi-admin.jar", "--spring.config.location=/home/ruoyi/conf/application.yml,/home/ruoyi/conf/application-druid.yml"]
输入命令:vim Dockerfile,复制粘贴上面的Dockerfile信息
按Esc 按键退出编辑模式,输入Shift+: 键 输入wq 回车 保存退出 Dockerfile
4. 构件镜像 docker build -t ruoyi-admin .
命令行输入docker build -t ruoyi-admin . 注意后面的 “.” 不能少,ruoyi-admin为镜像名称,只能小写
docker build -t ruoyi-admin .
[root@mojin ruoyi]# docker build -t ruoyi-admin .
Sending build context to Docker daemon 81.92 MB
Step 1/9 : FROM openjdk:8
---> e24ac15e052e
Step 2/9 : MAINTAINER mojin
---> Running in 6d54bf8b8ed8
---> 76b98c33030c
Removing intermediate container 6d54bf8b8ed8
Step 3/9 : VOLUME /tmp
---> Running in 121335e918d8
---> 8417ca2768e2
Removing intermediate container 121335e918d8
Step 4/9 : RUN mkdir -p /home/ruoyi/conf
---> Running in ee4f755e8611
---> 64828e4e808c
Removing intermediate container ee4f755e8611
Step 5/9 : ADD ruoyi-admin.jar /home/ruoyi/ruoyi-admin.jar
---> 37a006cbfb4f
Removing intermediate container 224e5f7c1251
Step 6/9 : ADD conf /home/ruoyi/conf
---> 1dcd5b8698e1
Removing intermediate container ad623d02c2be
Step 7/9 : EXPOSE 8025
---> Running in be1a4cff2c88
---> 7ffddf0c8d30
Removing intermediate container be1a4cff2c88
Step 8/9 : RUN bash -c 'touch /home/ruoyi/ruoyi-admin.jar'
---> Running in 29fcca7b4579
---> 2ce681171412
Removing intermediate container 29fcca7b4579
Step 9/9 : ENTRYPOINT java -Xms256m -Xmx256m -Djava.security.egd=file:/dev/./urandom -jar /home/ruoyi/ruoyi-admin.jar --spring.config.location=/home/ruoyi/conf/application.yml,/home/ruoyi/conf/application-druid.yml
---> Running in 567b91eb3c6c
---> d9a5fabe40d7
Removing intermediate container 567b91eb3c6c
Successfully built d9a5fabe40d7
5. 运行构建好的docker 镜像
运行构建的docker镜像
-v /home/ruoyi/conf:/home/ruoyi/conf # 挂载配置文件数据卷
-v /home/ruoyi/logs:/home/ruoyi/logs # 挂载日志文件数据卷
docker run -p 8025:8025 --name=ruoyi-admin -v /home/ruoyi/conf:/home/ruoyi/conf -v /home/ruoyi/logs:/home/ruoyi/logs --restart=always --privileged=true -d ruoyi-admin
查看镜像运行日志
docker logs -f ruoyi-admin
启动成功
6. 写个shell 脚本,一键执行卸载镜像、构建镜像、运行镜像步骤
vim ruoyi_build.sh 创建脚本 复制粘贴
chmod 777 ruoyi_build.sh 给读写执行权限
ruoyi_build.sh
#!/bin/bash
echo "开始部署…………"
prot_r=8025
prot_c=8025
BASE_PATH=/home/ruoyi
#docker 镜像/容器名字或者jar名字 这里都命名为这个【必须为小写】
SERVER_NAME=ruoyi-admin
#容器id
CID=$(docker ps | grep "$SERVER_NAME" | awk '{print $1}')
#镜像idls
IID=$(docker images | grep "$SERVER_NAME" | awk '{print $3}')
echo "修改*.jar文件的权限"
chmod 777 $BASE_PATH/ruoyi-admin.jar
#echo "迁移完成"
# 构建docker镜像
if [ -n "$IID" ]; then
echo "存在$SERVER_NAME镜像,IID=$IID"
echo "停止运行中的容器:docker stop $CID"
docker stop $CID # 停止运行中的容器
echo "删除原来的容器:docker rm $CID"
docker rm $CID ##删除原来的容器
echo "删除原来的镜像:docker rmi $IID"
docker rmi $IID ## 删除原来的镜像
else
echo "不存在$SERVER_NAME镜像,开始构建镜像"
fi
# 构建镜像
echo "构建镜像"
cd $BASE_PATH
echo "docker build -t $SERVER_NAME ."
docker build -t $SERVER_NAME .
echo "$SERVER_NAME 容器创建完成"
# 运行容器
echo "$SERVER_NAME 容器开始运行:docker run -p $prot_r:$prot_c --name=$SERVER_NAME -v /home/ruoyi/conf:/home/ruoyi/conf -v /home/ruoyi/logs:/home/ruoyi/logs --restart=always --privileged=true -d $SERVER_NAME"
#docker run -d -p $prot_r:$prot_c --name $SERVER_NAME $SERVER_NAME
docker run -p $prot_r:$prot_c --name=$SERVER_NAME -v /home/ruoyi/conf:/home/ruoyi/conf -v /home/ruoyi/logs:/home/ruoyi/logs --restart=always --privileged=true -d $SERVER_NAME
echo "完成部署。"
执行 sh ruoyi_build.sh
sh ruoyi_build.sh
[root@mojin ruoyi]# sh ruoyi_build.sh
开始部署…………
修改*.jar文件的权限
存在ruoyi-admin镜像,IID=d9a5fabe40d7
停止运行中的容器:docker stop 33a7462a936d
33a7462a936d
删除原来的容器:docker rm 33a7462a936d
33a7462a936d
删除原来的镜像:docker rmi d9a5fabe40d7
Untagged: ruoyi-admin:latest
Deleted: sha256:d9a5fabe40d7cbf30b339802f8432399e07aeeb0f6d49fbfbfa5bf14b4f75525
Deleted: sha256:2ce681171412b87e523e4787b22b5281dd43c50f80ec24ff6cb54655c6f9aa72
Deleted: sha256:5ba4fd7161b5e4eee9c58b2872b046dae6e3af132b96eab90bd607533af746fc
Deleted: sha256:7ffddf0c8d3090a97cc003645a5a33afcdacd5703e6fccf09be461cf9d131498
Deleted: sha256:1dcd5b8698e113965e67975f80f92ac53f0f9d84d86246563dba3a5d5eca30eb
Deleted: sha256:46e8207dc296c28820f6192a80ef37fd0dc500d7d6f3c4f5a7e125532ac6bde0
Deleted: sha256:37a006cbfb4f865b9ea9a0183ead41ee96b93a8634c6a081fbf109e49fee9e74
Deleted: sha256:d180d02654db0116c202adcbf2dd5068e713274da5227d8a46e7354f7811aba2
Deleted: sha256:64828e4e808c3500c89bdf98243fbbf3c9fa6e5fd195f94dedba0bfbfaf5ab7d
Deleted: sha256:3e2c9395641a718b4e9df92f8c3c8c98fc1f8c5afbcddbc52398713b03b519ad
Deleted: sha256:8417ca2768e2c90973514401943232b72765e1a41345fa3b26474988a566d405
Deleted: sha256:76b98c33030cef61c3ae28235809d1b34dee4644299da3f132c9550f6ac23996
构建镜像
docker build -t ruoyi-admin .
Sending build context to Docker daemon 81.93 MB
Step 1/9 : FROM openjdk:8
---> e24ac15e052e
Step 2/9 : MAINTAINER mojin
---> Running in 10af79bd5d2f
---> 4a54b9c7cba3
Removing intermediate container 10af79bd5d2f
Step 3/9 : VOLUME /tmp
---> Running in 42084c2ccadc
---> ac4c26eaafbd
Removing intermediate container 42084c2ccadc
Step 4/9 : RUN mkdir -p /home/ruoyi/conf
---> Running in 1fc136faa2ea
---> 9d9e0655e7a3
Removing intermediate container 1fc136faa2ea
Step 5/9 : ADD ruoyi-admin.jar /home/ruoyi/ruoyi-admin.jar
---> 7cc83c234549
Removing intermediate container 3679596388a5
Step 6/9 : ADD conf /home/ruoyi/conf
---> ffb7a25069db
Removing intermediate container 9cf1b163d593
Step 7/9 : EXPOSE 8025
---> Running in 26c1da0acd48
---> 6c253c004c37
Removing intermediate container 26c1da0acd48
Step 8/9 : RUN bash -c 'touch /home/ruoyi/ruoyi-admin.jar'
---> Running in 9057e0a827e5
---> 4dcfbca7b051
Removing intermediate container 9057e0a827e5
Step 9/9 : ENTRYPOINT java -Xms256m -Xmx256m -Djava.security.egd=file:/dev/./urandom -jar /home/ruoyi/ruoyi-admin.jar --spring.config.location=/home/ruoyi/conf/application.yml,/home/ruoyi/conf/application-druid.yml
---> Running in 42323d6c5d62
---> 1141cacdf794
Removing intermediate container 42323d6c5d62
Successfully built 1141cacdf794
ruoyi-admin 容器创建完成
ruoyi-admin 容器开始运行:docker run -p 8025:8025 --name=ruoyi-admin -v /home/ruoyi/conf:/home/ruoyi/conf -v /home/ruoyi/logs:/home/ruoyi/logs --restart=always --privileged=true -d ruoyi-admin
01d74c9f1888417f37dc31c3480d150767f7fa800fef337b9ee95958a2228866
完成部署。