简体   繁体   English

在docker中使node_modules保持最新

[英]Keeping node_modules up to date in docker

I'm working with a team on a repo built in Docker and Node. 我正在与一个使用Docker和Node构建的repo的团队合作。 When someone else installs node packages, I git pull those changes to package.json and package-lock.json . 当其他人安装节点包时,我git pull这些更改git pullpackage.jsonpackage-lock.json

I can't just run docker-compose build and then docker-compose up , though, to install these new node packages. 不过,我不能只运行docker-compose build ,然后使用docker-compose up来安装这些新的节点包。 I have to first docker-compose down -v . 我必须首先使用docker-compose down -v This is because we're using a volume to store the node modules. 这是因为我们使用卷来存储节点模块。 We're not tied to this, and frankly would rather just store the modules in a docker image layer. 我们并不依赖于此,坦率地说,只是将模块存储在docker图像层中。 But then, when we bind the app volume ( - .:/app in docker-compose.yml ), the node_modules folder in the image is shadowed. 但是,当我们绑定应用程序卷( - .:/app in docker-compose.yml )时,图像中的node_modules文件夹被遮蔽。

It feels like we're taking the wrong approach to this, but this seems to be what the generally accepted practice is. 感觉我们对此采取了错误的方法,但这似乎是普遍接受的做法。 Our setup is based on " Lessons from Building a Node App in Docker " 我们的设置基于“ 在Docker中构建节点应用程序的经验教训

Our Dockerfile : 我们的Dockerfile

FROM node:8.4.0
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install --loglevel=error --progess=false
COPY . /app/

Our docker-compose.yml : 我们docker-compose.yml

---
version: '2.1'

services:
  main:
    build:
      context: .
    volumes:
      - .:/app
      - /app/node_modules
    container_name: main
    command: ['node', 'index.js']

My understanding of the issue is that, in the docker-compose.yml file, The volume - /app/node_modules creates a volume that copies the node modules from inside the image layer into a volume, meaning that when we build the app again with a different package.json , the image is updated but not the volume. 我对这个问题的理解是,在docker-compose.yml文件中,volume - /app/node_modules创建了一个卷,它将节点模块从图像层内部复制到一个卷中,这意味着当我们再次使用它构建应用程序时一个不同的package.json ,图像被更新但不是卷。 down ing the volume forces it to re-copy from the updated image node_modules , which is why that works. down音量迫使它从更新的图像node_modules重新复制,这就是为什么这样做的原因。 Please let me know if my understanding is incorrect :) 如果我的理解不正确,请告诉我:)

We're running in AWS ECS, so unfortunately we can only use docker features available in 17.03.2-ce (no multi-stage docker files for now). 我们在AWS ECS中运行,所以不幸的是我们只能使用17.03.2-ce中提供的docker功能(目前没有多级docker文件)。

Looks like you map the current folder to /app in container. 看起来您将当前文件夹映射到容器中的/ app。 I would erase ./node_modules if it exists in current folder, before doing docker-compose. 在执行docker-compose之前,我会删除当前文件夹中存在的./node_modules。

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

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