I want to run a typescript monorepo with a nodejs backend and a create-react-app frontend. First i am trying to get the frontend running in a container. The application runs locally fine.
I want to change code in the host machine and have the code in the container update automatically.
When I run docker-compose up -d I see the error below.
> [email protected] start
> react-scripts start
Could not find a required file.
Name: index.js
Searched in: /app/src
folder structure
repo
- backend
- frontend
- Dockerfile
- build
- public
- src
- index.tsx
- docker-compose.yaml
DockerFile
FROM node
WORKDIR /app
COPY package.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
docker-compose.yaml
version: "3.8"
services:
frontend:
builds: ./frontend
ports:
- '3000:3000'
volumes:
- './frontend/src:/app/src'
stdin_open: true
tty: true
volumes:
data:
logs:
version: "3.8"
services:
frontend:
builds: ./frontend
ports:
- '3000:3000'
volumes:
- './frontend:/app'
- /app/node_modules
stdin_open: true
tty: true
error after running docker-compose up -d the container logs show
ENOENT: no such file or directory, open 'app/package.json'
note: Strangely, removing the workdir in the Dockerfile runs the container but installs everything in the top level and not in the /app directory. When I remove volumes entirely the container runs fine as well (still a deal breaker).
frontend directory the container serves the react app and local code changes auto update in the container.Assuming your tsconfig.json file is in frontend, it won't be present in your container so you'll be missing crucial parts like
"compilerOptions": {
"jsx": "react-jsx"
}
A more typical approach is to volume mount the entire application directory and create an anonymous volume mount so as to not clobber node_modules
volumes:
- ./frontend:/app
- /app/node_modules # anonymous volume mount
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With