You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker中使用Bun构建项目时PostCSS配置加载失败求助

Docker中用Bun构建项目时PostCSS配置加载失败(EBADF)的解决办法

问题概况

  • 使用oven/bun镜像在Docker环境构建项目,本地构建完全正常
  • 构建时反复出现PostCSS配置加载失败错误,错误码EBADF,对应的系统调用是fstat
  • 项目已将package.json设为module,根目录存在postcss.config.cjs配置文件

可行解决方案

1. 显式指定PostCSS配置路径

在Vite配置文件中直接导入并指定PostCSS配置,绕过自动搜索逻辑,避免文件描述符异常。修改vite.config.js或vite.config.ts:

import { defineConfig } from 'vite';
import postcssConfig from './postcss.config.cjs';

export default defineConfig({
  css: {
    postcss: postcssConfig,
  },
});

2. 更换Bun镜像版本

EBADF错误可能和特定版本Bun与Docker的兼容性有关,尝试切换到稳定版镜像,比如在Dockerfile中修改:

FROM oven/bun:1.0.20

3. 检查文件复制与权限

  • 确保postcss.config.cjs被正确复制到容器工作目录,在Dockerfile中添加:
    COPY postcss.config.cjs /tmp/frontend/
    
  • 修复文件权限,避免Bun进程无读取权限:
    RUN chmod 644 /tmp/frontend/postcss.config.cjs
    

4. 确认PostCSS配置导出格式

虽然项目用ES Module,但postcss.config.cjs是CommonJS格式,确保导出正确:

module.exports = {
  plugins: [
    require('autoprefixer'),
    // 其他所需插件
  ],
};

原始构建日志

#9 0.415 $ bunx --bun vite build                                                                                                                                 
#9 0.895 vite v4.4.9 building for production...                                                                                                                  
#9 0.913 Failed to load PostCSS config: Failed to load PostCSS config (searchPath: /tmp/frontend): [EBADF] Bad file descriptor                                   
#9 0.913 undefined
#9 0.913    code: "EBADF"
#9 0.913  syscall: "fstat"
#9 0.913    errno: -9
#9 0.913 
#9 0.913 Failed to load PostCSS config: Failed to load PostCSS config (searchPath: /tmp/frontend): [EBADF] Bad file descriptor
#9 0.913 undefined
#9 0.913    code: "EBADF"
#9 0.913  syscall: "fstat"
#9 0.913    errno: -9
#9 0.913 
#9 0.913 Failed to load PostCSS config: Failed to load PostCSS config (searchPath: /tmp/frontend): [EBADF] Bad file descriptor
#9 0.913 undefined
#9 0.913    code: "EBADF"
#9 0.913  syscall: "fstat"
#9 0.913    errno: -9
#9 0.913 
#9 0.913 Failed to load PostCSS config: Failed to load PostCSS config (searchPath: /tmp/frontend): [EBADF] Bad file descriptor
#9 0.913 undefined
#9 0.913    code: "EBADF"
#9 0.913  syscall: "fstat"
#9 0.913    errno: -9
#9 0.913 
#9 0.938 transforming...
#9 1.075 ✓ 6 modules transformed.
#9 1.075 ✓ built in 178ms
#9 1.075 [vite:css] Failed to load PostCSS config (searchPath: /tmp/frontend): [EBADF] Bad file descriptor
#9 1.075 undefined
#9 1.075 file: /tmp/frontend/src/css/tables.css
#9 1.075 error during build:
#9 1.075 
#9 1.114 error: "vite" exited with code 1 (SIGHUP)
#9 1.115 error: script "build" exited with code 1 (SIGHUP)

内容的提问来源于stack exchange,提问作者Jakub Hájek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 21:09:59