With below dockerfile, I am trying to create docker image
FROM node:16.13.2
ARG DEFAULT_PORT 3013
ENV PORT ${DEFAULT_PORT}
# set working directory
WORKDIR /app
# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
# install app dependencies
COPY package.json ./
RUN npm install --silent --only=production
# add app
COPY . ./
# start app
CMD ["npm", "start"]
But I get error in Run npm install --silent --only=production command
[ ] Building 78.9s (8/9)
=> [internal] load build definition from DockerfileNoContext 0.0s
=> => transferring dockerfile: 393B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/node:16.13.2 1.9s
=> [1/5] FROM docker.io/library/node:16.13.2@sha256:2033f4cc18f9d8b5d0baa7f276aaeffd202e1a2c6fe9af408af05a34fe68cbfb 0.0s
=> [internal] load build context 10.0s
=> => transferring context: 270.75MB 9.5s
=> CACHED [2/5] WORKDIR /app 0.0s
=> CACHED [3/5] COPY package.json ./ 0.0s
=> ERROR [4/5] RUN npm install --silent --only=production 66.8s
------
> [4/5] RUN npm install --silent --only=production:
#6 66.72 npm notice
#6 66.72 npm notice New minor version of npm available! 8.1.2 -> 8.4.0
#6 66.72 npm notice Changelog: <https://github.com/npm/cli/releases/tag/v8.4.0>
#6 66.72 npm notice Run `npm install -g [email protected]` to update!
#6 66.72 npm notice
------
executor failed running [/bin/sh -c npm install --silent --only=production]: exit code: 1
As per error message, I tried to run npm install -g [email protected] also but still failing with same error.
Can anyone help on what's wrong I am doing here ?
- thanks !
CodePudding user response:
You can try to delete package-lock.json and node_modules and also do npm cache clean --force and try to build the docker image again.
and also please make sure to copy the package-lock.json as well in dockerfile
FROM node:16.13.2
COPY package.json ./
RUN rm -f package-lock.json
RUN rm -rf node_modules
RUN npm cache clean --force && npm install
COPY package-lock.json ./
RUN npm install --silent --only=production
RUN mkdir /react-ui && mv ./node_modules ./react-ui
WORKDIR /react-ui
# add app
COPY . ./
# start app
CMD ["npm", "start"]
CodePudding user response:
You can try with the below format. There are many ways to dockerize the React app. Even though we can use any server to serve the files, for ease now I am using serve.js
# Get the base image
FROM node:14.16.0 as build-stage
# Set working directory
WORKDIR /usr/src/app
# Add `/app/node_modules/.bin` to $PATH
ENV PATH /usr/src/app/node_modules/.bin:$PATH
# Copy project files to /usr/src/app/
COPY . /usr/src/app/
# Get the production files ready
RUN npm run build
# Get the base image
FROM node:14.16.0
# Get the production files to the location /usr/src/app/build
COPY --from=build-stage /usr/src/app/build/ /usr/src/app/build
# Server for the application
RUN npm install -g serve
# Start app
CMD serve -s /usr/src/app/build -l 3013
