简体   繁体   中英

Docker Node image not recognizing node_modules folder when it's available in image

I have a Next.js application that I want to run using Docker compose

My docker.compose.yml :

version: "3.6"
services:
  ...

  next:
    container_name: next
    build:
      context: ./frontend/next
      dockerfile: Dockerfile.development
    restart: always
    ports:
      - 3000:3000
    volumes:
      - ./frontend/next:/frontend-app
      - ./frontend/next/package.json:/frontend-app/package.json
      - ./frontend/next/yarn.lock:/frontend-app/yarn.lock

and my ./frontend/next/Dockerfile.development is:

FROM mhart/alpine-node

RUN mkdir /frontend-app

WORKDIR /frontend-app

COPY package.json yarn.lock ./

RUN yarn install

COPY . .

EXPOSE 3000

CMD ["yarn", "dev"]

When I run docker-compose build then docker-compose up I get this error:

next    | yarn run v1.22.10
next    | $ next dev
next    | /bin/sh: next: not found
next    | error Command failed with exit code 127.
next    | info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

When running docker-compose build it's showing that yarn install is running correctly. Even if I run shell into my image and do ls /frontend-app I get that node_modules are present.

What's am I doing wrong here?

Edit: My ./frontend/next/.dockerignore is

.next/
node_modules/
Dockerfile

You're not sending your node_modules to docker, which means it can't find the next bin. Remove node_modules and.next from.dockerignore

So after Copy all file into container, I remove "package-lock.json".

Here is my Dockerfile

./project
Dockerfile
docker-compose.yml

Here is my docker-compose file

version: "3" services: web: build: . ports: - "3000:3000" volumes: -./project:/app command: > sh -c "npm i && npm run dev"

it works.

*note: "./project" is source folder

tree folder:
 ./project Dockerfile docker-compose.yml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM