容器项目之前后端分离

容器化部署ruoyi项目

bash 复制代码
#需要的镜像nginx、java、mysql、redis、
#导入maven镜像、Java镜像和node镜像
docker load -i java-8u111-jdk.tar
docker load -i maven-3.8.8-sapmachine-11.tar
docker load -i node-18.20.3-alpine3.20.tar
#拉取MySQL和nginx镜像
docker pull mysql:5.7
docker pull nginx:1.16.1
#准备数据库
docker run -itd --name ruoyi-mysql -v ./sql:/docker-entrypoint-initdb.d -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=ry-vue mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
#准备redis
docker run -itd --name ruoyi-redis
#获取ruoyi的源码包
git clone https://gitee.com/y_project/RuoYi-Vue.git && cd RuoYi-Vue
#修改后端的代码
vim /opt/RuoYi-Vue/ruoyi-admin/src/main/resources/application.yml
bash 复制代码
vim /opt/RuoYi-Vue/ruoyi-admin/src/main/resources/application-druid.yml
bash 复制代码
#打包后端代码
docker run -it --rm -v .:/opt/app -v /root/.m2:/root/.m2 -w /opt/app maven:3.8.8-sapmachine-11 mvn package
#打包前端代码
cd ruoyi-ui
docker run -it --rm -v .:/opt/app -w /opt/app node:18.20.3-alpine3.20 /bin/sh -c "npm install  --registry http://registry.npmmirror.com && npm run build:prod"
#打包好的的代码
java : ruoyi-admin/target
前端 : ruoyi-ui/dist
#整理打包好的代码
#后端
mkdir -p /opt/ruoyi-xa2401/{ruoyi-java,ruoyi-nginx}
cp ruoyi-admin/target/java包 /opt/ruoyi-xa2401/ruoyi-java
vim /opt/ruoyi-xa2401/ruoyi-java/Dockerfile
FROM java:8u111-jdk
COPY ./ruoyi-admin.jar /opt
CMD ["java","-jar","/opt/ruoyi-admin.jar"]
#前端
cp -r ruoyi-ui/dist /opt/ruoyi-xa2401/ruoyi-nginx
cp -r nginx.conf /opt/ruoyi-xa2401/ruoyi-nginx # nginx配置文件在最后面
vim opt/ruoyi-xa2401/ruoyi-nginx/Dockerfile
FROM nginx:1.16.1
COPY dist /usr/share/nginx/dist
COPY nginx.conf /etc/nginx/nginx.conf
CMD ["nginx","-g","daemon off;"]
#构建镜像并运行
docker build -t ruoyi-java:1.0 .
docker build -t ruoyi-nginx:1.0 .
docker run -it --name ruoyi-java --rm --link=ruoyi-mysql:mysql.ruoyi --link=ruoyi-redis:redis.ruoyi ruoyi-java:1.0 
docker run -it --name ruoyi-nginx --link=ruoyi-java:java.host -p 80:80 ruoyi-nginx:1.0

nginx的配置文件

bash 复制代码
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;
    charset utf-8;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    server {
        listen       80;
        server_name  localhost;
    
        #charset koi8-r;
        #access_log  /var/log/nginx/host.access.log  main;
    
        location / {
            root   /usr/share/nginx/dist;
            try_files $uri $uri/ /index.html;
            index  index.html index.htm;
        }
    
        location /prod-api/{
          proxy_pass http://java.host:8080/; #注意这里的写法
          proxy_set_header Host $http_host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       }
    
    }

}

测试

用docker compose编排

bash 复制代码
services:
    
    nginx:
        build:
            context: ./nginx
        ports:
            - "80:80"
        links:
            - "java:java.host"
        depends_on:
            - java

    java:
        build:
            context: ./java
        links:
            - "redis:redis.ruoyi"
            - "database:mysql.ruoyi"
        depends_on:
            - redis
            - database

    redis:
        image: redis

    database:
        image: mysql:5.7
        container_name: mysql
        volumes:
            - ./sql:/docker-entrypoint-initdb.d
            - ./databases:/var/lib/mysql
        environment:
            - MYSQL_ROOT_PASSWORD=123456
            - MYSQL_DATABASE=ry-vue
        command:
            - --character-set-server=utf8mb4
            - --collation-server=utf8mb4_unicode_ci

=123456

  • MYSQL_DATABASE=ry-vue

command:

  • --character-set-server=utf8mb4

  • --collation-server=utf8mb4_unicode_ci

复制代码
相关推荐
IT-民工211101 小时前
CI/CD 实践总结
运维·ci/cd·自动化
梦魇梦狸º2 小时前
腾讯轻量云服务器docker拉取不到镜像的问题:拉取超时
docker·容器·github
鬼才血脉2 小时前
docker+mysql配置
mysql·adb·docker
詹姆斯爱研究Java2 小时前
Docker 的基本概念和优势
docker
_.Switch3 小时前
Serverless架构与自动化运维
运维·python·缓存·自动化·运维开发
孤蓬&听雨4 小时前
RabbitMQ自动发送消息工具(自动化测试RabbitMQ)
分布式·测试工具·自动化·rabbitmq·自动发送消息
南猿北者4 小时前
docker镜像仓库常用命令
运维·docker·容器
杨浦老苏4 小时前
Docker可视化管理面板DPanel的安装
运维·docker·群晖
2301_806131365 小时前
Kubernetes的基本构建块和最小可调度单元pod-0
云原生·容器·kubernetes
SilentCodeY6 小时前
containerd配置私有仓库registry
容器·kubernetes·containerd·镜像·crictl