为什么 npm 在 docker 中重建工作但 npm install 没有

我有一个访问一些私有存储库的节点项目。当我使用以下配置文件(从另一个开发人员继承)和以下 Dockerfile 将项目提交给 CircleCI 时,构建工作正常。但是,如果我将 Dockerfile 中的 npm rebuild 替换为 npm install,构建将由于无法获取我正在使用的某些私有存储库而失败。我知道(或认为我知道)就是这种情况,如果我使用 shell 运行 docker 映像并从 package.json 中删除私有存储库,则 npm install 工作正常。这是我对正在发生的事情的假设。

  1. circleCi 中的 npm ci 可以工作,因为 CircleCi 可以访问我们的 Github SSH 密钥,并且这样做会在此构建运行的容器中安装包
  2. 当我在 CircleCi 运行 npm ci 之后运行 docker build 命令时,node_modules 已经拥有我需要的所有包(由于 CircleCi 安装了它们。如果是这种情况,我想我在技术上不需要再做一次 npm安装在 Dockerfile 中) 2.a 这似乎是我在 CircleCi 配置定义的 docker“build”中运行 docker build(circleci 配置文件中的倒数第二个“run”命令)......
  3. 当我在 Dockerfile 中运行 npm install 时,它出错了,因为我没有 SSH 访问权限,无法拉下私有存储库。但是,npm rebuild 只是重新编译已经安装的包

这是正确的还是我离题了?如果这是正确的,那么我想如果我们没有使用任何私有存储库,我可以让 CircleCi 运行构建命令(即没有 npm install、apt get 等,我也在 Dockerfile 中执行)并使用Dockerfile 作为“配置”而不是在 circleci.yaml 文件中定义基本相同的配置,是吗?

是的,我知道我可以稍微清理一下那个 Dockerfile...只是想先了解一下发生了什么。

希望这有点清楚,并提前感谢您的任何澄清。

    docker:
      - image: circleci/node:14.17.3
    environment:
      - DOCKER_TAG: dev
    steps:
      - checkout
      - run:
          name: Install npm v6.12.0
          command: sudo npm install -g npm@6.12.0
      - restore_cache:
          key: cache-{{ checksum "package.json" }}
      - run:
          name: npm install ci
          command: npm ci --only=production
      - save_cache:
          key: cache-{{ checksum "package.json" }}
          paths:
            - ./node_modules
      - run:
          name: npm prune
          command: npm prune --production
      - setup_remote_docker
      - run:
          name: Install dependencies
          working_directory: /
          command: |
            sudo apt-get update --fix-missing
            sudo apt install -y python-pip python-dev
            sudo pip install awscli
      - run:
          name: build
          command: docker build --no-cache -t <repo>:<tag>
      - run:
          name: push
          command: make push (push to ECR)

# Using official Node.js base image
FROM node:14.17.3-slim

# Create a directory for our application and one for its log files
# Update the image and install supervisord which we will use to run the application
RUN mkdir /opt/app \
  && mkdir /var/log/app \
  && apt-get update \
  && apt-get -y upgrade \
  && apt-get install -y supervisor bzip2 build-essential python python-pip \
  && pip install awscli \
  && npm install -g sequelize-cli

# Add our supervisord configuration file
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf

WORKDIR /opt/app
ADD . /opt/app

# Run an npm rebuild
RUN npm rebuild

# Set NODE_PATH
ENV NODE_PATH /opt/app
# Expose the port the app listens on
EXPOSE 5000

# Inject the host's NODE_ENV environment variable
ENV NODE_ENV $NODE_ENV

# Run the app with supervisord
CMD ["bin/boot.sh"]

ivysoftware2009 回答:为什么 npm 在 docker 中重建工作但 npm install 没有

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/6973.html

大家都在问