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

APo*_*031 24 nginx docker reactjs docker-compose

我正在尝试使用 nginzx、flask 和 react 创建 docker-compose 设置。我使用 react-create-app ( https://github.com/facebook/create-react-app )启动了我的 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"]
Run Code Online (Sandbox Code Playgroud)

撰写脚本是:

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:
Run Code Online (Sandbox Code Playgroud)

烧瓶和 nginx 容器启动良好,反应的输出是:

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
Run Code Online (Sandbox Code Playgroud)

APo*_*031 65

添加: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
Run Code Online (Sandbox Code Playgroud)

  • 成功了!https://github.com/facebook/create-react-app/issues/8688 (2认同)

小智 13

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


小智 5

Ran into same issue. Below mentioned steps resolved my problem: 1. add stdin_open: true

version: '3.1'

services:
  nginx:
    .
    .
  react:
    stdin_open: true
    .
    .
Run Code Online (Sandbox Code Playgroud)

2. Do not forget to build the container again after the above mentioned change is made.

docker-compose down
docker-compose up --build
Run Code Online (Sandbox Code Playgroud)