Docker run react-app image, but process exited early. Why? - reactjs

FROM node
WORKDIR /app
COPY . /app
RUN npm install
CMD ["npm", "start"]
I used this dockerfile to build the image. Upon proceeding with docker run -p 3000:80 image_name, it returns this
docker run -p 3000:80 lifestyle-app
> lifestyle-report#0.1.0 start
> react-scripts start
(node:28) [DEP_WEBPACK_DEV_SERVER_ON_AFTER_SETUP_MIDDLEWARE] DeprecationWarning: 'onAfterSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.
(Use `node --trace-deprecation ...` to show where the warning was created)
(node:28) [DEP_WEBPACK_DEV_SERVER_ON_BEFORE_SETUP_MIDDLEWARE] DeprecationWarning: 'onBeforeSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.
Starting the development server...
The build failed because the process exited too early. This probably means the system ran out of memory or someone called `kill -9` on the process.
I understood it had something to do with memory, but my question is which memory, because, if I ran npm start without docker, the react app is running just fine. Please help, thanks 🙏

This has nothing to do with memory. Because you didn't specify -d in your command line to enable detached mode, Docker exited immediately after the the main npm process exited (npm start will create a subprocess to run your JS script). When Docker exits, it kills all the other processes in the container, including the subprocess running your script, thus the kill -9 part in the error message. You should almost always run Docker apps with -d option, unless you only wish to execute a one-off command.
Just execute docker run -d -p 3000:80 lifestyle-app.

Related

Exited 126 on creating docker container

When I try and create the docker container, it immediately exits with error 126 and in the logs I get a message saying "/usr/local/bin/docker-entrypoint.sh: exec: line 11: .: Permission denied". Attached is my dockerfile code:
`
FROM node:18-alpine
WORKDIR /app
COPY . .
RUN npm ci
RUN npm run build
ENV NODE_ENV production
CMD ["node", "src/index.js"]
EXPOSE 3000
CMD ["npx", "serve", "build"]
This container is for a react application, and I do not have a docker-entrypoint file. Thanks!
I have also tried using commands with chmod I found but it did not solve.

How to run `httpd` in Docker in detached mode using CMD in Dockerfile?

This is my Dockerfile -
The image builds successfully but it doesn't run, it stops. I want to access the website being served from the Apache server from Docker container.
# build environment
FROM node:13.12.0-alpine as build
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package.json ./
COPY package-lock.json ./
RUN npm ci --silent
RUN npm install react-scripts#3.4.1 -g --silent
COPY . ./
RUN npm run build:staging
# production environment
FROM httpd:latest
COPY --from=build /app/build /usr/local/apache2/htdocs
EXPOSE 80
CMD ["httpd"]
You should delete CMD ["httpd"], see this:
CMD ["httpd-foreground"]
There is already a foreground httpd there.
Finally, Why CMD ["httpd"] won't work?
The CMD defined in Dockerfile would be acting as PID1 of your container. In docker, if PID1 exits, then, the container will also exit.
If use CMD ["httpd-foreground"], the apache process will always be in front, so the process will not exit, then the container is alive.
If use CMD ["httpd"], the httpd will directly exit after executing, then PID1 exits, so the container exits.

can't run react app with docker container

I have a react-app, which simple showing hello-world message but I like to run the app throug docker-container but having this problem. After this message, process stopped without running app..
ℹ 「wds」: Project is running at http://172.17.0.2/
ℹ 「wds」: webpack output is served from
ℹ 「wds」: Content not from webpack is served from /app/public
ℹ 「wds」: 404s will fallback to /
Starting the development server...
Can't understand what I should do because I have very small app with basic code in Dockerfile
FROM node:alpine
RUN mkdir /app
COPY . /app
WORKDIR /app
COPY package.json ./
RUN npm install
CMD ["npm", "start"]
Do I need to install webpack-dev-server, I tried but got version error like 'manually added server' has lower version than already server. so I re-install the webpack-dev-server.
I have created app with 'create-react-app', so I think every dependency is managed automatically..
Is anyone have idea, how can I solve the problem.. thanks in advance (BTW..)
Command which I use to build: docker build . -t lucki
Command to run image: docker run -p 3000:3000 lucki
this is project stracture:
after adding DEBUG=* in Dockerfile, I have response as:
The problem is that the dev mode will not run if it is not an interactive terminal.
Change your docker command to include an interactive terminal:
Add -it to your docker run command (-i interactive, -t pseudo-TTY) e.g. docker run -it -p 3000:3000 your_container
Canonical troubleshooting
Make sure the code runs without docker
Does npm start work on the command line?
Showing debug info
Add DEBUG=* as an environment variable inside your container.DEBUG is an environment variable which controls logging for many Node modules.
In your Dockerfile, add
ENV DEBUG=*
Or on the command line, add -e 'DEBUG=*' to your docker command.
This may help spot error messages which are somehow getting swallowed
Run node directly
Instead of running npm start, run your file directly.
e.g. in your Dockerfile,
CMD ["node", "index.js"]
Try running another docker container
If this is a problem with your docker setup, running a known good container may help you discover it.
docker run --rm -it node:alpine
Improvements
Your Dockerfile could also be simplified a bit.
FROM node:alpine
WORKDIR /app
COPY package*.json ./
RUN npm install --production
COPY . .
CMD ["npm", "start"]
mkdir is not needed, as WORKDIR automatically creates the directory.
package*.json will also copy package-lock.json
--production will skip installing devDependencies
Putting the COPY command last will leverage cache better (you won't have to re-run npm install unless your dependencies have changed)
You might also want to use Tini. Tini forwards signals, which means docker stop and pressing control+c in an interactive terminal will actually stop the node process immediately.
If you are using Docker 1.13+, add --init to the command line to have signals forwarded and processes reaped. On older versions, follow the instructions in the README
Same Problem I have Faced and it's fixed By using the following Command
Cause of Problem:- Due to react project setup it requires an input trigger to start the server if not it will automatically stop
FIX:- add -it with the docker run command
Example:- docker run --name main-app -it -p 3000:3000 main-image-react
I got the same issue
it solved when I use like
docker run -it -p 3000:80 <image name>
using -it other than -p and -d solved the issue.

npm install npm run build --prod / build is failed jenkins

I'm building a project that uses react docker and Jenkins. last week the project is working fine and the build is success. unfortunately this week I got this error from Jenkins:
The build failed because the process exited too early. This probably means the system ran out of memory or someone called kill -9 on the process.
npm ERR! code ELIFECYCLE.
actually, I'm new to react docker and Jenkins, so any suggestions would help. Thank you

Docker container exiting immediately after starting when using npm init react-app

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
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
docker run -it -p 80:3000 imagename resolve my problem.
Running the command with -it worked for me:
docker run -it -p 3001:3000 Imageid
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
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
Ran into the same issue. -it flag resolved the issue.
docker run -it -p 3001:3001 <image-id>
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
You can use the below with -itd flag as well.
sudo docker run -itd -p 3001:3000 Image-Name
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.

Resources