Watching a folder with Chokidar outside of application with docker

734 Views Asked by At

I have a application that watches for changes made in a folder, but I want to be able to change which folder is being watched in the users local system. If possible, a file path would be able to be pasted in the docker-compose file, and the application would watch the folder that the user chose.

As of now, the user would have to go into the path of the volume in their local machine to make changes to the "watch-folder" instead of choosing their own watch folder

Currently the code is -

const watcher = chokidar.watch('watch-folder', {
    persistent:true,
})

I'd like to possibly add something along these lines for the user to be able to add which folder they specified

watcher.add('FOLDER-FROM-DOCKER-COMPOSE');
1

There are 1 best solutions below

0
CodeNoob On

This can be done with bindmounts in docker. I had the application point to a specific path in the container, and the bindmount mounts a path from the host machine to that container.

Here is what my docker-compose file looks like, you can replace {PATH} with a path in your local machine and replace {CONTAINERPATH} with the path in your container.

version: "3"
services:
  api-server:
    build: ./ExpressBackend
    volumes:
      - "{PATH}:{CONTAINERPATH}"
    image: express
    ports:
      - "5000:5000"
    networks:
      - react-express

Tip - Container Path is set in the Dockerfile with the WORKDIR keyword