admin管理员组

文章数量:1122847

I am attempting to use docker for the first time and I have figured out how to make the sql and the php containers work. But, I continue to get the same exception with my nodejs.

My code follows the following format: Customer-side written using nodejs and Vendor-side written using php

node-1    | node:internal/modules/cjs/loader:1143
node-1    |   throw err;
node-1    |   ^
node-1    |
node-1    | Error: Cannot find module '/app/customer/index.js'
node-1    |     at Module._resolveFilename (node:internal/modules/cjs/loader:1140:15)
node-1    |     at Module._load (node:internal/modules/cjs/loader:981:27)
node-1    |     at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:128:12)
node-1    |     at node:internal/main/run_main_module:28:49 {
node-1    |   code: 'MODULE_NOT_FOUND',
node-1    |   requireStack: []
node-1    | }
node-1    |
node-1    | Node.js v18.20.5

My code makes use of the following docker-compose

services:

  node:
    build:
      context: .
      dockerfile: Dockerfile.nodejs
    volumes:
      - ./customer:/app
      - /customer/node_modules
      - shared-package:/usr/src/app/shared
    depends_on:
      - mysql
    ports:
      - "3000:3000"
  apache:
    build:
      context: .
      dockerfile: Dockerfile.apache
    volumes:
      - ./vendor:/var/www/html
      - shared-package:/var/www/html/shared
    depends_on:
      - mysql
    ports:
      - "8080:80"

  mysql:
    image: mysql
    environment:
        MYSQL_ROOT_PASSWORD: root
    volumes:
      - ./mysql-data:/var/lib/mysql
    ports:
      - "3306:3306"

volumes:
  shared-package:
    driver: local

And this is my dockerfile.nodejs

FROM node:18-alpine

WORKDIR /app



COPY ./customer .
RUN npm install

WORKDIR /app/customer
EXPOSE 3000

CMD ["node", "index.js"]

This is a photo of my file structure

I am attempting to use docker for the first time and I have figured out how to make the sql and the php containers work. But, I continue to get the same exception with my nodejs.

My code follows the following format: Customer-side written using nodejs and Vendor-side written using php

node-1    | node:internal/modules/cjs/loader:1143
node-1    |   throw err;
node-1    |   ^
node-1    |
node-1    | Error: Cannot find module '/app/customer/index.js'
node-1    |     at Module._resolveFilename (node:internal/modules/cjs/loader:1140:15)
node-1    |     at Module._load (node:internal/modules/cjs/loader:981:27)
node-1    |     at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:128:12)
node-1    |     at node:internal/main/run_main_module:28:49 {
node-1    |   code: 'MODULE_NOT_FOUND',
node-1    |   requireStack: []
node-1    | }
node-1    |
node-1    | Node.js v18.20.5

My code makes use of the following docker-compose

services:

  node:
    build:
      context: .
      dockerfile: Dockerfile.nodejs
    volumes:
      - ./customer:/app
      - /customer/node_modules
      - shared-package:/usr/src/app/shared
    depends_on:
      - mysql
    ports:
      - "3000:3000"
  apache:
    build:
      context: .
      dockerfile: Dockerfile.apache
    volumes:
      - ./vendor:/var/www/html
      - shared-package:/var/www/html/shared
    depends_on:
      - mysql
    ports:
      - "8080:80"

  mysql:
    image: mysql
    environment:
        MYSQL_ROOT_PASSWORD: root
    volumes:
      - ./mysql-data:/var/lib/mysql
    ports:
      - "3306:3306"

volumes:
  shared-package:
    driver: local

And this is my dockerfile.nodejs

FROM node:18-alpine

WORKDIR /app



COPY ./customer .
RUN npm install

WORKDIR /app/customer
EXPOSE 3000

CMD ["node", "index.js"]

This is a photo of my file structure

Share Improve this question asked Nov 24, 2024 at 2:09 Rithik TankRithik Tank 394 bronze badges 1
  • 1 I think the problem might be with the COPY command, as you seem to be copying the customer directory to the WORKDIR, which is /app. In the end your container will look like ``` /app ├── index.js ├── package.json ├── node_modules/ ├── etc etc ``` try changing it to COPY ./customer /app/customer. Also, please ignore the terrible formatting, I'm being rate-limited but I'll put it in a comment later to make it clearer ^~^ – ION606 Commented Nov 24, 2024 at 3:20
Add a comment  | 

1 Answer 1

Reset to default 1

In the Dockerfile, the final WORKDIR line is wrong and you should delete it.

WORKDIR /app
COPY ./customer .
# WORKDIR /app/customer  # delete this one

Since the left-hand side of COPY is a directory, its contents are copied into the destination directory (which is the current container working directory, which is /app). The image filesystem has an /app/index.js file but not a /app/customer subdirectory.

You can verify this by running docker-compose run node ls /app to launch a temporary container to see what's in the container filesystem.

I'd also recommend deleting almost all of the volumes: blocks in the Compose file, except for the mysql container. Your code is already built into your image and you don't need to separately inject it; it can lead to reproducibility problems if you have code on your local system that isn't built into the image for whatever reason. The mix of container paths on the right-hand side of volumes: also doesn't look right to me and that can lead to further unexpected problems.

本文标签: