Search code examples
node.jsreactjsdockercontainers

404 when serving react application in docker container


I've create a react application with create-react-app and have build a docker image with the following docker file.

FROM node:alpine AS builder
WORKDIR /app
RUN npm install
COPY . .
RUN npm run build

FROM node:alpine
WORKDIR /app
COPY --from=builder /app/build .
RUN npm install -g serve
EXPOSE 80
CMD serve -p 80 -s build

When running the container and accessing port 80 on localhost I'm met with "404 the requested path could not be found". The container is run with the command `docker run -p 80:80 "image name" and the output is "Accepting connections at http://localhost:80" What could be the reasons for the 404 and what can i do to fix it?


Solution

  • Looking at the documentation of serve... You are copying /app/build from the builder container in to /app on the new container and then calling serve with a folder name of build, which does not exist. (-s doesn't take a parameter`)