无法使用node:alpine在Docker容器中运行React应用的开发版本 [英] Unable to run a development build of a React app in a Docker container using node:alpine

查看:112
本文介绍了无法使用node:alpine在Docker容器中运行React应用的开发版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Facebook的 create-react-app 的React应用.在开发过程中,我在特定的本地端口(60001)上运行它.为此,我将 package.json scripts 部分中的默认 start 脚本更改为以下内容:

I have a React app which is based on Facebook's create-react-app. During development I run it on a specific local port (60001). To achieve this I changed the default start script in the scripts part of my package.json to the following:

    "start": "set PORT=60001 && react-scripts start",

我现在需要使用相同的端口在Docker容器中运行它.在我的Dockerfile中,我有两个阶段:

I now have a requirement to run this in a Docker container using the same port. In my Dockerfile I have these two stages:

FROM node:alpine as build
COPY ./package.json /app/package.json
WORKDIR /app
RUN npm install
RUN rm -f .npmrc

FROM build as build-dev
COPY ./public/ /app/public
COPY ./src/ /app/src
EXPOSE 60001
CMD ["npm", "start"]

注意,我公开了我希望npm开发服务器绑定到的容器上的端口.

Notice I'm exposing the port on the container that I expect the npm dev server to bind to.

我按照您的期望构建了映像,目标是上面的 build-dev Docker阶段.

I build the image as you'd expect, targeting the build-dev Docker stage above.

docker build --target build-dev -t my-app:local

然后我像这样运行容器:

I then run the container like this:

docker run -it -p 60001:60001 my-app:local

我希望在我的开发应用程序构建正在运行时,将我的本地端口60001绑定到容器上的同一端口.

The intention is to bind my local port 60001 to the same port on the container when I expect my dev app build to be running.

当我这样做时,我看到确认npm正在运行正确的脚本并且应用程序已编译.但是,浏览到 http://localhost:60001/没有任何帮助.只是一个 ERR_CONNECTION_REFUSED ,好像那里什么都没有.

When I do this I see confirmation that npm is running the correct script and that the app has compiled. However, browsing to http://localhost:60001/ gives me nothing. Just an ERR_CONNECTION_REFUSED, as if there's nothing there.

我看不到我在做什么错.有任何想法吗?我希望我可以从容器日志中获得一些见识,但是当我为此容器运行 docker logs 时,我只看到反应脚本输出确认我的应用已编译.

I can't see what I'm doing wrong here. Any ideas? I'd hoped I might be able to get some insight from the container logs, but when I run docker logs for this container I just see the react scripts output confirming my app compiled.

推荐答案

如果您使用的是 docker-compose ,则添加这一行为我解决了问题...

If you are using docker-compose adding this one line solved the problem for me...

该行是 stdin_open:true .

这是我的 docker-compose.yml 的基本示例:

version: "3.7"

services:
  test-app:
    stdin_open: true
    container_name: test-app
    build: .
    ports:
      - "3000:3000"
    volumes:
      - /app/node_modules
      - .:/app

示例 Dockerfile 供参考:

FROM node:alpine

WORKDIR /app

COPY package.json yarn.lock ./

RUN yarn install

COPY . .

EXPOSE 3000

CMD ["yarn", "start"]

有关此的更多信息,我将检查 create-react-app

For more information on this I would checkout the github issue for create-react-app

GitHub问题

祝你好运!这使我疯了几个小时

Good luck! This drove me nuts for hours

PS不确定如何在生产环境中保持这种状态

PS Not sure how this will hold up in a production build

这篇关于无法使用node:alpine在Docker容器中运行React应用的开发版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