Docker-Compose启动失败:node-sass相关问题求助
问题
我尝试用Docker启动项目,执行docker-compose up时出错。从GitHub主仓库拉取代码后,推送分支会基于分支名生成项目子域名,但我的环境无法生成min.js等文件,同事的环境却能正常生成。
错误日志
$ docker-compose up [+] Building 0.0s (0/0) [+] Running 2/2 ✔ Container web-relaunch-2017_msd-frontend-build-1 Recreated 0.3s ✔ Container web-relaunch-2017_msd-frontend-web-1 Recreated 0.2s Attaching to web-relaunch-2017_msd-frontend-build-1, web-relaunch-2017_msd-frontend-web-1 web-relaunch-2017_msd-frontend-build-1 | npm WARN ws@7.4.2 requires a peer of bufferutil@^4.0.1 but none is installed. You must install peer dependencies yourself. web-relaunch-2017_msd-frontend-build-1 | npm WARN ws@7.4.2 requires a peer of utf-8-validate@^5.0.2 but none is installed. You must install peer dependencies yourself. web-relaunch-2017_msd-frontend-build-1 | npm WARN The package autoprefixer is included as both a dev and production dependency. web-relaunch-2017_msd-frontend-build-1 | npm WARN The package grunt-contrib-clean is included as both a dev and production dependency. web-relaunch-2017_msd-frontend-build-1 | npm WARN The package grunt-contrib-compress is included as both a dev and production dependency. web-relaunch-2017_msd-frontend-build-1 | npm WARN The package grunt-contrib-concat is included as both a dev and production dependency. web-relaunch-2017_msd-frontend-build-1 | npm WARN The package grunt-contrib-copy is included as both a dev and production dependency. web-relaunch-2017_msd-frontend-build-1 | npm WARN The package grunt-contrib-cssmin is included as both a dev and production dependency. web-relaunch-2017_msd-frontend-build-1 | npm WARN The package grunt-contrib-uglify is included as both a dev and production dependency. web-relaunch-2017_msd-frontend-build-1 | npm WARN The package grunt-css-url-rewrite is included as both a dev and production dependency. web-relaunch-2017_msd-frontend-build-1 | npm WARN The package grunt-sass is included as both a dev and production dependency. web-relaunch-2017_msd-frontend-build-1 | npm WARN The package grunt-targethtml is included as both a dev and production dependency. web-relaunch-2017_msd-frontend-build-1 | npm WARN The package grunt-text-replace is included as both a dev and production dependency. web-relaunch-2017_msd-frontend-build-1 | npm WARN The package jit-grunt is included as both a dev and production dependency. web-relaunch-2017_msd-frontend-build-1 | npm WARN The package uglify-save-license is included as both a dev and production dependency. web-relaunch-2017_msd-frontend-build-1 | npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.3.2 (node_modules/fsevents): web-relaunch-2017_msd-frontend-build-1 | npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"}) web-relaunch-2017_msd-frontend-build-1 | npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.13 (node_modules/chokidar/node_modules/fsevents): web-relaunch-2017_msd-frontend-build-1 | npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"}) web-relaunch-2017_msd-frontend-build-1 | web-relaunch-2017_msd-frontend-build-1 | removed 2 packages and audited 1056 packages in 8.042s web-relaunch-2017_msd-frontend-build-1 | found 125 vulnerabilities (2 low, 43 moderate, 65 high, 15 critical) web-relaunch-2017_msd-frontend-build-1 | run `npm audit fix` to fix them, or `npm audit` for details web-relaunch-2017_msd-frontend-build-1 | /usr/local/bin/grunt -> /usr/local/lib/node_modules/grunt-cli/bin/grunt web-relaunch-2017_msd-frontend-build-1 | + grunt-cli@1.4.3 web-relaunch-2017_msd-frontend-build-1 | updated 1 package in 2.474s web-relaunch-2017_msd-frontend-build-1 | web-relaunch-2017_msd-frontend-build-1 | jit-grunt: Plugin for the "sass" task not found. web-relaunch-2017_msd-frontend-build-1 | If you have installed the plugin already, please setting the static mapping. web-relaunch-2017_msd-frontend-build-1 | See https://github.com/shootaroo/jit-grunt#static-mappings web-relaunch-2017_msd-frontend-build-1 | web-relaunch-2017_msd-frontend-build-1 | Warning: Task "sass:development" failed. Use --force to continue. web-relaunch-2017_msd-frontend-build-1 | web-relaunch-2017_msd-frontend-build-1 | Aborted due to warnings.
配置文件
docker-compose.yml
version: '3.3' services: build: build: context: docker/build volumes: - .:/app command: > sh -c "npm install && npm install -g grunt-cli && npm install grunt-sass && grunt dev" web: build: context: docker/web volumes: - type: bind source: ./docker/web/httpd.conf target: /etc/apache2/httpd.conf - type: bind source: ./ target: /var/www/localhost/htdocs consistency: cached read_only: true ports: - 8082:80 command: httpd -D FOREGROUND tty: true
Build服务Dockerfile
FROM node:10.11-alpine # # Install common tools # RUN apk add --no-cache \ bash \ alpine-sdk \ wget \ patch \ rsync \ php \ ruby \ ruby-dev \ ruby-rdoc \ python # Allow npm to run as root RUN npm set unsafe-perm true # global installed grunt cli RUN npm install -g grunt-cli WORKDIR /app CMD ["/bin/bash"]
Web服务Dockerfile
FROM alpine:3.9 RUN apk update RUN apk add bash RUN apk add php7-apache2 php7-session php7-json php7-ctype php7-mbstring WORKDIR /var/www/localhost/htdocs
已尝试重建node-sass、降级Node等方法,均无效,求简单解决方案。
解决方案
核心问题是jit-grunt无法识别sass插件,以下是针对性的修复步骤:
1. 规范依赖安装方式
当前你在启动命令里单独安装grunt-sass,但如果项目package.json里未声明该依赖,jit-grunt无法自动识别。
- 修改
docker-compose.yml中build服务的命令,去掉冗余的全局依赖安装:command: > sh -c "npm install --no-fund --no-audit && grunt dev" - 检查项目根目录
package.json,确保grunt-sass在devDependencies中。如果没有,执行npm install grunt-sass --save-dev后提交到仓库,再重新拉取代码。
2. 给jit-grunt添加静态映射
在项目的Gruntfile.js中手动指定sass插件的路径,避免jit-grunt自动查找失败:
// 方式一:直接加载插件 grunt.loadNpmTasks('grunt-sass'); // 方式二:用jit-grunt的静态映射 require('jit-grunt')(grunt, { sass: 'grunt-sass' });
3. 清理缓存避免冲突
本地和Docker缓存可能导致依赖异常,执行以下操作:
- 删除本地
node_modules和package-lock.json文件 - 执行
docker-compose build --no-cache重建镜像 - 重新启动容器:
docker-compose up
4. 验证Node版本兼容性
你使用的Node 10.11需要匹配兼容的grunt-sass版本,建议在package.json中明确指定兼容版本:
"grunt-sass": "^3.1.0"
内容的提问来源于stack exchange,提问作者D'arganatan
相关产品推荐
相关产品推荐

