vue发版html 生成打包到docker镜像进行发版

将Vue项目打包成Docker镜像部署主要分为以下几个步骤:

1. Vue项目打包‌

执行npm run build生成dist文件夹,包含静态资源文件

注意检查index.html中资源引用路径是否正确(避免绝对路径问题)

2. 编写Dockerfile

dockerfile 复制代码
Copy Code
FROM nginx:latest
COPY dist/ /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

3. 构建镜像‌

bash 复制代码
docker build -t vue-app .‌

4. 运行容器‌

bash 复制代码
docker run -d -p 8080:80 --name vue-container vue-app

5. 高级配置(可选)‌

  • 自定义nginx配置:添加nginx.conf文件并COPY到/etc/nginx/nginx.conf
  • 多环境配置:通过.env文件区分开发/测试/生产环境
  • 使用docker-compose管理容器

完整示例项目结构:

  • dist/ (Vue打包输出)
  • Dockerfile
  • nginx.conf (可选自定义配置)

访问方式:http://localhost:8080(端口号根据实际映射调整)

样例

以下是实现Vue项目Docker部署时自定义Nginx配置的完整方案:

1. 首先创建自定义nginx.conf文件(推荐放在项目根目录下):

bash 复制代码
worker_processes  1;

events {
    worker_connections  1024;
}

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

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }
}

2. 修改后的Dockerfile配置:

Dockerfile

bash 复制代码
nginx:latest
COPY dist/ /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

关键点说明:

  1. try_files配置解决Vue路由history模式404问题
  2. 配置了错误页面处理
  3. 使用COPY指令将配置文件覆盖默认配置
  4. 保持80端口暴露

构建命令不变:docker build -t vue-app . && docker run -p 8080:80 vue-app

相关推荐
周末也要写八哥2 小时前
html网页设计适合新手的学习路线总结
html
killerbasd2 小时前
牧苏苏传 我不装了 4/7
前端·javascript·vue.js
ZC跨境爬虫3 小时前
【爬虫实战对比】Requests vs Scrapy 笔趣阁小说爬虫,从单线程到高效并发的全方位升级
前端·爬虫·scrapy·html
爱上好庆祝3 小时前
svg图片
前端·css·学习·html·css3
handsomestWei4 小时前
Docker引擎API接入配置
运维·http·docker·容器·api
大家的林语冰4 小时前
《前端周刊》尤大开源 Vite+ 全家桶,前端工业革命启动;尤大爆料 Void 云服务新产品,Vite 进军全栈开发;ECMA 源码映射规范......
前端·javascript·vue.js
M ? A6 小时前
Vue 迁移 React 实战:VuReact 一键自动化转换方案
前端·vue.js·经验分享·react.js·开源·自动化·vureact
Burt6 小时前
我的 2026 全栈选型:Vue3 + Elysia + Bun + AlovaJS
vue.js·全栈·bun
小锋java12346 小时前
SpringBoot 4 + Spring Security 7 + Vue3 前后端分离项目设计最佳实践
java·vue.js·spring boot
一 乐6 小时前
校园线上招聘|基于springboot + vue校园线上招聘系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·校园线上招聘系统