docker java项目容器化改造
前后端分离项目
前端 https://gitee.com/yuco/eladmin-web.git
后端 https://gitee.com/yuco/eladmin.git
- 要素:
vue
npm
springboot
mysql
redis
java后端容器化
思路:
了解在物理机虚拟机的部署流程,然后编写dockerfile进行容器化部署。
java项目,使用mvn进行构建,最终生成jar包,拷贝到jdk环境中启动即可,所以需要多阶段构建。
先启动容器,然后手动构建验证,最后在编写dockerfile进行构建。
# 1.进入maven环境,下载代码,构建得到jar包,pom.xml已经配置好了阿里云的mvn源
docker run --rm -ti aerialist7/maven-git sh #创建一个mvn 、jdk环境
git clone https://gitee.com/yuco/eladmin.git
cd /eladmin
mkdir -p /root/.m2
echo '<settings><mirrors><mirror><id>nexus-tencentyun</id><url>https://maven.aliyun.com/repository/public </url><mirrorOf>central</mirrorOf></mirror></mirrors></settings>' > /root/.m2/settings.xml #配置腾讯mvn源地址
mvn clean package
启动java程序
/eladmin # ls
LICENSE eladmin-common eladmin-logging eladmin-tools sql
README.md eladmin-generator eladmin-system pom.xml
/eladmin # cd eladmin-system/target/
/eladmin/eladmin-system/target # ls
classes eladmin-system-2.6.jar.original generated-test-sources maven-status
eladmin-system-2.6.jar generated-sources maven-archiver test-classes
/eladmin/eladmin-system/target # java -jar eladmin-system-2.6.jar
_ _ _
| | | | (_)
___| |______ __ _ __| |_ __ ___ _ _ __
/ _ | |______/ _` |/ _` | '_ ` _ \| | '_ \
| __| | | (_| | (_| | | | | | | | | | |
\___|_| \__,_|\__,_|_| |_| |_|_|_| |_|
:: Spring Boot :: (v2.6.4)
elAdmin- 2024-05-07 06:31:58 [main] INFO me.zhengjie.AppRun - Starting AppRun v2.6 using Java 1.8.0_191 on 1e2bbff29645 with PI
编写dockerfile
根据手动编译,咱们已经知道了流程,
1、需要mvn java git环境
2、修改mvn源地址,编译源码
3、生成jar包,启动java -jar eladmin-system-2.6.jar
#准备源码
cd /opt
git clone https://gitee.com/yuco/eladmin.git
FROM aerialist7/maven-git as builder
WORKDIR /opt/eladmin
COPY . .
RUN mvn clean package
FROM java:8u111
WORKDIR /opt/eladmin
COPY --from=builder /opt/eladmin/eladmin-system/target/eladmin-system-2.6.jar .
CMD [ "sh", "-c", "java -Dspring.profiles.active=prod -jar eladmin-system-2.6.jar" ]
如上的java参数-Dspring.profiles.active=prod是指可以有多个config.yml配置文件,以在不同的环境下测试。
#构建镜像
docker build -t eladmin:v1 .
准备数据库
[root@localhost mysql]# cat docker-compose.yml
version: '3'
services:
mysql:
command: '--character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci'
image: 'mysql:5.7'
environment:
- MYSQL_ROOT_PASSWORD=H123.com
- MYSQL_DATABASE=eladmin
volumes:
- '/opt/mysql2:/var/lib/mysql'
container_name: mysql
ports:
- '3306:3306'
导入eladmin数据
[root@localhost eladmin]# cd sql
[root@localhost sql]# ls
eladmin.sql tool_picture.sql 脚本如何选择.md
[root@localhost sql]# pwd
/opt/eladmin/sql
[root@localhost sql]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
41edc310433c mysql:5.7 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
[root@localhost sql]# docker cp /opt/eladmin/sql/eladmin.sql mysql:/tmp/
Successfully copied 51.2kB to mysql:/tmp/
[root@localhost sql]#
[root@localhost sql]# docker exec -it mysql /bin/bash #进入数据库
bash-4.2# mysql -uroot -pH123.com eladmin < /tmp/eladmin.sql #导入数据
mysql: [Warning] Using a password on the command line interface can be insecure.
bash-4.2# mysql -uroot -pH123.com
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.44 MySQL Community Server (GPL)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| eladmin |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> USE eladmin; #选择数据库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> SHOW TABLES; #查看数据库中的表
+--------------------+
| Tables_in_eladmin |
+--------------------+
| code_column_config |
| code_gen_config |
| mnt_app |
| mnt_database |
| mnt_deploy |
| mnt_deploy_history |
| mnt_deploy_server |
| mnt_server |
| sys_dept |
| sys_dict |
| sys_dict_detail |
| sys_job |
| sys_log |
数据库常用命令
要查看 MySQL 中 `test` 数据库的信息,你需要通过 MySQL 命令行工具或任何支持的图形界面工具连接到数据库。下面我会给你一个简单的步骤说明如何使用命令行查看数据库信息。
**步骤 1: 连接到 MySQL 数据库**
首先,你需要使用 MySQL 命令行客户端连接到 MySQL 服务器。假设你已经有了适当的权限和认证信息,你可以使用以下命令:
```bash
mysql -u username -p
```
这里 `username` 是你的 MySQL 用户名。输入命令后,系统会提示你输入密码。
**步骤 2: 选择数据库**
连接成功后,你需要选择 `test` 数据库:
```sql
USE test;
```
**步骤 3: 查看数据库中的表**
要查看 `test` 数据库中所有的表,你可以使用以下 SQL 命令:
```sql
SHOW TABLES;
```
**步骤 4: 查看表的结构**
选择一个特定的表,比如 `example_table`,并查看其结构:
```sql
DESCRIBE example_table;
```
**步骤 5: 查询表中的数据**
查看表中的数据,可以使用 `SELECT` 语句。例如,查看 `example_table` 表中的所有记录:
```sql
SELECT * FROM example_table;
```
这些步骤将帮助你检查 `test` 数据库中的内容。如果你需要执行特定的查询或有关数据的更复杂的分析,请确保 SQL 语句符合你的需求。
创建redis
[root@localhost redis]# cat docker-compose.yaml
version: '3'
services:
redis:
command: redis-server
image: 'redis:3.2'
restart: always
container_name: redis
ports:
- '6379:6379'
启动后端
[root@localhost houduan]# cat docker-compose.yaml
version: '3'
services:
eladmin:
image: 'eladmin:v1'
environment:
- REDIS_HOST=192.168.199.142
- DB_PWD=H123.com
- DB_USER=root
- DB_HOST=192.168.199.142
ports:
- '8000:8000'
container_name: eladmin-api
前端Vue构建
思路:
- 前端构建需要node环境,构建结果通常为dist静态文件,项目运行仅需要web服务器即可。因此采用多阶构建
- 确认基础镜像:
docker search vue-node
- 验证构建,最终采用
codemantn/vue-node
作为基础镜像
准备源码
git clone https://gitee.com/yuco/eladmin-web
cd eladmin-web/
修改npm源
npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/
npm config set registry https://registry.npm.taobao.com #测试能不能用
npm install
npm run build:prod
生成dist静态文件,把文件内容放到nignx的/usr/share/nginx/html即可
编写dockerfile
FROM codemantn/vue-node AS builder
LABEL maintainer="yanfei"
# config npm
RUN npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/ && \
npm config set registry https://mirrors.huaweicloud.com/repository/npm/
WORKDIR /opt/eladmin-web
COPY . .
# build
RUN ls -l && npm install && npm run build:prod
FROM nginx:alpine
WORKDIR /usr/share/nginx/html
COPY --from=builder /opt/eladmin-web/dist /usr/share/nginx/html/
EXPOSE 80
修改前端代码,后端AIP地址
浏览器——前端——后端
需要在前端代码后面成后端的API地址
[root@localhost eladmin-web]# cat .env.production
ENV = 'production'
# 如果使用 Nginx 代理后端接口,那么此处需要改为 '/',文件查看 Docker 部署篇,Nginx 配置
# 接口地址,注意协议,如果你没有配置 ssl,需要将 https 改为 http
VUE_APP_BASE_API = 'http://192.168.199.142:8000'
# 如果接口是 http 形式, wss 需要改为 ws
VUE_APP_WS_API = 'ws://192.168.199.142:8000'
[root@localhost eladmin-web]#
构建镜像
docker build . -t eladmin-web:v1 -f Dockerfile
启动前端
mkdir qianduan
[root@localhost qianduan]# cat docker-compose.yaml
version: '3'
services:
eladmin-web:
image: 'eladmin-web:v1'
ports:
- '800:80'
container_name: eladmin-web
[root@localhost qianduan]# docker-compose up -d
Recreating eladmin-web ... done
[root@localhost qianduan]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
118cfc5bca90 eladmin-web:v1 "/docker-entrypoint.…" 4 seconds ago Up 3 seconds 0.0.0.0:800->80/tcp, :::800->80/tcp eladmin-web
62eae48d373b eladmin:v1 "sh -c 'java -Dsprin…" 55 minutes ago Up 55 minutes 0.0.0.0:8000->8000/tcp, :::8000->8000/tcp eladmin-api
4dc3fc361855 redis:3.2 "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp redis
41edc310433c mysql:5.7 "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
查看数据库是否有用户
[root@localhost qianduan]# docker exec -it mysql /bin/bash
bash-4.2# mysql -uroot -p
mysql> use eladmin;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+--------------------+
| Tables_in_eladmin |
+--------------------+
| code_column_config |
| code_gen_config |
| mnt_app |
| mnt_database |
| mnt_deploy |
| mnt_deploy_history |
| mnt_deploy_server |
| mnt_server |
| sys_dept |
| sys_dict |
| sys_dict_detail |
| sys_job |
| sys_log |
| sys_menu |
| sys_quartz_job |
| sys_quartz_log |
| sys_role |
| sys_roles_depts |
| sys_roles_menus |
| sys_user |
| sys_users_jobs |
| sys_users_roles |
| tool_alipay_config |
| tool_email_config |
| tool_local_storage |
| tool_qiniu_config |
| tool_qiniu_content |
+--------------------+
27 rows in set (0.00 sec)
mysql> select * from sys_user;
+---------+---------+----------+-----------+--------+-------------+------------------+------------------------------+----------------------------------------------------------------------------------+--------------------------------------------------------------+----------+---------+-----------+-----------+---------------------+---------------------+---------------------+
| user_id | dept_id | username | nick_name | gender | phone | email | avatar_name | avatar_path | password | is_admin | enabled | create_by | update_by | pwd_reset_time | create_time | update_time |
+---------+---------+----------+-----------+--------+-------------+------------------+------------------------------+----------------------------------------------------------------------------------+--------------------------------------------------------------+----------+---------+-----------+-----------+---------------------+---------------------+---------------------+
| 1 | 2 | admin | ??? | ? | 18888888888 | [email protected] | avatar-20200806032259161.png | /Users/jie/Documents/work/me/admin/eladmin/~/avatar/avatar-20200806032259161.png | $2a$10$Egp1/gvFlt7zhlXVfEFw4OfWQCGPw0ClmMcc6FjTnvXNRVf9zdMRa | | 1 | NULL | admin | 2020-05-03 16:38:31 | 2018-08-23 09:11:56 | 2020-09-05 10:43:31 |
| 2 | 2 | test | ?? | ? | 19999999999 | [email protected] | NULL | NULL | $2a$10$4XcyudOYTSz6fue6KFNMHeUQnCX5jbBQypLEnGk1PmekXt5c95JcK | | 1 | admin | admin | NULL | 2020-05-05 11:15:49 | 2020-09-05 10:43:38 |
| 3 | 7 | yanfei | yanfei | ? | 13838384388 | [email protected] | NULL | NULL | $2a$10$WGBNrFiJE3APZ3nSiOaBg.Z/Gq6BsXmg2I0kIB9ozwlLe30XiH62u | | 1 | admin | admin | NULL | 2024-05-07 17:09:56 | 2024-05-07 17:09:56 |
+---------+---------+----------+-----------+--------+-------------+------------------+------------------------------+----------------------------------------------------------------------------------+--------------------------------------------------------------+----------+---------+-----------+-----------+---------------------+---------------------+---------------------+
3 rows in set (0.00 sec)
mysql>
合并docker-compse文件
version: '3'
services:
mysql:
command: '--character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci'
image: 'mysql:5.7'
environment:
- MYSQL_ROOT_PASSWORD=H123.com
- MYSQL_DATABASE=eladmin
volumes:
- '/opt/mysql2:/var/lib/mysql'
container_name: mysql
ports:
- '3306:3306'
redis:
command: redis-server
image: 'redis:3.2'
restart: always
container_name: redis
ports:
- '6379:6379'
eladmin-web:
image: 'eladmin-web:v1'
ports:
- '800:80'
container_name: eladmin-web
eladmin:
image: 'eladmin:v1'
environment:
- REDIS_HOST=redis
- DB_PWD=H123.com
- DB_USER=root
- DB_HOST=mysql
ports:
- '8000:8000'
container_name: eladmin-api
标签:容器,java,web,改造,eladmin,sys,mysql,docker,root
From: https://www.cnblogs.com/9527com/p/18178038