将react应用程序作为docker容器运行时,找不到react脚本



我有一个react应用程序,我正在尝试对其进行dockerize。以下是我的Dockerfile 的内容

FROM node:alpine
WORKDIR /src
COPY package.json /src
RUN npm install
COPY . /src
EXPOSE 3000
CMD ["npm", "run", "start"]

在构建docker文件时,我会遇到以下错误。

> [5/6] RUN npm install:
#9 13.99 npm ERR! code ERESOLVE
#9 13.99 npm ERR! ERESOLVE unable to resolve dependency tree
#9 13.99 npm ERR! 
#9 14.00 npm ERR! While resolving: <project-name>@1.1.6
#9 14.00 npm ERR! Found: react@17.0.2
#9 14.00 npm ERR! node_modules/react
#9 14.00 npm ERR!   react@"^17.0.2" from the root project
#9 14.00 npm ERR! 
#9 14.00 npm ERR! Could not resolve dependency:
#9 14.00 npm ERR! peer react@"^15.0.1 || ^16.0.1" from react-text-loop@2.3.0
#9 14.00 npm ERR! node_modules/react-text-loop
#9 14.00 npm ERR!   react-text-loop@"^2.3.0" from the root project
#9 14.00 npm ERR! 
#9 14.00 npm ERR! Fix the upstream dependency conflict, or retry
#9 14.00 npm ERR! this command with --force, or --legacy-peer-deps
#9 14.00 npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
#9 14.00 npm ERR! 
#9 14.00 npm ERR! See /root/.npm/eresolve-report.txt for a full report.
#9 14.00 
#9 14.00 npm ERR! A complete log of this run can be found in:
#9 14.00 npm ERR!     /root/.npm/_logs/2021-12-08T14_34_26_174Z-debug-0.log

然后,我将RUN命令更改为使用RUN npm i -g npm,如下所示。

FROM node:alpine
WORKDIR /src
COPY package.json /src
RUN npm i -g npm
COPY . /src
EXPOSE 3000
CMD ["npm", "run", "start"]

这似乎可以在没有错误的情况下构建映像,但是,当我尝试将docker映像作为容器运行时,容器退出,在获取日志时,我看到以下错误

> <projectname>-ui@1.1.6 start
> react-scripts start
sh: react-scripts: not found

更新

当我使用RUN npm i --legacy-peer-deps时,我得到以下错误

