首页 > 编程语言 >使用 docker 打包构建部署 Vue 项目,一劳永逸解决node-sass安装问题

使用 docker 打包构建部署 Vue 项目,一劳永逸解决node-sass安装问题

时间:2023-08-24 22:55:57浏览次数:57  
标签:node npm Vue sass 构建 linux 镜像

文章源于 Jenkins 构建 Vue 项目失败,然后就把 node_modules 删了重新构建发现 node-sass 安装不上了,折腾一天终于可以稳定构建了。
犹记得从学 node 的第一天,就被 node-sass 折磨了一整天,后面本地没问题了然后服务器开始折磨了,这次又遇到,尝试了一次又一次,还是用本地包构建最稳,觉得还算有用,故记录一二。

构建环境 docker+jenkins

之前已经记录过就不在多说了,可参考之前的文章,此为打包构建的流程

本篇文章的目录结构

- Dockerfile 构建node打包镜像
- sources.list 阿里云软件源 debian 9
- linux-x64-83_binding.node  node-sass包 v4.14.1
- Dockerfile.dist 构建vue运行的nginx镜像
- nginx.conf nginx镜像的配置
- src  源码
  - dist 由node打包镜像运行的容器生成的构建产物
  - package.json npm包配置
  - ...

为 vue 项目制制作 node 打包镜像

linxu 下 node 打包遇到解决的问题

笔者环境:docker:18.06,node:14.10.1,vue:2.6.11 ,webpack-cli:3.3.12

  • image-webpack-loader 在 linux 环境需要安装依赖包
    • apt-get clean && apt-get update && apt-get install -y --no-install-recommends apt-utils autoconf automake file g++ libtool make nasm libpng-dev
  • sentry 下载慢可以设置下镜像
    • npm set sentrycli_cdnurl=https://npmmirror.com/mirrors/sentry-cli/
  • node-sass
    • 从 GitHub 下载下半天,尝试设置淘宝镜像也没有用
    • npm set sass_binary_site=https://npmmirror.com/mirrors/node-sass/
    • 最后各种尝试设置源都无效,于是采用指定本地的方式,
      1. 下载 node 所对应的 node-sass 版本
      1. 指定文件路径: npm set sass_binary_path=/app/linux-x64-83_binding.node
      1. 安装到依赖:npm i --save-dev [email protected] --sass_binary_path=/app/linux-x64-83_binding.node
      1. 重新构建包生成相关文件 :npm rebuild node-sass(不执行会报错找不到 node_modules/node-sass/vendor)

以上,就是在 linux 中 node 打包 vue 项目的过程中所遇到的一些问题,接下来分享 docker 中如何将这些坑一一解决

Dockerfile

node14 的镜像基于 debian 9 ,默认源安装不了软件,故需要指定其他镜像软件源,笔者用的阿里云的

FROM node:14.10.1 AS base
COPY ./sources.list /etc/apt/
COPY ./linux-x64-83_binding.node /app/linux-x64-83_binding.node
RUN apt-get clean && apt-get update && apt-get install -y --no-install-recommends apt-utils autoconf automake file g++ libtool make nasm libpng-dev

Dockerfile 用到的 sources.list

阿里云的包源设置,用于软件安装,构建打包镜像会将其复制到 /etc/apt 目录
!官方文档更新没对,还是在阿里云包源文档的评论区的有用

deb http://mirrors.aliyun.com/debian-archive/debian stretch main contrib non-free
deb http://mirrors.aliyun.com/debian-archive/debian stretch-backports main
deb http://mirrors.aliyun.com/debian-archive/debian-security stretch/updates main
deb-src http://mirrors.aliyun.com/debian-archive/debian stretch main
deb-src http://mirrors.aliyun.com/debian-archive/debian stretch-backports main
deb-src http://mirrors.aliyun.com/debian-archive/debian-security stretch/updates main

Dockerfile 用到的 linux-x64-83_binding.node

用于 vue 项目构建时指定本地路径,构建打包镜像复制到 /app 目录

构建 node 打包镜像

将上面的 Dockerfile,sources.list,linux-x64-83_binding.node 放到 linux 同一目录中,执行 docker build 命令 打包自定义镜像即可

docker build --rm  -t mynode:14.10.1 .

打包镜像完成,接下来将使用此镜像进行 vue 项目的打包生成 dist 部署文件

使用 docker 构建的自定义 node 镜像打包 vue 项目

将项目顶级目录 src 映射到容器中的 /src ,运行刚刚构建的 mynode:14.10.1 镜像并传入打包 vue 相关命令,如果还有下载慢需要加镜像的包,再添加即可。

docker run -i --rm \
--privileged=true \
-e "TZ=Asia/Shanghai" \
-v ./src:/src \
--name build_node_vueproject \
mynode:14.10.1 \
/bin/bash -c 'cd /src
node -v
npm -v
npm set sentrycli_cdnurl=https://cdn.npm.taobao.org/dist/sentry-cli
npm set sass_binary_path=/app/linux-x64-83_binding.node
npm config set registry https://registry.npm.taobao.org
npm i --save-dev [email protected] --sass_binary_path=/app/linux-x64-83_binding.node
npm rebuild node-sass
npm install
npm run prod'

