简体   繁体   中英

Nodemon inside docker container

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?

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

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 .

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

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, eg 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM