为什么docker创建空的node_modules以及如何避免它? [英] Why does docker create empty node_modules and how to avoid it?

查看:128
本文介绍了为什么docker创建空的node_modules以及如何避免它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有些类似的问题,但它们没有t回答了为什么即使将dockerfile设置为将node_modules容纳在容器中,Docker也仍在回购中创建空的 node_modules 目录?

There are some similar questions but they haven't answered why docker creates the empty node_modules directory in the repo even though the dockerfile is setup to hold node_modules in the container?

我想知道为什么在空的主机上创建目录,并考虑到yarn已经在 node_modules 内的容器内安装了软件包,以及如何避免该软件包。



I'm interested to know why directory is created on the host empty, give that yarn already installs packages inside the container within node_modules and how to avoid it.

## Dockerfile

FROM node:8.11.4-alpine
RUN apk update && apk add yarn
RUN yarn global add nodemon
WORKDIR /usr/app

COPY package.json yarn.lock /usr/app/
RUN yarn

EXPOSE 3000



## docker-compose.yml

version: "3.2"

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    command: nodemon index.js
    volumes:
      - .:/usr/app
      - /usr/app/node_modules
    ports:
      - "3000:3000"


推荐答案

是的,您的映像构建过程是将节点程序包安装到映像中的 node_modules 目录中。因此,在构建映像之后,映像包含 node_modules ,您可以使用它来运行应用程序。

You are right, your image building process is installing the node packages into the node_modules directory in the image. So, after you build your image the image contains node_modules and you can use it to run your application.

您请参阅主机上的 node_modules ,因为您在Compose文件中设置了卷。

You see node_modules on your host machine, because of your volumes setup in your Compose file. There is more to it than you see in the other answer though.

正在发生的事情是,您正在映射。:/ usr / app 在第一个卷定义中,这意味着您正在将主机上的当前目录映射到容器中的 / usr / app

What's happening is that you are mapping .:/usr/app in your first volume definition, which means that you are mapping your current directory on the host to /usr/app in the container.

这将使用主机上的当前目录覆盖映像中的 / usr / app 目录。而且您的主机没有 node_modules 目录(除非您也在主机上安装了node_modules),因此您的容器将无法使用此映射,因为您已覆盖 / usr / app 并且覆盖中没有 node_modules 目录。节点将抛出一个错误无法找到节点模块。

This will override the /usr/app directory in the image with the current directory on the host. And your host does not have the node_modules directory (unless you installed node_modules on the host, too) and therefore your container will not work with this mapping, beacuse you've overriden /usr/app and there is no node_modules dir in the override. Node will throw an error that it cannot find node modules.

下一个卷映射解决了这种情况,这实际上是常见的Node开发设置。您创建一个卷 / usr / app / node_modules 。注意,该卷没有主机部分,在映射中没有,这里只有一个目录。这意味着Docker将从映像中挂载 / usr / app / node_modules 目录,并将其添加到之前将主机目录映射到的映射中。 / usr / app

The next volume mapping solves the situation, this is actually a common Node development setup. You create a volume /usr/app/node_modules. Note, that this volume does not have a host part there is no : in the mapping, there is only one directory here. This means that Docker will mount the /usr/app/node_modules directory from the image and add it to the previous mapping where you mapped the host dir to /usr/app.

因此,在运行的容器中,您将拥有来自主机当前目录的源代码,以及PLUS node_modules

So in the running container you'll have your source code from the host's current directory PLUS node_modules from the underlying image due to the double mapping.

作为副作用,您会看到一个空的 node_modules 目录在您主机的当前目录中。

As a side effect you'll see an empty node_modules directory in your host's current directory.

这篇关于为什么docker创建空的node_modules以及如何避免它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