44

I am trying to start a Docker container with a react project, the project is created using npm init react-app.

This is my docker file

# Specify a base image
FROM node:alpine

WORKDIR /usr/app

# Install some depenendencies
COPY ./package.json ./
RUN npm install
COPY ./ ./

# Default command
CMD ["npm", "run", "start"]

Docker build . creates an image successfully (with a lot of npm warnings) and then when I run Docker run <image> this is the output in my terminal

> mytest@0.1.0 start /usr/app
> react-scripts start

ℹ 「wds」: Project is running at http://172.17.0.2/
ℹ 「wds」: webpack output is served from 
ℹ 「wds」: Content not from webpack is served from /usr/app/public
ℹ 「wds」: 404s will fallback to /
Starting the development server...

Soon as it hits Starting the development server... it stops running in my terminal. If I check Docker ps I can see no containers are running, if I run Docker ps -a I can see a container was started up and then exited immediately.

Docker logs shows the terminal output above, anybody run into this situation? Its only with my npm init react-app project, my other nodejs + express projects run fine with the exact same docker file

Jason McFarlane
  • 2,048
  • 3
  • 18
  • 30
  • 1
    Can you share the details of your `docker run` command pls? Are you using the `-d` flag? – Software Engineer Mar 21 '20 at 16:46
  • To build `docker build .` it builds successfully and then I am just running `docker run `. It works for my other projects just not for one created with `npm init react-app` I tried it in 2 different react-app repos one ejected and one normal, both dont work. Tried it in my nodejs + express api projects, works fine – Jason McFarlane Mar 21 '20 at 16:55
  • The container will exit as soon as its main process exists. Try running `docker run -it --rm -v $(pwd):/usr/app -w /usr/app node:alpine` then at the prompt try `npm install` followed by `npm start run` (note: this may mess up your source directory so maybe backup your project first). You may get some helpful insights that way. Then, try the same thing but also with `-e SKIP_PREFLIGHT_CHECK=true` on the run line before the image name. – Software Engineer Mar 21 '20 at 17:00
  • 1
    Use ```EXPOSE 3000``` in you Dockerfile and then try to build – Ali Torki Mar 22 '20 at 23:29

9 Answers9

50

I tried downgrading but it didn't work. What worked for me is in docker-compose file in the react app sector I added:

stdin_open: true

This solution is also suggested here: issue on github

Ramy M. Mousa
  • 5,727
  • 3
  • 34
  • 45
27

docker run -it -p 80:3000 imagename resolve my problem.

h01m3s
  • 290
  • 2
  • 5
  • 1
    Based on the question I think this is the right solution since the question doesn't mention docker-compose. – MGLondon Aug 17 '20 at 11:04
12

Running the command with -it worked for me:

docker run -it -p 3001:3000 Imageid
Remolten
  • 2,614
  • 2
  • 25
  • 29
Smita
  • 129
  • 2
7

I solve the problem. Inside package.json replace "react-scripts": "3.4.1" to "react-scripts": "3.4.0"

Then rebuild image and It works ! They mess up something with react-scripts": "3.4.1" Just use version 3.4.0

Flutter Supabase
  • 192
  • 2
  • 10
4

So nothing is wrong with the setup turns out there is an open issue for this.

https://github.com/facebook/create-react-app/issues/8688

Downgrading to 3.4 solves for now

Jason McFarlane
  • 2,048
  • 3
  • 18
  • 30
4

Ran into the same issue. -it flag resolved the issue.

docker run -it -p 3001:3001 <image-id>
Abhishek Sinha
  • 1,129
  • 13
  • 12
  • 1
    I downvoted because this is exactly the same as previous answers such as https://stackoverflow.com/a/61327560/735926 or https://stackoverflow.com/a/61004740/735926 – bfontaine Sep 17 '21 at 10:16
4

I got the same issue as you and just ran the above command to fix it:

sudo docker run -it -p 3001:3000 Image-Name

Hope it helps

1

You can use the below with -itd flag as well.

sudo docker run -itd -p 3001:3000 Image-Name
Tschitsch
  • 1,253
  • 8
  • 18
1

Using the suggestion mentioned in this ans. on stackoverflow and on github, of adding

ENV CI=true to the Dockerfile before CMD ["npm", "run", "start"], worked.

I'm not sure why this works. It may have something to do with this point. You can read more about the env CI=true in the react docs.

shmuels
  • 1,039
  • 1
  • 9
  • 22