#10 103.3 npm notice 
#10 103.3 npm notice New minor version of npm available! 8.1.4 -> 8.2.0
#10 103.3 npm notice Changelog: <https://github.com/npm/cli/releases/tag/v8.2.0>
#10 103.3 npm notice Run `npm install -g npm@8.2.0` to update!
#10 103.3 npm notice 
#10 103.3 npm ERR! code 1
#10 103.3 npm ERR! path /src/node_modules/node-sass
#10 103.3 npm ERR! command failed
#10 103.3 npm ERR! command sh -c node scripts/build.js
#10 103.3 npm ERR! Building: /usr/local/bin/node /src/node_modules/node-gyp/bin/node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags= --libsass_library=
#10 103.3 npm ERR! gyp info it worked if it ends with ok
#10 103.3 npm ERR! gyp verb cli [
#10 103.3 npm ERR! gyp verb cli   '/usr/local/bin/node',
#10 103.3 npm ERR! gyp verb cli   '/src/node_modules/node-gyp/bin/node-gyp.js',
#10 103.3 npm ERR! gyp verb cli   'rebuild',
#10 103.3 npm ERR! gyp verb cli   '--verbose',
#10 103.3 npm ERR! gyp verb cli   '--libsass_ext=',
#10 103.3 npm ERR! gyp verb cli   '--libsass_cflags=',
#10 103.3 npm ERR! gyp verb cli   '--libsass_ldflags=',
#10 103.3 npm ERR! gyp verb cli   '--libsass_library='
#10 103.3 npm ERR! gyp verb cli ]
#10 103.3 npm ERR! gyp info using node-gyp@7.1.2
#10 103.3 npm ERR! gyp info using node@17.2.0 | linux | x64
#10 103.3 npm ERR! gyp verb command rebuild []
#10 103.3 npm ERR! gyp verb command clean []
#10 103.3 npm ERR! gyp verb clean removing "build" directory
#10 103.3 npm ERR! gyp verb command configure []
#10 103.3 npm ERR! gyp verb find Python Python is not set from command line or npm configuration
#10 103.3 npm ERR! gyp verb find Python Python is not set from environment variable PYTHON
#10 103.3 npm ERR! gyp verb find Python checking if "python3" can be used
#10 103.3 npm ERR! gyp verb find Python - executing "python3" to get executable path
#10 103.3 npm ERR! gyp verb find Python - "python3" is not in PATH or produced an error
#10 103.3 npm ERR! gyp verb find Python checking if "python" can be used
#10 103.3 npm ERR! gyp verb find Python - executing "python" to get executable path
#10 103.3 npm ERR! gyp verb find Python - "python" is not in PATH or produced an error
#10 103.3 npm ERR! gyp verb find Python checking if "python2" can be used
#10 103.3 npm ERR! gyp verb find Python - executing "python2" to get executable path
#10 103.3 npm ERR! gyp verb find Python - "python2" is not in PATH or produced an error
#10 103.3 npm ERR! gyp ERR! find Python 
#10 103.3 npm ERR! gyp ERR! find Python Python is not set from command line or npm configuration
#10 103.3 npm ERR! gyp ERR! find Python Python is not set from environment variable PYTHON
#10 103.3 npm ERR! gyp ERR! find Python checking if "python3" can be used
#10 103.3 npm ERR! gyp ERR! find Python - "python3" is not in PATH or produced an error
#10 103.3 npm ERR! gyp ERR! find Python checking if "python" can be used
#10 103.3 npm ERR! gyp ERR! find Python - "python" is not in PATH or produced an error
#10 103.3 npm ERR! gyp ERR! find Python checking if "python2" can be used
#10 103.3 npm ERR! gyp ERR! find Python - "python2" is not in PATH or produced an error
#10 103.3 npm ERR! gyp ERR! find Python 
#10 103.3 npm ERR! gyp ERR! find Python **********************************************************
#10 103.3 npm ERR! gyp ERR! find Python You need to install the latest version of Python.
#10 103.3 npm ERR! gyp ERR! find Python Node-gyp should be able to find and use Python. If not,
#10 103.3 npm ERR! gyp ERR! find Python you can try one of the following options:
#10 103.3 npm ERR! gyp ERR! find Python - Use the switch --python="/path/to/pythonexecutable"
#10 103.3 npm ERR! gyp ERR! find Python   (accepted by both node-gyp and npm)
#10 103.3 npm ERR! gyp ERR! find Python - Set the environment variable PYTHON
#10 103.3 npm ERR! gyp ERR! find Python - Set the npm configuration variable python:
#10 103.3 npm ERR! gyp ERR! find Python   npm config set python "/path/to/pythonexecutable"
#10 103.3 npm ERR! gyp ERR! find Python For more information consult the documentation at:
#10 103.3 npm ERR! gyp ERR! find Python https://github.com/nodejs/node-gyp#installation
#10 103.3 npm ERR! gyp ERR! find Python **********************************************************
#10 103.3 npm ERR! gyp ERR! find Python 
#10 103.3 npm ERR! gyp ERR! configure error 
#10 103.3 npm ERR! gyp ERR! stack Error: Could not find any Python installation to use
#10 103.3 npm ERR! gyp ERR! stack     at PythonFinder.fail (/src/node_modules/node-gyp/lib/find-python.js:302:47)
#10 103.3 npm ERR! gyp ERR! stack     at PythonFinder.runChecks (/src/node_modules/node-gyp/lib/find-python.js:136:21)
#10 103.3 npm ERR! gyp ERR! stack     at PythonFinder.<anonymous> (/src/node_modules/node-gyp/lib/find-python.js:179:16)
#10 103.3 npm ERR! gyp ERR! stack     at PythonFinder.execFileCallback (/src/node_modules/node-gyp/lib/find-python.js:266:16)
#10 103.3 npm ERR! gyp ERR! stack     at exithandler (node:child_process:404:5)
#10 103.3 npm ERR! gyp ERR! stack     at ChildProcess.errorhandler (node:child_process:416:5)
#10 103.3 npm ERR! gyp ERR! stack     at ChildProcess.emit (node:events:390:28)
#10 103.3 npm ERR! gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:288:12)
#10 103.3 npm ERR! gyp ERR! stack     at onErrorNT (node:internal/child_process:475:16)
#10 103.3 npm ERR! gyp ERR! stack     at processTicksAndRejections (node:internal/process/task_queues:83:21)
#10 103.3 npm ERR! gyp ERR! System Linux 5.10.47-linuxkit
#10 103.3 npm ERR! gyp ERR! command "/usr/local/bin/node" "/src/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
#10 103.3 npm ERR! gyp ERR! cwd /src/node_modules/node-sass
#10 103.3 npm ERR! gyp ERR! node -v v17.2.0
#10 103.3 npm ERR! gyp ERR! node-gyp -v v7.1.2
#10 103.3 npm ERR! gyp ERR! not ok 
#10 103.3 npm ERR! Build failed with error code: 1
#10 103.6 
#10 103.6 npm ERR! A complete log of this run can be found in:
#10 103.6 npm ERR!     /root/.npm/_logs/2021-12-08T14_59_25_774Z-debug.log
------
executor failed running [/bin/sh -c npm i --legacy-peer-deps]: exit code: 1

我做错了什么?

我认为我遇到的问题是我使用了node:alpine基本映像,这导致了Python错误。现在我已经将基本映像更改为node:16.6.1 docker映像构建良好。

相关内容

  • 没有找到相关文章

最新更新