Problem if the node_modules wasn't already created in the project directory while using docker-compose and volumes

44 Views Asked by At

I am struggling with a problem where the node modules' folder should be created before running the docker otherwise, I am getting vite not found.

docker-compose.yml:

version: "3.4"

services:
  node:
    build:
      context: ..
      dockerfile: ./docker/node/Dockerfile
    container_name: node
    ports:
      - "8000:8000"
    volumes:
      - node_modules:/srv/app/node_modules
volumes:
  node_modules:
    driver: local
    driver_opts:
      type: "none"
      o: "bind"
      device: "../node_modules"

My node Dockerfile:

FROM node:alpine

WORKDIR /srv/app

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./

RUN npm install

# Copy the rest of the application files
COPY . .


EXPOSE 8000

CMD ["npm", "run", "dev"]

My project structure is:

|-- docker/
|   |-- node/
|       |-- Dockerfile
|   |-- docker-compose.yml
|-- package.json
|-- package-lock.json
|-- other vue js project structure

The error I am getting is

ERROR: for node  Cannot create container for service node: failed to mount local volume: mount /home/mohammad/Desktop/Vuejs/vite-project/node_modules:/var/lib/docker/volumes/docker_node_modules/_data, flags: 0x1000: no such file or directory
ERROR: Encountered errors while bringing up the project.

However, If I created the folder node_modules by hand in the host, It's working perfectly. Could someone please tell me what is the problem?

1

There are 1 best solutions below

0
Mohammad Mustafa On

I fixed the problem by changing the docker-compose.yml file volume and redirect it to the whole project.

The updated file is :

version: "3.4"

services:
  node:
    build:
      context: ..
      dockerfile: ./docker/node/Dockerfile
    container_name: node
    ports:
      - "8000:8000"
    volumes:
      - app:/srv/app
volumes:
  app:
    driver: local
    driver_opts:
      type: "none"
      o: "bind"
      device: ".."