1

I’m trying to build a docker container that installs react and its dependencies and then replicates those installations on my machine, so I don’t have to install the dependencies every time I want to create an application. But for some reason I’m not able to perform this "automatic" creation.. Can anyone help me, please?

PS: the react file structure is being created in the container, but not being replicated in my machine.

My Dockerfile:

FROM node:20.1.0 AS builder

WORKDIR /app COPY package*.json ./ RUN npm install

RUN npx create-react-app react-app

FROM node:20.1.0

WORKDIR /app/react-app COPY --from=builder /app/react-app . COPY . .

EXPOSE 3000

compose.yaml

version: "3.8"

services: app-dev: build: context: . dockerfile: Dockerfile.dev ports: - "3000:3000" volumes: - .:/app - /app/node_modules

1 Answers1

0

You can do this but you should simply your dockerfile a bit. I'd go with something like this:

    # Use an official Node runtime as a parent image
FROM node:20.1.0

Set the working directory

WORKDIR /app

Copy package.json and package-lock.json to install dependencies

COPY package*.json ./ RUN npm install

Create a React app

RUN npx create-react-app react-app

Change to the app directory

WORKDIR /app/react-app

Expose the port the app runs on

EXPOSE 3000

Command to run the app

CMD ["npm", "start"]

This will ensure that your React app's files in the container are mirrored to your local directory, and the node_modules directory is kept separate and avoids any conflicts.

ebilly
  • 1