Vue项目通过docker容器方式部署到华为云服务器

Vue项目通过docker容器方式部署到华为云服务器
前端vue部署

  1. 修改前端交互 端口及IP地址

  2. 打包控制台输入命令:npm run build
    在这里插入图片描述

  3. 安装Nginx 并且修改Nginx挂载的配置文件nginx-proxy.config
    NgInx配置文件示例如下:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    server {
        listen       80;
        server_name  localhost;
		charset utf-8;
        index index.html index.htm index.php;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        root /usr/share/nginx/html; 
        location / {
      # 此处的 @router 实际上是引用下面的转发,否则在 Vue 路由刷新时可能会抛出 404
             try_files $uri $uri/ @router;
          # 请求指向的首页
             index index.html;
           # proxy_pass http://123.249.79.20:8090;
           }
       # 由于路由的资源不一定是真实的路径,无法找到具体文件
       # 所以需要将请求重写到 index.html 中,然后交给真正的 Vue 路由处理请求资源
             location @router {
            rewrite ^.*$ /index.html last;
                } 
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}
  1. 将vue打包的dist文件上传到服务器(在服务器任意位置创建空白文件夹,存放dist文件)
    在这里插入图片描述

  2. 找到上传到服务器的dist文件,并且在同一目录下创建Dockerfile文件

  3. 编写Dockerfile文件

    示例:

    # 设置基础镜像,这里使用最新的nginx镜像,前面已经拉取过了
    FROM nginx
    # 将dist文件中的内容复制到 /usr/share/nginx/html/ 这个目录下面
    COPY dist/  /usr/share/nginx/html/
    
  4. 创建vue镜像(切记后边有 . ): docker build -t vueApp .

  5. 创建镜像容器:docker run -d --name VueTest -p 8111:80 vueApp

在这里插入图片描述

9.将dist目录下的所有文件移动到docker容器的nginx内部根目录

命令如下:

  • 文件移到docker nginx内部: docker cp dist/ (容器id):/

或者通过进入Xftp进行文件直接上传注意dist文件包含文件位置如下图所示
进入docker nginx目录下语句:docker exec -it 容器id bash
在这里插入图片描述

10.重启Nginx:docker restart (容器id)

11.访问网站地址

其中可能会出现跨域问题

  • 检查vue前端ip和端口是否为华为云服务公开端口
  • 后端gateway是否设置跨域
    在这里插入图片描述
server:
  port: 8090
spring:
  application:
    name: gateway
  cloud:
    nacos:
      discovery:
        server-addr: 123.249.79.20:8848
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
        - id: admin
          uri: lb://admin
          predicates:
            - Path=/umsUser/**
        - id: product
          uri: lb://product
          predicates:
            - Path=/pmsAttr/**,/pmsBrand/**,/pmsCategory/**,/pmsProduct/**,/pmsSku/**,/umsFile/**
      globalcors:
        cors-configurations:
          '[/**]':
            allowedOrigins: "http://123.249.79.20"
            allowedMethods: "*"
            allowedHeaders: "*"

后端部署可点击后端springboot微服务项目部署

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值