Home > Software engineering >  React app docker container exits/restarts after executing npm run build command
React app docker container exits/restarts after executing npm run build command

Time:11-30

Dockerfile:

FROM node:14

WORKDIR /code 

COPY ./code/package.json ./code/package-lock.json /code/

RUN npm install 

COPY ./code /code

CMD [ "npm", "run", "build"]

docker-compose.yml:

version: '3'

services:
  medace-lecture-capsules:
    build: ./app
    container_name: medace-lecture-capsules
    restart: always
    expose:
      - "3000"
    networks:
      - shared

networks:
  shared:
    external:
      name: shared

docker-compose logs:

Attaching to medace-lecture-capsules
medace-lecture-capsules    | 
medace-lecture-capsules    | > medace-frontend-lecture-capsules@0.1.0 build /code
medace-lecture-capsules    | > react-scripts build
medace-lecture-capsules    | 
medace-lecture-capsules    | Creating an optimized production build...
medace-lecture-capsules    | Compiled successfully.
medace-lecture-capsules    |   
medace-lecture-capsules    | File sizes after gzip:
medace-lecture-capsules    | 
medace-lecture-capsules    |   130.34 KB  build/static/js/2.dd4b4a7b.chunk.js
medace-lecture-capsules    |   3.83 KB    build/static/js/main.725c12bb.chunk.js
medace-lecture-capsules    |   970 B      build/static/css/main.aae65096.chunk.css
medace-lecture-capsules    |   791 B      build/static/js/runtime-main.15e7259b.js
medace-lecture-capsules    | 
medace-lecture-capsules    | The project was built assuming it is hosted at /.
medace-lecture-capsules    | You can control this with the homepage field in your package.json.
medace-lecture-capsules    | 
medace-lecture-capsules    | The build folder is ready to be deployed.
medace-lecture-capsules    | You may serve it with a static server:
medace-lecture-capsules    | 
medace-lecture-capsules    |   npm install -g serve
medace-lecture-capsules    |   serve -s build
medace-lecture-capsules    | 
medace-lecture-capsules    | Find out more about deployment here:
medace-lecture-capsules    | 
medace-lecture-capsules    |   https://cra.link/deployment
medace-lecture-capsules    |  

I've used docker-compose build followed by docker-compose up -d, but the container somehow manages to exit after creating build. I want to run a production server, that's why I'm using npm run build. Tried serve -s build but ends up getting a 404 error when loading the webpage.

CodePudding user response:

Your command seems to be your compile command.

The CMD command of the container need to be something that will run forever like

CMD node app.js

Or maybe CMD npm start

Also.. something unrelated to your problem, if you use npm install I think your lock file will be ignored, and regenerated. To install exactly what is in your lock file use

RUN npm ci

sorry - was easier to tell you why it was not working on my phone than how to make it work

FROM node:14
WORKDIR /code 
COPY ./code/package.json ./code/package-lock.json /code/
RUN npm ci # install what is in lock file no package file 
COPY ./code /code
RUN npm run build
RUN npm install -g serve
CMD serve -s build 3000

and then if you want to try serve locally, you can add npm install serve in your project and save the package and package lock files, and then you don't have to install serve globally in your container so it becomes:

FROM node:14
WORKDIR /code 
COPY ./code/package.json ./code/package-lock.json /code/
RUN npm ci # install what is in lock file no package file 
COPY ./code /code
RUN npm run build
# the port for the following should match the one you are exposing with docker compose:
CMD ./node_modules/.bin/serve -s build 3000

once you have added the dependency serve to your project

  • Related