DEV Community

loading...
Cover image for DOCKER - How To Resolve "react exited with code 0"

DOCKER - How To Resolve "react exited with code 0"

igmrrf profile image Richard Francis Updated on ・2 min read

Resolving "react exit with code 0" when running a React container

Exit Code

I guess before running into this error on your Docker instance I presume you have basic knowledge of Docker and as such won't need to explain or give basic explanation about Docker and rather go straight to the solution of the aforementioned problem.

The Error

The error is met when you run the below command on a set of project(s) that include a React App.

docker-composer up
Enter fullscreen mode Exit fullscreen mode

or try to start a React App container from an image on your Docker instance.

For the simplest solution, Skip to Option 5 😉

Option 1

(While Running Multiple Containers)

Add the configuration "tty:true" to your react instance

    react:
            tty: true //NOTE
            build: dockerreact
            ports: - "3000:3000"
Enter fullscreen mode Exit fullscreen mode

Option 2

(While Running Multiple Containers)

Setting "stdin_open:true" to your react instance

    react:
            stdin_open: true //NOTE:
            build: dockerreact
            ports: - "3000:3000"
Enter fullscreen mode Exit fullscreen mode

Option 3

(While Running Multiple Containers or Single React Container)

Add "CI=true" as a command that should be executed before "npm install" in your Dockerfile within your React project root directory

FROM node:14.5

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY package.json /usr/src/app

RUN CI=true //NOTE

RUN npm install
COPY . /usr/src/app

EXPOSE 3000
CMD ["npm","start"]
Enter fullscreen mode Exit fullscreen mode

Option 4

(While Running Multiple Containers or Single React Container)

Use "docker-compose run" instead of "docker-compose up" as this automatically sets either option 1 or 2 as true

docker-compose run
Enter fullscreen mode Exit fullscreen mode

NOTE: This is only works on already built docker images

Option 5

(While Running Multiple Containers or Single React Container)

Lastly, Downgrade your "react scripts" version to 3.4.0

NOTE: Downgrading to 3.3.0 or lower wont solve this error and might bring up other issues on your react project

"dependencies": {
        "@testing-library/jest-dom": "^4.2.4",
        "@testing-library/react": "^9.3.2",
        "@testing-library/user-event": "^7.1.2",
        "react": "^16.13.1",
        "react-dom": "^16.13.1",
        "react-scripts": "3.4.0,  //NOTE:
        "react-scripts": "3.4.1" //NOT WORKING
},
Enter fullscreen mode Exit fullscreen mode

Discussion

pic
Editor guide
Collapse
sahithya87 profile image
sahithya87

Hi,I am running a React App in ECS .The docker image has been created and pushed to ECR through jenkins pipeline.The ECS container gets created through terraform code. But the container gets exited with code 0 and new one starts.This became a loop.When i try creating a container manually with commands from the same image,then the container remains active.
When i check the logs from cloudwatch i get the logs as
content not from webpack is served from /app/public
404s will fallback to /

Can i get some help on this?

Collapse
igmrrf profile image
Richard Francis Author

Have you tried making the changes as stated in the post to your Docker file?

Collapse
helengu profile image
Helen Gu

Thank you! Option 3 doesn't work for me but option 5 does~

Collapse
dfrase profile image