构建生成的容器运行完毕,就可以将 dist 下的构建产物进行发布了

制作 vue 项目发布镜像

Dockerfile.dist,nginx.conf 与上面 Dockerfile 等同级目录,故映射前一步的构建产物 ./src/dist 到镜像中

  • Dockerfile 文件: Dockerfile.dist
FROM nginx:latest
EXPOSE 80
COPY ./src/dist /usr/share/nginx/html
COPY ./nginx.conf /etc/nginx
  • history 模式的 nginx.conf(根据项目调整)
worker_processes auto;

events {
    worker_connections 1024;
}
http {
    server {
        listen       80;
        location / {
            root /usr/share/nginx/html/;
            index index.html;
            try_files $uri $uri/ /index.html;
        }
    }
}

  • 执行构建

docker build --rm -f ./Dockerfile.dist -t vuedist:latest .

运行构建的 vue 镜像

docker run --name myvueproject -d -p 80:80 vuedist:latest

至此,记录结束,踩坑不易,文章更不易,如有错误,也欢迎指教
转载请注明出处:By 易墨

标签:node,npm,Vue,sass,构建,linux,镜像
From: https://www.cnblogs.com/morang/p/docker_build_node_vue_01.html

相关文章

  • abp-vnext-pro 实战(九,前端vue和vben学习)
    vben效果 VbenAdmin(vvbin.cn) 对应的代码在 vue-vben-admin/src/views/demo/page/form/basic/data.tsatmain·vbenjs/vue-vben-admin(github.com){field:'time',component:'RangePicker',label:'起止日期',colProps:{......
  • vue2的源码github下载和本地启动调试源码
    1.下载源码:https://github.com/vuejs/vue2.安装依赖,命令行执行:yarn3.修改package.json的运行脚本scripts里面加上源码map定位的参数,在打包后页面引入使用时可以调试到源码: {"name":"vue",。。。"scripts":{//增加一个start的启动命令加入参数--sourcemap......
  • Vue动态创建组件实例并挂载到body
    方式一importVuefrom'vue'/***@paramComponent组件实例的选项对象*@paramprops组件实例中的prop*/exportfunctioncreate(Component,props){constcomp=new(Vue.extend(Component))({propsData:props}).$mount()document.body.appendChild(......
  • VUE- elementUI使用quill富文本编辑器(编辑文本、上传图片)
    准备工作:安装 yarninstall vue-quill-editormain.js//编辑器importVueQuillEditorfrom'vue-quill-editor'//引入样式import'quill/dist/quill.core.css'import'quill/dist/quill.snow.css'import'quill/dist/quill.bubble.css'......
  • 04.node.js websocket
    一、概念Node.jsWebSocket是一个用于建立实时双向通信的模块。WebSocket协议允许服务器与客户端之间进行全双工通信,其API使用了事件驱动和流式的方式。二、客户端  三、服务端  参考:https://developer.mozilla.org/zh-CN/docs/Web/API/WebSocket ......
  • vue实现大文件上传下载
    ​ 以ASP.NETCoreWebAPI 作后端 API ,用 Vue 构建前端页面,用 Axios 从前端访问后端 API,包括文件的上传和下载。 准备文件上传的API #region 文件上传  可以带参数        [HttpPost("upload")]        publicJsonResultuploadProject(I......
  • vue中,一个参数是一个图片网络地址,当重新上传一个图片替换原来的图片后,地址没变,但是图
    这个问题可能是由于浏览器缓存导致的。为了解决这个问题,你可以向图片的URL地址中添加一个随机参数,以确保每次加载图片时都会从服务器获取最新的图片。你可以使用类似于以下方式来添加随机参数:<img:src="imageUrl+'?timestamp='+Date.now()"/>这样,每次你更新了图片后,URL中......
  • Vue组件缓存之keep-alive正确使用姿势
    先来看一个项目中的需求作为苦逼的前端开发者,我们无时无刻都要面对产品经理提的各种需求,比如下图这个场景场景:从首页的点击导航进入列表页,列表页点击列表进入该数据详情页从详情页返回,希望列表页缓存,不重新渲染数据,这样会提高用户体验。分析一下这样需求,如果是小程序......
  • Electron,VUEJS3,Vite,TypesSript 开发环境配置
    Electron,VUEJS3,Vite,TypesSript开发环境配置项目早期是vue3+vite开发的,后期由于运营需求,要修改为Win安装包。方案还是比较多的:1.WPF-Webview由于目前只需要兼容win,所以可以选择WPF,但WPF需要WebView的,还需要本地架设服务。整体部署比较复杂以及需要熟悉C#与WPF相关开发。2.......
  • vue3 使用 setup 语法糖时,keep-alive 缓存使用 include / exclude 获取组件名
    <template><router-viewv-slot="{Component,route}"><keep-alive:include="['ComponentName']"><component:is="Component":key="route.name"/></keep-alive>......