Docker运行React应用镜像,但进程提前退出。为什么?

huangapple go评论71阅读模式
英文:

Docker run react-app image, but process exited early. Why?

问题

以下是翻译好的内容,代码部分不做翻译:

FROM node
WORKDIR /app
COPY . /app
RUN npm install
CMD ["npm", "start"]

我使用这个Dockerfile来构建镜像。在执行docker run -p 3000:80 image_name后,返回以下内容:

docker run -p 3000:80 lifestyle-app

> lifestyle-report@0.1.0 start
> react-scripts start

(node:28) [DEP_WEBPACK_DEV_SERVER_ON_AFTER_SETUP_MIDDLEWARE] DeprecationWarning: 'onAfterSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.
(Use `node --trace-deprecation ...` to show where the warning was created)
(node:28) [DEP_WEBPACK_DEV_SERVER_ON_BEFORE_SETUP_MIDDLEWARE] DeprecationWarning: 'onBeforeSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.
Starting the development server...

The build failed because the process exited too early. This probably means the system ran out of memory or someone called `kill -9` on the process.

我理解这与内存有关,但我的问题是指的是哪种内存,因为如果我在没有Docker的情况下运行npm start,React应用程序运行正常。请帮助,谢谢🙏。

英文:
FROM node

WORKDIR /app

COPY . /app

RUN npm install

CMD ["npm", "start"]

I used this dockerfile to build the image. Upon proceeding with docker run -p 3000:80 image_name, it returns this

docker run -p 3000:80 lifestyle-app

> lifestyle-report@0.1.0 start
> react-scripts start

(node:28) [DEP_WEBPACK_DEV_SERVER_ON_AFTER_SETUP_MIDDLEWARE] DeprecationWarning: 'onAfterSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.
(Use `node --trace-deprecation ...` to show where the warning was created)
(node:28) [DEP_WEBPACK_DEV_SERVER_ON_BEFORE_SETUP_MIDDLEWARE] DeprecationWarning: 'onBeforeSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.
Starting the development server...

The build failed because the process exited too early. This probably means the system ran out of memory or someone called `kill -9` on the process.

I understood it had something to do with memory, but my question is which memory, because, if I ran npm start without docker, the react app is running just fine. Please help, thanks 🙏

答案1

得分: 1

这与内存无关。因为您没有在命令行中指定 -d 以启用分离模式,所以Docker在主要的npm进程退出后立即退出(npm start会创建一个子进程来运行您的JS脚本)。当Docker退出时,它会终止容器中的所有其他进程,包括运行您的脚本的子进程,因此错误消息中包含了 kill -9 部分。除非您只想执行一次性命令,否则几乎总是应该使用 -d 选项来运行Docker应用程序。

只需执行 docker run -d -p 3000:80 lifestyle-app

英文:

This has nothing to do with memory. Because you didn't specify -d in your command line to enable detached mode, Docker exited immediately after the the main npm process exited (npm start will create a subprocess to run your JS script). When Docker exits, it kills all the other processes in the container, including the subprocess running your script, thus the kill -9 part in the error message. You should almost always run Docker apps with -d option, unless you only wish to execute a one-off command.

Just execute docker run -d -p 3000:80 lifestyle-app.

huangapple
  • 本文由 发表于 2023年2月19日 02:48:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495623.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定