Nodemon inside docker container
Asked Answered
R

4

14

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?

Reformatory answered 18/1, 2018 at 16:24 Comment(3)
ensure that all your files are correctly present in /usr/src/app by entering the container: docker exec -ti <container name> shIsotron
As I wrote the node_modules folder is missing if I attach the local volume to the container. Without the attacched volume, the modules are present but I can't "listen" for changes in host machine code.Reformatory
Ok I got it, I'll write an answerIsotron
I
12

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).

Isotron answered 18/1, 2018 at 16:37 Comment(1)
Holy xyeet, been searching for a solution for ages and this was such a clear explanation why the issue happens and and such an easy solution also! :)Esmond
K
7

TL;DR: npm install in a sub-folder, while moving the node_modules folder to the root.

Try this config to see and it should help you.

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

As the other answer said, even if you have run npm install at your WORKDIR. When you mount the volume, the content of the WORKDIR is replaced by your mount folder temporarily, which the npm install did not run.

As node search its require package in serveral location, a workaround is to move the 'installed' node_modules folder to the root, which is one of its require path.

Doing so you can still update code until you require a new package, which the image needs another build.

I reference the Dockerfile from this docker sample project.

Kimmel answered 23/8, 2018 at 3:20 Comment(0)
D
2

In a Javascript or a Nodejs application when we bind src file using bind volume in a Docker container either using docker command or docker-compose we end up overriding node_modules folder. To overcome this issue you need to use anonymous volume. In an anonymous volume, we provide only the destination folder path as compared to the bind volume where we specify source:destination folder path.

General syntax

--volume <container file system directory absolute path>:<read write access>

An example docker run command

docker container run \
    --rm \
    --detach \
    --publish 3000:3000 \
    --name hello-dock-dev \
    --volume $(pwd):/home/node/app \
    --volume /home/node/app/node_modules \
    hello-dock:dev

For further reference you check this handbook by Farhan Hasin Chowdhury

Diabolize answered 4/2, 2021 at 19:22 Comment(0)
I
0

Maybe there's no need to mount the whole project. In this case, I would only mount the directory where I put all the source files, e.g. src/.

This way you won't have any problem with the node_modules/ directory.

Also, if you are using Windows you may need to add the -L (--legacy-watch) option to the nodemon command, as you can see in this issue. So it would be nodemon -L.

Inbreeding answered 13/4, 2021 at 14:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.