0

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.

  • I suspect it could be due to your node/npm version that your docker image is using. Try experimenting with different versions? Maybe upgrade them? carbon is a bit old. – Samson Mar 15 '22 at 10:23
  • Any suggestions which I should use ?? when i use npm start command it works, only problem is when i run serve -s command. – sai chandan Mar 15 '22 at 10:39
  • @Samson I'm currently copying the whole source, what is the command I should use to only copy my build folder ?? – sai chandan Mar 15 '22 at 11:32
  • npm start works but serve doesnt work because of the source code in the serve module, as you can see from the error message. Check which node version you are using locally, then create a docker image with the same version. – Samson Mar 15 '22 at 11:37
  • to copy only your build folder, do `COPY build build` – Samson Mar 15 '22 at 11:38
  • @Samson I was able to fix the serve -s error it was because of the node version, it is working now, my last step is copying only the build folder, my current code is ```COPY .. ```, i tried with your command COPY build build, it seems to work. – sai chandan Mar 15 '22 at 11:52
  • Perfect, glad to have helped :) I'll post it as an answer then, please do accept it as the answer for visibility so it may help other people! – Samson Mar 15 '22 at 16:58

2 Answers2

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.

Samson
  • 1,336
  • 2
  • 13
  • 28
0

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

Kristian
  • 2,071
  • 23
  • 15