'Unable to run serve -s build inside docker image for my react application

I'm trying to run my react application using the build folder via docker file.

Everything seems to run properly when tried without docker, but when i run using the docker file build folder gets created but serve -s build command is not working inside the docker image. Below is my dockerfile.

FROM node:carbon

# Create app directory
WORKDIR /usr/src/docker-react-sample

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./
COPY package-lock.json ./
RUN npm install
#To bundle your app’s source code inside the Docker image, use the COPY instruction:
COPY . .
# Build for production.
RUN npm run build

# Install `serve` to run the application.
RUN npm install -g serve

# Uses port which is used by the actual application
EXPOSE 3000

# Run application
#CMD [ "npm", "start" ]
CMD serve -s build

Below is the error i get when i run it with docker

/usr/local/lib/node_modules/serve/node_modules/camelcase/index.js:3
const UPPERCASE = /[\p{Lu}]/u;
                  ^

SyntaxError: Invalid regular expression: /[\p{Lu}]/: Invalid escape
    at Object.<anonymous> (/usr/local/lib/node_modules/serve/node_modules/camelcase/index.js:3:19)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/usr/local/lib/node_modules/serve/node_modules/boxen/index.js:6:19)
    at Module._compile (module.js:653:30)

But instead of "serve -s build" if i use CMD [ "npm", "start" ] it work properly.

Any help provided would be helpful.



Solution 1:[1]

You are only getting the error on serve because the issue comes with incompatibility with the serve package, most likely due to the node version, carbon is quite old and no longer being actively maintained. Check which node version you are using on your local machine and use that version for your docker base.

You may also do COPY build build instead of COPY . . to only copy the build folder, which is the only folder you need. This will allow the image to build faster.

Solution 2:[2]

I got this on node version 8(Windows), upgraded to 16+.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Samson
Solution 2 Kristian