create-react-app icon indicating copy to clipboard operation
create-react-app copied to clipboard

[React-Scripts] v3.4.1 fails to start in Docker

Open timcosta opened this issue 6 years ago • 45 comments

Describe the bug

I updated from [email protected] to [email protected] and when I attempted to restart my Docker container using the new version it exited with code 0.

Recreating core-ui_web_1 ... done
Attaching to core-ui_web_1
web_1        |
web_1        | > [email protected] start /app
web_1        | > react-app-rewired start
web_1        |
web_1        | ℹ 「wds」: Project is running at http://172.24.0.6/
web_1        | ℹ 「wds」: webpack output is served from
web_1        | ℹ 「wds」: Content not from webpack is served from /app/public
web_1        | ℹ 「wds」: 404s will fallback to /
web_1        | Starting the development server...
web_1        |
core-ui_web_1 exited with code 0

This is all the logs had, but running docker-compose ps showed an exit code of 0. I tried running with/without react-app-rewired to rule that out as an issue, but the only thing that solved the issue was downgrading to ~3.3.0 again.

One weirdness I see that is different from 3.3.0 and 3.4.1 is the webpack output is served from line is blank in 3.4.1 but says / in 3.3.0.

Did you try recovering your dependencies?

I ran a fresh Docker rebuild with no cache using node:erbium as the base image to ensure there was no corruption. No luck.

Which terms did you search for in User Guide?

Docker, exit code 0.

Environment

Ran in my docker container to ensure the output is accurate.

➜  dc run web npx create-react-app --info
npx: installed 99 in 7.075s

Environment Info:

  current version of create-react-app: 3.4.1
  running from /root/.npm/_npx/6/lib/node_modules/create-react-app

  System:
    OS: Linux 4.9 Debian GNU/Linux 9 (stretch) 9 (stretch)
    CPU: (4) x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
  Binaries:
    Node: 12.13.1 - /usr/local/bin/node
    Yarn: 1.19.1 - /usr/local/bin/yarn
    npm: 6.12.1 - /usr/local/bin/npm
  Browsers:
    Chrome: Not Found
    Firefox: Not Found
  npmPackages:
    react: ^16.12.0 => 16.12.0
    react-dom: ^16.12.0 => 16.12.0
    react-scripts: ^3.4.1 => 3.4.1
  npmGlobalPackages:
    create-react-app: Not Found

Steps to reproduce

  1. Install CRA with [email protected]
  2. Run in docker container
  3. Exits with code 0

Expected behavior

I expected the server to start.

Actual behavior

The server exited with status code 0 and no output after Starting the development server...

Reproducible demo

All CRA settings are left as their default. No CLI flags or env vars other than NODE_ENV=development.

Here's my Dockerfile:

# create base image with all packages, proper entrypoint, and directories created
FROM node:erbium AS base

# install any packages we need from apt here
RUN apt-get update \
    && apt-get install dumb-init

# set entrypoint to `dumb-init` as it handles being pid 1 and forwarding signals
# so that you dont need to bake that logic into your node app
ENTRYPOINT ["dumb-init", "--"]

# all of our code will live in `/app`
WORKDIR /app

# using the base image, create an image containing all of our files
# and dependencies installed, devDeps and test directory included
FROM base AS dependencies

COPY package*.json ./
RUN npm set progress=false \
    && npm config set depth 0 \
    && npm i
COPY ./config-overrides.js ./
COPY ./public ./public
COPY ./src ./src

# if you have any build scripts to run, like for the `templated-site` flavor
# uncomment and possibly modify the following RUN command:
# RUN npm run build
# keeping all of the bash commands you can within a single RUN is generally important,
# but for this case it's likely that we want to use the cache from the prune which will
# change infrequently.

# test running image using all of the files and devDeps
FROM dependencies AS test
USER node
ENV NODE_ENV=test
# use `sh -c` so we can chain test commands using `&&`
CMD ["npm", "test"]

FROM dependencies AS development

# expose port 3000 from in the container to the outside world
# this is the default port defined in server/manifest.js, and
# will need to be updated if you change the default port
EXPOSE 3000
CMD ["npm", "start"]

# release ready image, devDeps are pruned and tests removed for size control
FROM development AS release

RUN npm prune --production \
    rm -rf lib/**/*.spec.js

timcosta avatar Mar 21 '20 17:03 timcosta

After digging into the source code, I believe the issue is that https://github.com/facebook/create-react-app/blob/7e6d6cd05f3054723c8b015c813e13761659759e/packages/react-scripts/config/webpackDevServer.config.js#L90 combined with https://github.com/facebook/create-react-app/blob/4784997f0682e75eb32a897b4ffe34d735912e6c/packages/react-dev-utils/getPublicUrlOrPath.js#L40 is causing the publicPath passed to webpack to be undefined.

timcosta avatar Mar 21 '20 17:03 timcosta

Update: using customize-cra i forced the publicPath to be set to / which fixed nothing being shown in the output, but i still get exit code 0.

timcosta avatar Mar 21 '20 18:03 timcosta

Ah, https://github.com/facebook/create-react-app/commit/7e6d6cd05f3054723c8b015c813e13761659759e is the cause. When I set CI=true in my docker setup the container starts without issue. I believe that this should not cause the process to exit because it likely breaks running CRA in docker for many people in v3.4.0+.

timcosta avatar Mar 21 '20 18:03 timcosta

For anyone else that stumbles on this, instead of using docker-compose up to start your CRA service with ^3.4.0, use docker-compose run which will attach your TTY to the container and prevent the breakage.

It is impossible to run in the background now without the CI flag enabled.

timcosta avatar Mar 21 '20 18:03 timcosta

