简体   繁体   English

Docker 容器内的 Nodemon

[英]Nodemon inside docker container

I'm trying to use nodemon inside docker container:我正在尝试在 docker 容器中使用 nodemon:

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).将本地卷附加到容器以监视代码更改,会导致一些覆盖和 nodemon 抱怨找不到节点模块(其中任何一个)。 How can I solve this?我该如何解决这个问题?

In you Dockerfile , you are running npm install after copying your package*json files.Dockerfile ,您在复制package*json文件后运行npm install A node_modules directory gets correctly created in /usr/src/app and you're good to go./usr/src/app正确创建了一个node_modules目录,您就可以开始了。

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.但是,当您将本地目录挂载到/usr/src/app ,容器内该目录的内容将被本地版本的节点项目覆盖,该版本显然缺少node_modules目录,从而导致您遇到错误。

You need to run npm install on the running container after you mounted your directory.挂载目录后,您需要在正在运行的容器上运行npm install 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指令更改为以下内容:

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 ).这将导致在您的本地目录中创建一个 node_modules 目录,并且您的容器应该正确运行 nodemon (切换回当前的CMD )。

TL;DR : npm install in a sub-folder, while moving the node_modules folder to the root. TL;DR : npm install在子文件夹中,同时将node_modules文件夹移动到根目录。

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 .正如另一个答案所说,即使您已在WORKDIR运行npm install When you mount the volume, the content of the WORKDIR is replaced by your mount folder temporarily, which the npm install did not run.挂载卷时, WORKDIR的内容会临时替换为您的挂载文件夹,而npm install并未运行该文件夹。

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.当节点在多个位置搜索它的require 包时,一种解决方法是将“已安装”的node_modules文件夹移动到根目录,这是它的 require 路径之一。

Doing so you can still update code until you require a new package, which the image needs another build .这样做你仍然可以更新代码,直到你需要一个新的包,镜像需要另一个build

I reference the Dockerfile from this docker sample project .我从这个 docker 示例项目中引用了 Dockerfile。

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.在 Javascript 或 Nodejs 应用程序中,当我们使用 docker 命令或 docker-compose 在 Docker 容器中使用绑定卷绑定 src 文件时,我们最终会覆盖 node_modules 文件夹。 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.在匿名卷中,与指定source:destination文件夹路径的绑定卷相比,我们仅提供目标文件夹路径。

General syntax一般语法

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

An example docker run command一个示例 docker run 命令

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如需进一步参考,请查看 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/ .在这种情况下,我只会挂载我放置所有源文件的目录,例如src/

This way you won't have any problem with the node_modules/ directory.这样你就不会对node_modules/目录有任何问题。

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 .此外,如果您使用的是 Windows,您可能需要在 nodemon 命令中添加 -L (--legacy-watch) 选项,如您在本期中所见。 So it would be nodemon -L .所以它将是nodemon -L

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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