[英]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=true
在CMD ["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.