命令 npm install 未在容器内执行

问题描述 投票:0回答:1

我有这个

docker-compose.yml

version: '3'
services:
  cypress:
    environment:
      - CYPRESS_BASE_URL=https://test.com
      - CYPRESS_USERNAME=user
      - CYPRESS_PASSWORD=password
    entrypoint: ["tail", "-f", "/dev/null"]
    build:
      context: .
      dockerfile: docker/Dockerfile
    volumes:
      - ./app:/app

我的

Dockerfile

FROM cypress/included:13.7.1

WORKDIR /app

# Copy application files
COPY ./app/package*.json ./
COPY ./app .

RUN npm install

# Set the default command to run when the container starts
CMD ["/bin/bash"]

树是这样的:

容器构建完成后,我连接到它,就可以看到文件 package.json;我运行 npm install 并且运行完美;

为什么这个 npm install 在容器构建过程中不起作用?

我尝试注释卷,在这种情况下,npm install 正在工作,但是 /node_modules、package-locak.json 仅在 docker 容器中可见;

提前谢谢;

node.js docker npm docker-compose
1个回答
0
投票

Compose 文件中的

volumes:
块会覆盖图像中的整个
/app
目录;您在 Dockerfile 中所做的任何操作都将不可见。

我通常会避免像这样的绑定安装。由于您将整个应用程序代码放入映像中(这是一个很好的做法),因此您可以删除绑定安装。

COPY 图像
 还包含标准 
cypress/included
,因此您无需覆盖 Dockerfile 中的
CMD
或强制覆盖 Dockerfile 中的
CMD
,以便容器不执行任何操作。
我可能会将 Dockerfile 更改为:

entrypoint:

撰写设置:

FROM cypress/included:13.7.1 WORKDIR /app COPY ./app/package*.json ./ # Run this _before_ the second `COPY` to avoid rerunning package # installation on non-package changes RUN npm install COPY ./app . # inherit the CMD from the base image

© www.soinside.com 2019 - 2024. All rights reserved.