node.js -/bin/sh : sequelize: not found when I try to run express. js 应用程序与 docker-compose

标签 node.js docker express dockerfile sequelize.js

我在 users-service 目录的 Dockerfile 中有这个。

FROM node:15-alpine

COPY . /app

WORKDIR /app

RUN echo ">>>> $PWD"

RUN ls -lart

RUN yarn install

RUN ls -lart /app/node_modules/.bin

CMD yarn watch
当前目录是预期的 /app。 Sequelize 二进制文件位于 node_modules/.bin 文件夹中。 /app/node_modules/.bin 命令的结果是...
total 16
lrwxrwxrwx    1 root     root            21 Apr  9 18:35 uuid -> ../uuid/dist/bin/uuid
lrwxrwxrwx    1 root     root            30 Apr  9 18:35 sequelize-cli -> ../sequelize-cli/lib/sequelize
lrwxrwxrwx    1 root     root            30 Apr  9 18:35 sequelize -> ../sequelize-cli/lib/sequelize
这是 docker-compose.yml :
....

services:
  users-service:
    build: './users-service'
    depends_on:
      - users-service-db
    environment:
      - DB_URI=mysql://root:**********@users-service-db/users_db?charset=UTF8
    ports:
      - 7100:7100
    volumes:
      - ./users-service:/app
    networks:
      - autostop

  users-service-db:
    command: --default-authentication-plugin=mysql_native_password
    environment:
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_DATABASE=users_db
    restart: always  
    image: mysql:5.7.20
    ports:
      - 0.0.0.0:3306:3306
    networks:
      - autostop

...
当我运行 docker-compose builddocker-compose up --build 它显示
$ yarn db:migrate && babel-watch -L src/index.js
users-service_1     | $ sequelize db:migrate
users-service_1     | /bin/sh: sequelize: not found
enter image description here

最佳答案

问题是,当您映射卷时,甚至 node_modules 也会被主机文件夹覆盖。如果主机源代码中没有 node_modules,它将无法工作。所以你想要的是屏蔽容器的 node_modules 文件夹,这样它就不会受到主机文件夹映射的影响

....

services:
  users-service:
    build: './users-service'
    depends_on:
      - users-service-db
    environment:
      - DB_URI=mysql://root:**********@users-service-db/users_db?charset=UTF8
    ports:
      - 7100:7100
    volumes:
      - ./users-service:/app
      - node_modules:/app/node_modules
    networks:
      - autostop

  users-service-db:
    command: --default-authentication-plugin=mysql_native_password
    environment:
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_DATABASE=users_db
    restart: always  
    image: mysql:5.7.20
    ports:
      - 0.0.0.0:3306:3306
    networks:
      - autostop
volumes:
  node_modules:
...
通过将其映射到命名卷,将确保内部文件夹不受外部主机映射文件夹的影响

关于node.js -/bin/sh : sequelize: not found when I try to run express. js 应用程序与 docker-compose,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67026826/

相关文章:

docker - 如何在 Docker 上发布 UDP 端口?

javascript - 单击复选框时无法发出事件。 (socket.io)

node.js - 2个node.js微服务之间的身份验证策略

linux - 如何在 CygWin 上使用连接代理

node.js - Node JS 中的最大可写流

windows - 适用于Windows Hyper V默认交换机IP的Docker

javascript - 我得到 Promise { <pending> } 作为返回值,并且在异步作用域中调用也会立即给我未定义的结果

docker - 在 Emacs 中使用 org-mode 的 Docker 编程环境的文学编程设置

javascript - 如何使用express-validator对嵌套对象实现验证

node.js - 如何在 expressjs 中将 session 数据存储在文件中?