【问题标题】:/bin/sh: sequelize: not found when I try to run express.js app with docker-compose/bin/sh: sequelize: 当我尝试使用 docker-compose 运行 express.js 应用程序时找不到
【发布时间】:2021-07-05 16:07:00
【问题描述】:

我在用户服务目录的 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

【问题讨论】:

  • 您的volumes: 将整个/app 目录隐藏在图像中,包括/app/node_modules。我建议从docker-compose.yml 中删除该块。 (然后由于您正在运行图像中的固定代码,您可以CMD yarn start 或类似的东西,不会监视不会更改的文件。)

标签: node.js docker express dockerfile sequelize.js


【解决方案1】:

问题在于,当您映射卷时,即使是 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_modules 时出现此错误。 ERROR: Named volume "node_modules:/app/node_modules:rw" is used in service "users-service" but no declaration was found in the volumes section. 但是,不应该映射到./users-service/node_modules 吗?事实上,正如您在问题中看到的那样,我试图确保 /app/node_modules 中是否存在 node_modules。它在那里,包括二进制文件..
  • 您需要在最后添加volumes 部分,正如我在代码中显示的那样。它需要与versionservices 具有相同的缩进级别
  • 成功了。你能解释一下这个- node_modules:/app/node_modules吗? app/node_modules 是如何映射到指定卷的?
  • docker 使用一个层卷,其中每一层都被组合在一起,而你的 fs 是文件系统的最终视图。因此,当您映射 ./app:/app 时,当前文件夹中的 app 文件夹将映射到容器内的 /app。但这会覆盖整个文件夹。由于我们不希望这种情况发生,我们将命名卷映射到/app/node_modules,这可以保护./app:/app 挂载以覆盖/app/node_modules
猜你喜欢
  • 1970-01-01
  • 2020-02-19
  • 1970-01-01
  • 1970-01-01
  • 2019-11-15
  • 2019-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多