Docker初学部署React项目报错

163 阅读3分钟
  1. 报错: ERROR [2/7] RUN addgroup -S app && adduser -S -G app

     => ERROR [2/7] RUN addgroup -S app && adduser -S -G app                                              0.5s 
    ------
     > [2/7] RUN addgroup -S app && adduser -S -G app:
    0.471 BusyBox v1.36.1 (2024-06-10 07:11:47 UTC) multi-call binary.
    0.471
    0.471 Usage: adduser [OPTIONS] USER [GROUP]
    0.471
    0.471 Create new user, or add USER to GROUP
    0.471
    0.471   -h DIR          Home directory
    0.471   -g GECOS        GECOS field
    0.471   -s SHELL        Login shell
    0.471   -G GRP          Group
    0.471   -S              Create a system user
    0.471   -D              Don't assign a password
    0.471   -H              Don't create home directory
    0.471   -u UID          User id
    0.471   -k SKEL         Skeleton directory (/etc/skel)
    ------
    
     1 warning found (use docker --debug to expand):
     - JSONArgsRecommended: JSON arguments recommended for CMD to prevent unintended behavior related to OS signals (line 37)
    Dockerfile:10
    --------------------
       8 |     # if the app is run as root, any vulnerability in the app can be exploited to gain access to the host system
       9 |     # good pratice to run the app as a non-root user
      10 | >>> RUN addgroup -S app && adduser -S -G app
      11 |     # set user to run app
      12 |     USER app
    --------------------
    ERROR: failed to solve: process "/bin/sh -c addgroup -S app && adduser -S -G app" did not complete successfully: exit code: 1
    

    原因:使用的是 BusyBox 提供的 adduser 工具,其选项与标准的 adduser 略有不同。这些选项定义了如何创建用户及其属性。

    解决: RUN addgroup app && adduser -D -H -G app app

    • addgroup app:创建一个名为 app 的组。
    • adduser -D -H -G app app:创建一个名为 app 的用户,关联到组 app,且不设置 home 目录和密码。
  2. 报错:ERROR [6/7] RUN npm install

    => ERROR [6/7] RUN npm install                                                                      18.5s 
    ------
     > [6/7] RUN npm install:
    18.41 npm error code EACCES
    18.42 npm error syscall mkdir
    18.42 npm error path /home/app
    18.42 npm error errno EACCES
    18.43 npm error FetchError: Invalid response body while trying to fetch <https://registry.npmjs.org/@eslint%2fjs:> EACCES: permission denied, mkdir '/home/app'
    18.43 npm error     at /usr/local/lib/node_modules/npm/node_modules/minipass-fetch/lib/body.js:170:15      
    18.43 npm error     at async Response.json (/usr/local/lib/node_modules/npm/node_modules/minipass-fetch/lib/body.js:75:17)
    18.43 npm error     at async RegistryFetcher.packument (/usr/local/lib/node_modules/npm/node_modules/pacote/lib/registry.js:98:25)
    18.43 npm error     at async RegistryFetcher.manifest (/usr/local/lib/node_modules/npm/node_modules/pacote/lib/registry.js:128:23)
    18.43 npm error     at async #fetchManifest (/usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/build-ideal-tree.js:1199:20)
    18.43 npm error     at async #nodeFromEdge (/usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/build-ideal-tree.js:1037:19)
    18.43 npm error     at async #buildDepStep (/usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/build-ideal-tree.js:901:11)
    18.43 npm error     at async Arborist.buildIdealTree (/usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/build-ideal-tree.js:181:7)
    18.43 npm error     at async Promise.all (index 1)
    18.43 npm error     at async Arborist.reify (/usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/reify.js:131:5) {
    18.43 npm error   code: 'EACCES',
    18.43 npm error   errno: 'EACCES',
    18.43 npm error   syscall: 'mkdir',
    18.43 npm error   path: '/home/app',
    18.43 npm error   type: 'system',
    18.43 npm error   requiredBy: '.'
    18.43 npm error }
    18.43 npm error
    18.43 npm error The operation was rejected by your operating system.
    18.43 npm error It is likely you do not have the permissions to access this file as the current user       
    18.43 npm error
    18.43 npm error If you believe this might be a permissions issue, please double-check the
    18.43 npm error permissions of the file and its containing directories, or try running
    18.43 npm error the command again as root/Administrator.
    18.44 npm notice
    18.44 npm notice New minor version of npm available! 10.8.2 -> 10.9.0
    18.44 npm notice Changelog: <https://github.com/npm/cli/releases/tag/v10.9.0>
    18.44 npm notice To update run: npm install -g npm@10.9.0
    18.44 npm notice
    18.44 npm error Log files were not written due to an error writing to the directory: /home/app/.npm/_logs  
    18.44 npm error You can rerun the command with --loglevel=verbose to see the logs in your terminal 
    

    **原因:**这个错误 EACCES 说明当前用户没有权限在 /home/app 目录下执行 npm install,特别是对 mkdir 操作权限有限制。通常,这种问题发生在你使用非 root 用户(例如 app 用户)时,而该用户没有访问或写入其工作目录的权限。

    解决: RUN mkdir -p /home/app && chown -R app:app /home/app

    • /home/app 目录授予写权限来解决这个问题:确保 app 用户对 /home/app 目录具有足够的权限
  3. 报错:docker run react-docker

    1. 文件名字错误