Skip to content

debain 非root用户docker构建问题 #165

@yinyuscloor

Description

@yinyuscloor

报错信息

~/y/wechat-web-devtools-linux (master)> tools/build-with-docker.sh                        
1001:1001                                                                                                            
v16.20.2                                                                                                             
8.19.4                                                                                                               
npm ERR! code EACCES                                                                                                 
npm ERR! syscall open                                                                                                
npm ERR! path /.npmrc                                                                                                
npm ERR! errno -13                                                                                                   
npm ERR!                                                                                                             
npm ERR! Your cache folder contains root-owned files, due to a bug in                                                
npm ERR! previous versions of npm which has since been addressed.                                                    
npm ERR!                                                                                                             
npm ERR! To permanently fix this problem, please run:                                                                
npm ERR!   sudo chown -R 1001:1001 "/.npm"                                                                           
                                                                                                                     
npm ERR! Log files were not written due to an error writing to the directory: /.npm/_logs                            
npm ERR! You can rerun the command with `--loglevel=verbose` to see the logs in your terminal                        
 失败  command: npm config set prefix "$root_dir/cache/npm/node_global"                                              
  at ./tools/setup-wechat-devtools.sh:65

问题原因

  1. Docker容器以非root用户(1001:1001)运行
  2. npm默认使用$HOME/.npm作为缓存目录
  3. 当$HOME未正确设置时,npm可能使用根目录/作为基准路径
  4. 容器内的/.npm目录属于root用户,1001用户无写入权限

具体修改

tools/build-with-docker.sh第15行

修改前:

docker run --rm -i \
    -u "$(id -u):$(id -g)" \
    -e "ACTION_MODE=${ACTION_MODE:-false}" \
    -w /workspace \
    -v "$root_dir:/workspace" \
    $image \
    bash ./tools/setup-wechat-devtools.sh $@

修改后:

docker run --rm -i \
    -u "$(id -u):$(id -g)" \
    -e "ACTION_MODE=${ACTION_MODE:-false}" \
    -e "HOME=/tmp/home" \  # 新增此行
    -w /workspace \
    -v "$root_dir:/workspace" \
    $image \
    bash ./tools/setup-wechat-devtools.sh $@

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions