I tried recreating your setup as much as I could from you question and it works. The problem is probably with either volume or how you run nodemon.
You can try debugging the problem like so:
- Remove
CMD [ "npm", "start" ]
from Dockerfile
- Add
tty: true
to youtube-clone-app
service in Docker Compose (like in my example below)
- Run the container with Docker Compose
docker-compose up --build --force-recreate
(options --build --force-recreate
rebuild the image).
- Run
bash
in container with docker-compose exec youtube-clone-app bash
- In bash, list files with
ls
to see if all necessary files are there.
- Print the contents of a file you are trying to update with
cat <file>
, update the file on your host, and print it again to see if the contents are updated.
- Run nodemon manually in bash with
nodemon index.js
(or some other file that is your entrypoint)
If there is a missing file in step 5 or contents of a file are not updated in step 6, it is probably an issue with volume mount.
In step 7 you can test which command works - it might be that CMD [ "npm", "start" ]
is the problem.
Here is my test setup that works:
// index.js
console.log("Hello")
# docker-compose.yaml
version: "3.8"
services:
youtube-clone-app:
build: ./
container_name: server
volumes:
- ./:/usr/src/youtube-clone-app
tty: true # Keep container running even if there is no CMD/ENTRYPOINT
# Dockerfile
FROM node:12.18.4
WORKDIR /usr/src/youtube-clone-app
COPY ./ ./
RUN npm install
ENTRYPOINT ["npx", "nodemon", "index.js"] # If nodemon is installed globally, remove the "npx" argument
If you run this test setup with docker-compose up --build --force-recreate
you should see Hello
printed in the terminal. When you change index.js
and save it, it should print whatever you changed it to.
Also, prefer ENTRYPOINT
to CMD
. For more info check out the What is the difference between CMD and ENTRYPOINT in a Dockerfile? question on Stack Overflow.