Nodemon inside docker container

Jumpa picture Jumpa · Jan 18, 2018 · Viewed 13.6k times · Source

I'm trying to use nodemon inside docker container:

Dockerfile

FROM node:carbon
RUN npm install -g nodemon
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "nodemon" ]

Build/Run command

docker build -t tag/apt .
docker run -p 49160:8080 -v /local/path/to/apt:/usr/src/app -d tag/apt

Attaching a local volume to the container to watch for changes in code, results in some override and nodemon complains that can't find node modules (any of them). How can I solve this?

Answer

whites11 picture whites11 · Jan 18, 2018

In you Dockerfile, you are running npm install after copying your package*json files. A node_modules directory gets correctly created in /usr/src/app and you're good to go.

When you mount your local directory on /usr/src/app, though, the contents of that directory inside your container are overriden with your local version of the node project, which apparently is lacking the node_modules directory, causing the error you are experiencing.

You need to run npm install on the running container after you mounted your directory. For example you could run something like:

docker exec -ti <containername> npm install

Please note that you'll have to temporarily change your CMD instruction to something like:

CMD ["sleep", "3600"]

In order to be able to enter the container.

This will cause a node_modules directory to be created in your local directory and your container should run nodemon correctly (after switching back to your current CMD).