简体   繁体   English

Docker x NodeJS-node_modules问题

[英]Docker x NodeJS - Issue with node_modules

I'm a web developer who currently is working on a next.js project (it's just a framework to SSR ReactJS). 我是一名网络开发人员,目前正在研究next.js项目(这只是SSR ReactJS的框架)。 I'm using Docker config on this project and I discovered an issue when I add/remove dependencies. 我在该项目上使用Docker配置,添加/删除依赖项时发现了一个问题。 When I add a dependency, build my project and up it with docker-compose, my new dependency isn't added to my Docker image. 添加依赖项时,构建我的项目并使用docker-compose进行构建,新的依赖项不会添加到我的Docker映像中。 I have to clean my docker system with docker system prune to reset everything then I could build and up my project. 我必须使用docker system prune以重置所有内容,然后才能构建和启动项目。 After that, my dependency is added to my Docker container. 之后,将我的依赖项添加到我的Docker容器中。

I use Dockerfile to configure my image and different docker-compose files to set different configurations depending on my environments. 我使用Dockerfile来配置映像,并使用不同的Dockerfile docker-compose文件来根据环境设置不同的配置。 Here is my configuration: 这是我的配置:

Dockerfile Dockerfile

FROM node:10.13.0-alpine

# SET environment variables
ENV NODE_VERSION 10.13.0
ENV YARN_VERSION 1.12.3

# Install Yarn
RUN apk add --no-cache --virtual .build-deps-yarn curl \
  && curl -fSLO --compressed "https://yarnpkg.com/downloads/$YARN_VERSION/yarn-v$YARN_VERSION.tar.gz" \
  && tar -xzf yarn-v$YARN_VERSION.tar.gz -C /opt/ \
  && ln -snf /opt/yarn-v$YARN_VERSION/bin/yarn /usr/local/bin/yarn \
  && ln -snf /opt/yarn-v$YARN_VERSION/bin/yarnpkg /usr/local/bin/yarnpkg \
  && rm yarn-v$YARN_VERSION.tar.gz \
  && apk del .build-deps-yarn

# Create app directory
RUN mkdir /website
WORKDIR /website
ADD package*.json /website

# Install app dependencies 
RUN yarn install

# Build source files
COPY . /website/
RUN yarn run build

docker-compose.yml (dev env) docker-compose.yml(开发环境)

version: "3"
services:
  app:
    container_name: website
    build:
      context: .
    ports:
      - "3000:3000"
      - "3332:3332"
      - "9229:9229"
    volumes:
      - /website/node_modules/
      - .:/website
    command: yarn run dev 0.0.0.0 3000
    environment:
      SERVER_URL: https://XXXXXXX.com

Here my commands to run my Docker environment: 这是我运行Docker环境的命令:

docker-compose build --no-cache docker-compose build --no-cache

docker-compose up 码头工人组成

I suppose that something is wrong in my Docker's configuration but I can't catch it. 我想我的Docker配置出了点问题,但是我无法抓住它。 Do you have an idea to help me? 您有帮助我的主意吗?

Thanks! 谢谢!

Your volumes right now are not set up to do what you intend to do. 您当前的卷尚未设置为可以执行的操作。 The current set below means that you are overriding the contents of your website directory in the container with your local . 下面的当前设置表示您正在使用local覆盖容器中website目录的内容. directory. 目录。

volumes:
  - /website/node_modules/
  - .:/website

I'm sure your intention is to map your local directory into the container first, and then override node_modules with the original contents of the image's node_modules directory, ie /website/node_modules/ . 我敢肯定你的意图是本地目录第一映射到容器中,然后覆盖node_modules与图像的node_modules目录的原始内容,即/website/node_modules/

Changing the order of your volumes like below should solve the issue. 如下更改卷的顺序应该可以解决问题。

volumes:
  - .:/website
  - /website/node_modules/

You are explicitly telling Docker you want this behavior. 您明确告诉Docker您想要这种行为。 When you say: 当你说:

volumes:
  - /website/node_modules/

You are telling Docker you don't want to use the node_modules directory that's baked into the image. 您正在告诉Docker您不想使用node_modules包含的node_modules目录。 Instead, it should create an anonymous volume to hold the node_modules directory (which has some special behavior on its first use) and persist the data there, even if other characteristics like the underlying image change. 相反,它应该创建一个匿名卷来保存node_modules目录(该目录在首次使用时具有某些特殊行为),并将数据保留在那里,即使基础图像等其他特征发生了变化。

That means if you change your package.json and rebuild the image, Docker will keep using the volume version of your node_modules directory. 这意味着,如果您更改package.json并重建映像,则Docker将继续使用node_modules目录的卷版本。 (Similarly, the bind mount of .:/website means everything else in the last half of your Dockerfile is essentially ignored.) (类似地, .:/website的绑定挂载实际上意味着忽略了Dockerfile的后半部分中的所有其他内容。)

I would remove the volumes: block in this setup to respect the program that's being built in the image. 我将删除volumes:块,以尊重映像中正在构建的程序。 (I'd also suggest moving the command: to a CMD line in the Dockerfile .) Develop and test your application without using Docker, and build and deploy an image once it's essentially working, but not before. (我还建议将command:移到DockerfileCMD行。)无需使用Docker即可开发和测试您的应用程序,并在其基本运行后(而不是之前)构建并部署映像。

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

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