Ben Walker
Ben Walker

Reputation: 2137

Dockerfile copy from build failing for create-react-app

I have a react app I'm trying to dockerize for production. It was based off create-react-app. To run the app locally, I am in the app's root folder and I run npm start. This works. I built the app with npm run build. Then I try to create the docker image with docker build . -t app-name. This is failing for not being able to find the folder I'm trying to copy the built app from (I think).

Here's what's in my Dockerfile:

FROM node:13.12.0-alpine as build
WORKDIR /src
ENV PATH /node_modules/.bin:$PATH
COPY package.json ./
COPY package-lock.json ./
RUN npm install --silent
COPY . ./

RUN npm run build

FROM nginx:alpine
COPY --from=build build /usr/share/nginx/html
EXPOSE 80

CMD ["npm", "start"]

I'm pretty sure I've got something wrong on the COPY --from line.

The app structure is like this, if it matters

-app-name (folder)
  -src (folder)
  -build (folder)
  -dockerfile
  -other stuff, but I think I listed what matters

The error I get is failed to compute cache key: "/build" not found: not found

I'm running my commands in windows powershell.

What do I need to change?

Upvotes: 0

Views: 4538

Answers (1)

Saikat Chakrabortty
Saikat Chakrabortty

Reputation: 2680

You were almost correct, Just that the path where the build folder is generated is at /src/build and not at /build. and hence the error you see, and why the /src coming? it's due to the WORKDIR /src.

and hence this should work: COPY --from=build /src/build /usr/share/nginx/html

besides, since you are using nginx server to serve the build static files, you don't need to or you cant run npm start with CMD.

instead, just leave it, and you can access the application at port 80.

so the possible working Dockerfile would be:

FROM node:13.12.0-alpine as build
WORKDIR /src
ENV PATH /node_modules/.bin:$PATH
COPY package*.json ./

RUN npm install --silent
COPY . ./

RUN npm run build

FROM nginx:alpine
COPY --from=build /src/build /usr/share/nginx/html
EXPOSE 80

This is in accordance with the Dockerfile in the above question, in some specific cases, advanced configuration might be required.

Upvotes: 1

Related Questions