Setting up the docker-compose volumes

510 views Asked by At

I don't know what's wrong with my docker-compose, I'm trying to implement volume to constantly updating the app inside the container while working on it on the host with the nodemon, but it doesn't work. I have been fighting with it almost the whole day, but I can't see what is wrong.

FROM node:12.18.4
WORKDIR /usr/src/youtube-clone-app
COPY ./ ./
RUN npm install
CMD [ "npm", "start" ]
version: "3.8"
services:
    youtube-clone-app:
        build: ./
        container_name: server
        ports:
            - "5000:5000"
        volumes:
            - ./:/usr/src/youtube-clone-app
2

There are 2 answers

0
metad00r On

It's a bit difficult to understand exactly what you need, but try this:

FROM node:12.18.4
WORKDIR /usr/src/youtube-clone-app
RUN npm install
COPY ./ ./
CMD ["nodemon", "start"]

Note CMD ["nodemon", "start"] instead of CMD ["npm", "start"]. If necessary, try to replace start with your node app script, e.g. ./server.js. Make sure nodemon is in your package.json. Additionally, add a context to your docker-compose build option.

4
touchmarine On

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:

  1. Remove CMD [ "npm", "start" ] from Dockerfile
  2. Add tty: true to youtube-clone-app service in Docker Compose (like in my example below)
  3. Run the container with Docker Compose docker-compose up --build --force-recreate (options --build --force-recreate rebuild the image).
  4. Run bash in container with docker-compose exec youtube-clone-app bash
  5. In bash, list files with ls to see if all necessary files are there.
  6. 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.
  7. 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.