简体   繁体   English

使用 npm init react-app 时,Docker 容器在启动后立即退出

[英]Docker container exiting immediately after starting when using npm init react-app

I am trying to start a Docker container with a react project, the project is created using npm init react-app.我正在尝试使用 react 项目启动 Docker 容器,该项目是使用 npm init react-app 创建的。

This is my docker file这是我的 docker 文件

# Specify a base image
FROM node:alpine

WORKDIR /usr/app

# Install some depenendencies
COPY ./package.json ./
RUN npm install
COPY ./ ./

# Default command
CMD ["npm", "run", "start"]

Docker build . creates an image successfully (with a lot of npm warnings) and then when I run Docker run <image> this is the output in my terminal成功创建了一个图像(有很多 npm 警告),然后当我运行Docker run <image>这是我终端中的输出

> mytest@0.1.0 start /usr/app
> react-scripts start

ℹ 「wds」: Project is running at http://172.17.0.2/
ℹ 「wds」: webpack output is served from 
ℹ 「wds」: Content not from webpack is served from /usr/app/public
ℹ 「wds」: 404s will fallback to /
Starting the development server...

Soon as it hits Starting the development server... it stops running in my terminal.一旦它点击Starting the development server...它就停止在我的终端中运行。 If I check Docker ps I can see no containers are running, if I run Docker ps -a I can see a container was started up and then exited immediately.如果我检查Docker ps我可以看到没有容器正在运行,如果我运行Docker ps -a我可以看到一个容器已启动然后立即退出。

Docker logs shows the terminal output above, anybody run into this situation? Docker logs显示了上面的终端输出,有人遇到过这种情况吗? Its only with my npm init react-app project, my other nodejs + express projects run fine with the exact same docker file它仅适用于我的 npm init react-app 项目,我的其他 nodejs + express 项目使用完全相同的 docker 文件运行良好

I tried downgrading but it didn't work.我尝试降级,但没有奏效。 What worked for me is in docker-compose file in the react app sector I added:对我docker-compose是我添加的 react 应用程序部门中的docker-compose文件:

stdin_open: true

This solution is also suggested here: issue on github此处也建议使用此解决方案: github 上的问题

docker run -it -p 80:3000 imagename解决了我的问题。

使用-it运行命令对我-it

docker run -it -p 3001:3000 Imageid

I solve the problem.我解决了这个问题。 Inside package.json replace "react-scripts": "3.4.1" to "react-scripts": "3.4.0"在 package.json 中将 "react-scripts": "3.4.1" 替换为 "react-scripts": "3.4.0"

Then rebuild image and It works !然后重建图像,它的工作原理! They mess up something with react-scripts": "3.4.1" Just use version 3.4.0他们搞砸了 react-scripts": "3.4.1" 只需使用 3.4.0 版

So nothing is wrong with the setup turns out there is an open issue for this.因此,设置没有任何问题,结果证明这是一个未解决的问题。

https://github.com/facebook/create-react-app/issues/8688 https://github.com/facebook/create-react-app/issues/8688

Downgrading to 3.4 solves for now现在降级到 3.4 解决

Ran into the same issue.遇到了同样的问题。 -it flag resolved the issue. -it标志解决了这个问题。

docker run -it -p 3001:3001 <image-id>

I got the same issue as you and just ran the above command to fix it:我遇到了和你一样的问题,只是运行上面的命令来修复它:

sudo docker run -it -p 3001:3000 Image-Name

Hope it helps希望能帮助到你

您也可以将以下内容与-itd标志一起使用。

sudo docker run -itd -p 3001:3000 Image-Name

Using the suggestion mentioned in this ans.使用此ans 中提到的建议 on stackoverflow and on github , of adding在 stackoverflow 和github 上,添加

ENV CI=true to the Dockerfile before CMD ["npm", "run", "start"] , worked. ENV CI=trueCMD ["npm", "run", "start"]之前对 Dockerfile 起作用。

I'm not sure why this works.我不确定为什么会这样。 It may have something to do with this point.这可能与一点有关。 You can read more about the env CI=true in the react docs .您可以在 react docs 中阅读有关 env CI=true更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 npm 安装 create-react-app 时出错,使用 npm init react-app 时出错 - error installing create-react-app using npm & error when using npm init react-app 为什么`npm init react-app`默认使用yarn? (npm V6.14.5) - Why is `npm init react-app` using yarn as default? (npm V6.14.5) 将 function 组件更改为 class 组件时,`npm init react-app` 导致 `'React' is not defined no-undef` - `npm init react-app` leads to `'React' is not defined no-undef` when changing function component to class component docker-compose 在启动使用 create-react-app 创建的 react 应用程序后立即停止 - docker-compose stops immediately after starting react app created using create-react-app cd react-app后在react js中使用npm start时出错 - Error while using npm start in react js after cd react-app 创建 React 应用程序失败。 我使用了 npm init react-app my-app - Creating React app failed. I used npm init react-app my-app React 应用程序退出 docker 容器,退出代码为 0 - React app exiting in docker container with exit code 0 init react-app中的软件包在哪里? - Where are the packages in init react-app? 如何在 docker-compose 中制作 React 应用程序? 构建步骤完成后容器正在退出 - How do I make a react app in docker-compose? Container is exiting after build steps are complete 为 react-app 运行 docker 容器时出现问题,其中 package.json 在子文件夹中 - Trouble running a docker container for react-app where package.json is in a subfolder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM