React 应用程序退出 docker 容器,退出代码为 0

IT技术 reactjs docker nginx docker-compose
2021-04-16 01:28:59

我正在尝试使用 nginzx、flask 和 react 创建一个 docker-compose 设置。我使用 react-create-app ( https://github.com/facebook/create-react-app )启动了我的 react 应用程序,但尚未对其进行任何更改。

我的react应用程序的 Dockerfile 是:

FROM node:10

WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./
RUN npm install --verbose

# Bundle app source
COPY . .


EXPOSE 3000
CMD ["npm", "start"]

撰写脚本是:

version: '3.1'

services:
    nginx:
        image: nginx:1.15
        container_name: nginx
        volumes:
            - ../:/var/www
            - ./nginx-dev.conf:/etc/nginx/conf.d/default.conf
        ports:
            - 80:80
        networks:
            - my-network
        depends_on:
            - flask
            - react
    react:
        build:
            context: ../react-app/
            dockerfile: ./Dockerfile
        container_name: react
        volumes:
            - ../react-app:/usr/src/app
        networks:
            my-network:
                aliases:
                    - react-app
        expose:
            - 3000
        ports:
            - "3000:3000"
    flask:
        ...
networks:
    my-network:

flask和 nginx 容器启动良好,react的输出是:

react    | 
react    | > react-app@0.1.0 start /usr/src/app
react    | > react-scripts start
react    | 
react    | ℹ 「wds」: Project is running at http://my-ip-address/
react    | ℹ 「wds」: webpack output is served from 
react    | ℹ 「wds」: Content not from webpack is served from /usr/src/app/public
react    | ℹ 「wds」: 404s will fallback to /
react    | Starting the development server...
react    | 
react    | 
react    | npm verb lifecycle react-app@0.1.0~start: unsafe-perm in lifecycle true
react    | npm verb lifecycle react-app@0.1.0~start: PATH: /usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/usr/src/app/node_modules/.bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
react    | npm verb lifecycle react-app@0.1.0~start: CWD: /usr/src/app
react    | npm info lifecycle react-app@0.1.0~poststart: react-app@0.1.0
react    | npm verb exit [ 0, true ]
react    | npm timing npm Completed in 1727ms
react    | npm info ok 
react exited with code 0
4个回答

添加:stdin_open: true到我的 docker-compose 文件的 React 组件修复了我的问题。

例子:

version: '3.1'

services:
    react:
        build:
            context: ../react-app/
            dockerfile: ./Dockerfile
        container_name: react
        volumes:
            - ../react-app:/usr/src/app
        networks:
            my-network:
                aliases:
                    - react-app
        expose:
            - 3000
        ports:
            - "3000:3000"
        stdin_open: true

它看起来像是 [React-Scripts] v3.4.1 的问题。请查看此链接

是的,这是非常相关的。非常感谢你。我将发布修复我的设置的片段。
2021-05-30 01:28:59

遇到同样的问题。下面提到的步骤解决了我的问题: 1. 添加stdin_open: true

version: '3.1'

services:
  nginx:
    .
    .
  react:
    stdin_open: true
    .
    .

2. 完成上述更改后,不要忘记再次构建容器。

docker-compose down
docker-compose up --build

由于问题出在 react-scripts 版本 > 3.4.0 以及从 sandeep-reddy 发布的链接,您可以通过在 Dockerfile 中使用这些行来固定安装的版本(信用 Michael Herman)。

编辑:看起来您仍然需要stdin_open:truedocker-compose.yml 文件中的行才能使其工作。

RUN npm ci
RUN npm install react-scripts@3.4.0 -g --silent
我认为最初的解决方案会起作用,因为我看到它在其他地方被使用而没有正确测试它。这就是为什么我后来做了更正。如果这样更好,我可以删除我的答案
2021-05-22 01:28:59
如果您仍然需要,为什么要将此作为新答案发布stdin_open: true
2021-05-30 01:28:59