Issue
I have this in the Dockerfile in the users-service directory.
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
Current directory is /app
as expected. Sequelize binary is there in the node_modules/.bin
folder. Result of /app/node_modules/.bin
command is…
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
This is the 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
...
When I run docker-compose build
or docker-compose up --build
it shows
$ yarn db:migrate && babel-watch -L src/index.js
users-service_1 | $ sequelize db:migrate
users-service_1 | /bin/sh: sequelize: not found
Solution
The problem is that when you map the volume, even the node_modules
get overridden by the host folder. And if you don’t have node_modules
in the host source code it won’t work. So what you want is to mask the container’s node_modules
folder, so it doesn’t get impacted by host folder map
....
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:
...
By mapping it to a named volume, will make sure that the internal folder is not impacted by your outer host mapped folder