容器项目之前后端分离

容器化部署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

复制代码
相关推荐
程序猿000001号8 分钟前
Selenium 深度解析:自动化浏览器操作的利器
selenium·测试工具·自动化
Karoku06627 分钟前
【k8s集群应用】kubeadm1.20高可用部署(3master)
运维·docker·云原生·容器·kubernetes
saynaihe2 小时前
安全地使用 Docker 和 Systemctl 部署 Kafka 的综合指南
运维·安全·docker·容器·kafka
G_whang3 小时前
centos7下docker 容器实现redis主从同步
redis·docker·容器
认真学习的小雅兰.3 小时前
如何在Ubuntu上利用Docker和Cpolar实现Excalidraw公网访问高效绘图——“cpolar内网穿透”
linux·ubuntu·docker
the丶only4 小时前
单点登录平台Casdoor搭建与使用,集成gitlab同步创建删除账号
linux·运维·服务器·docker·gitlab
书生-w4 小时前
Docker部署GitLab服务器
服务器·docker·gitlab
塔克拉玛攻城狮4 小时前
私有网盘+在线文档:内网离线搭建NextCloud+OnlyOffice详细指南
docker·在线文档·网盘
ccubee5 小时前
docker 安装 ftp
运维·docker·容器
yaosheng_VALVE5 小时前
探究全金属硬密封蝶阀的奥秘-耀圣控制
运维·eclipse·自动化·pyqt·1024程序员节