Same here with docker run. Downgrading react-scripts to 3.3.0 was not a valid solution because of this other issue https://github.com/facebook/create-react-app/issues/8499 Downgrading react-scripts to 3.4.0 works.

astral-horizon-labs avatar Mar 21 '20 19:03 astral-horizon-labs

Adding stdin_open: true to the docker-compose file solved the issue for me

edsondewes avatar Mar 21 '20 22:03 edsondewes

Adding stdin_open: true or tty: true to the docker-compose file works.

stdin_open stands for interactive and tty means terminal. If you do a quick google search you find this: Screen Shot 2020-03-24 at 11 22 29

7e6d6cd clearly indicates a check for interactivity.

docker run by default allocates tty. So that's why that works. https://docs.docker.com/compose/reference/run/

Docker's documentation says: "For interactive processes (like a shell), you must use -i -t together in order to allocate a tty for the container process."

https://docs.docker.com/engine/reference/run/#foreground

ivorscott avatar Mar 22 '20 05:03 ivorscott

The dev environment can be started, but the production environment is blank on browser

ainannan2010 avatar Mar 22 '20 10:03 ainannan2010

I am a user of CircleCI, I use a cypress orb to run my e2e tests CI=true yarn start is not enough, the server is closed

CircleCI received exit code 0

Rollbacking to 3.4.0 make it works (I was using the 3.3.1 version before and it worked)

In my case I do not have much control over the docker being runned

callain avatar Mar 23 '20 15:03 callain

This is (I think) the cause: https://github.com/facebook/create-react-app/pull/7203

Judging by this and that docker containers by default are not interactive, that is why this is currently broken and setting tty: true fixes that.

JirkaChadima avatar Mar 24 '20 09:03 JirkaChadima

I see lot of workarounds here. It's very unfortunate that step up from 3.4.0 to 3.4.1 (very minor improvement... yeap) causes that many of developers looses hours for additional reasearch to find out what stopped working - exit code 0... Sure, adding the stdin_open: true helped but is it somewhere in documentation or some notification in logs?

rozacek avatar Mar 24 '20 10:03 rozacek

Adding stdin_open: true to the docker-compose file solved the issue for me

It's working for me thanks!!

murilopucci avatar Mar 24 '20 14:03 murilopucci

upgrading to 3.4.1 with the change made from #7203 is also causing my circle ci puppeteer pipelines to break by closing the process. will start looking for workarounds.

chadacious avatar Mar 24 '20 15:03 chadacious

Issue also affects kubernetes deployments based off of using the npm start command inside a docker image. an exit 0 is returned causing the pod to enter crashloopback. adding the CI=true to the .env file fixes the issue. downgrading to 3.4.0 also resolves this behavior.

MHarris021 avatar Mar 25 '20 18:03 MHarris021

Adding stdin_open: true worked for me.

@rozacek this actually is in the release notes here: https://github.com/facebook/create-react-app/releases

#7203 Closes webpack dev server and exits process on "end" stdin (@kelseyleftwich)

I agree that 3.4.1 has caused quite a bit of chaos for a minor version bump. It also updates to babel 7.9 which is significant as it adds new syntax to typescript for re-exports.

nickretallack avatar Mar 28 '20 11:03 nickretallack

Adding stdin_open: true worked for me too.

rafaelluz-f avatar Mar 30 '20 05:03 rafaelluz-f

I can't use stdin_open: true as i'm not using docker-compose,

I'm trying to run cypress tests in Circle CI and CI=true makes no difference either.

varjmes avatar Mar 30 '20 18:03 varjmes

I think I might be echoing some of the other users. If this is just for a single Docker container, that's run with Dockerfile, is there a way to set this option? Is the preferred way to migrate to docker-compose?

oconnelc avatar Apr 01 '20 00:04 oconnelc

Adding stdin_open: true worked for me.

@rozacek this actually is in the release notes here: https://github.com/facebook/create-react-app/releases

#7203 Closes webpack dev server and exits process on "end" stdin (@kelseyleftwich)

I agree that 3.4.1 has caused quite a bit of chaos for a minor version bump. It also updates to babel 7.9 which is significant as it adds new syntax to typescript for re-exports.

I added it and then the hot reload stopped working, did you have that issue too?

oosswwaalldd avatar Apr 03 '20 00:04 oosswwaalldd

As others have mentioned I can't get Cypress to start the dev server in CircleCI (which runs in a docker container without tty) with 3.4.1. The output of the test is a little different than the OP, In fact, it returns exit code 1; but I think it's the same issue. While we add CI=true in our script below, CircleCI already sets it by default:

> CI=true BROWSER=none PORT=60009 react-scripts start

ℹ 「wds」: Project is running at http://192.168.80.3/
ℹ 「wds」: webpack output is served from 
ℹ 「wds」: Content not from webpack is served from /root/repo/public
ℹ 「wds」: 404s will fallback to /
Setting NODE_PATH to resolve modules absolutely has been deprecated in favor of setting baseUrl in jsconfig.json (or tsconfig.json if you are using TypeScript) and will be removed in a future major release of create-react-app.

Starting the development server...

Error: server closed unexpectedly
    at ChildProcess.onClose (/root/repo/node_modules/start-server-and-test/src/index.js:65:14)
    at ChildProcess.emit (events.js:311:20)
    at maybeClose (internal/child_process.js:1021:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:286:5)
error Command failed with exit code 1.

If I ssh into the machine in CircleCI and then start the Cypress tests they run without issue. In this case there is a tty because I'm connected via SSH.

Downgrading react-scripts to 3.4.0 works but that has security vulnerabilities in its dependencies (acorn, minimist).

jeremywadsack avatar Apr 03 '20 20:04 jeremywadsack