我在 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 build
或 docker-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
最佳答案
问题是,当您映射卷时,甚至 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/