Exited with code 0 (unexpected backend/node_modules)

Hi,
I am starting to learn docker and want to learn docker by dockerizing my existing app (for practice). But I am running into this error Operation timed out exited with code 0 on the api in the docker-compose file.
It should run all the code in the backend/ dir but for some reason, it is creating a sub-director backend/backend/node_modules where node_modules and the nested backend are empty. The app running cmd in the main backend dir.
Am I doing something wrong here?

# Stage 1: Install Dependencies (Cacheable)
FROM node:18-alpine AS builder
WORKDIR /app
RUN apk add --no-cache curl
COPY package*.json ./
RUN npm install --legacy-peer-deps

# Stage 2: Copy Application Code (Production)
FROM node:18-alpine
WORKDIR /backend
COPY --from=builder /app/node_modules ./node_modules
COPY package*.json .
COPY start.sh .
COPY wait-for.sh .
COPY . .

EXPOSE 3001
CMD [ "npm", "run", "start:dev" ]
version: "3.9"
services:
  redis:
    container_name: tgc_sc_redis
    image: redis:7-alpine
    healthcheck:
      test: ["CMD", "redis-cli","ping"]
      interval: 5s
      retries: 3  
    ports:
      - "6379:6379"
    networks:
      - tgc_sc_network
  rabbitmq:
    container_name: tgc_sc_rabbitmq
    image: rabbitmq:3-management-alpine
    ports:
        - "5673:5673"
        - "15673:15673"
    volumes:
      - ~/.docker-conf/rabbitmq/data/:/var/lib/rabbitmq/
      - ~/.docker-conf/rabbitmq/log/:/var/log/rabbitmq/
    networks:
      - tgc_sc_network
  api:
    restart: on-failure
    build: 
      context: ./backend
      dockerfile: Dockerfile
    container_name: tgc_sc_api
    volumes:
      - ./backend:/backend
      - ./backend/node_modules:/backend/node_modules/
    ports:
      - "3001:3001"
    networks:
      - tgc_sc_network
    depends_on:
      - redis
      - rabbitmq
    entrypoint: ["/backend/wait-for.sh", "rabbitmq:5673", "--" , "/backend/start.sh"]
    command: [ "npm", "run", "start:dev" ]
volumes:
  node_modules:
  backend:
networks:
  tgc_sc_network:
    name: tgc_sc_network
    driver: bridge

This probably creates the second node_modules folder.

Why do you have a builder image if you don’t build anything (like with Svelte and devDependencies), but copy the whole node_modules anyway?

Why do you declare node_modules as bind mount with volumes in compose?

I’m still learning Docker. I am not sure if I understand your question. But let me explain my thought process of dockerizing the app.

My initial approach was to bind mount the node_modules. Then after doing research, I realized it’s not recommended to do so. So, I used the builder approach to cache the node_modules in the docker.

I managed to make the app run, it was giving the error because of the rabbitMQ extension, it was running on the same port removing it resolved the issue. But the nested folder issue is still there I tried removing both volumes and removed all the unused volumes.

Kind of every line of a Dockerfile is cached. You only need to make sure only last lines change, place npm install early, then usually a cached version is used und building is faster.