【问题标题】:Conditional npm install ARG in DockerfileDockerfile 中的条件 npm install ARG
【发布时间】:2020-04-25 16:15:04
【问题描述】:

我正在尝试根据参数从 GitHub 安装节点模块包。我已经尝试过,但是我不完全理解这个解决方案。执行没有错误,但我确信它没有安装包。我在部署之前在本地运行容器并获取模块未找到错误

Dokerfile是这样的,能不能把每条命令都换行?

FROM node:10

ENV HOME /usr/src/reach-live-api

ARG siteval

RUN mkdir -p $HOME
RUN mkdir -p /root/.ssh
WORKDIR $HOME

COPY ./keys/reachlive_rsa /root/.ssh/id_rsa
RUN chmod 700 /root/.ssh/id_rsa
RUN echo "Host bitbucket.org\n\tStrictHostKeyChecking no\n" >> /root/.ssh/config

COPY . $HOME
RUN npm install --silent --production

RUN if [ "$siteval" = "prod" ]; then \
    RUN npm install "reach-live-elasticsearch@git+https://github.com/lalitmohan001/reach-live-elasticsearch-prod.git" \
    RUN npm install "reach-live-firebase@git+https://github.com/lalitmohan001/reach-live-firebase-prod.git" \
    RUN npm install "reach-live-paypal@git+https://github.com/lalitmohan001/reach-live-paypal-prod.git" \
    else \
    RUN npm install "reach-live-elasticsearch@git+https://github.com/lalitmohan001/reach-live-elasticsearch.git" \
    RUN npm install "reach-live-firebase@git+https://github.com/lalitmohan001/reach-live-firebase.git" \
    RUN npm install "reach-live-paypal@git+https://github.com/lalitmohan001/reach-live-paypal.git"; \
    fi 

RUN npm prune --production

RUN rm -fr .npm
RUN rm -fr /root/.ssh
RUN rm -fr keys

CMD ["npm", "start"]

我使用下面的命令来构建

docker build -t gcr.io/reachlive-123/api:25Apr2020 . --build-arg siteval=dev

非常感谢您的帮助

【问题讨论】:

  • (如果你在构建的镜像上运行docker history,它将以纯文本形式保存你的 GitHub 凭据,任何人都可以看到;你最好尝试在 Docker 之外克隆这些依赖项。 )
  • 如果您没有收到无法找到命令 RUN 的 shell 错误,则此行根本没有运行。你能把它扩展成一个完整的minimal reproducible example,包括整个Dockerfile和你正在运行的docker build命令吗?
  • docker build -t gcr.io/reachlive-123/api:25Apr2020 . --build-arg siteval=dev,我用来创建构建,我还用完整的 docker 文件更新了问题。此外,git repo 是私有的,但我在 docker 文件中使用了令牌,但出于安全原因,我没有将令牌添加到上述问题中 @DavidMaze
  • 我确信它已经到达 npm install 的步骤,但它认为这是一个字符串而不是执行@DavidMaze

标签: docker dockerfile npm-install


【解决方案1】:

原来问题出在

RUN if [ "$siteval" = "prod" ]; then \
 RUN npm install "reach-live-elasticsearch@git+https://github.com/lalitmohan001/reach-live-elasticsearch-prod.git" \
 RUN npm install "reach-live-firebase@git+https://github.com/lalitmohan001/reach-live-firebase-prod.git" \
 RUN npm install "reach-live-paypal@git+https://github.com/lalitmohan001/reach-live-paypal-prod.git" \
 else \
 RUN npm install "reach-live-elasticsearch@git+https://github.com/lalitmohan001/reach-live-elasticsearch.git" \
 RUN npm install "reach-live-firebase@git+https://github.com/lalitmohan001/reach-live-firebase.git" \
 RUN npm install "reach-live-paypal@git+https://github.com/lalitmohan001/reach-live-paypal.git"; \
 fi 

我已将其更改为并且可以正常工作

RUN if [ "$arg" = "prod" ]; then \
 npm install reach-live-elasticsearch@git+https://github.com/lalitmohan001/reach-live-elasticsearch-prod.git \
 reach-live-firebase@git+https://github.com/lalitmohan001/reach-live-firebase-prod.git \ 
 reach-live-paypal@git+https://github.com/lalitmohan001/reach-live-paypal-prod.git ; \
 else \ 
 npm install reach-live-elasticsearch@git+https://github.com/lalitmohan001/reach-live-elasticsearch.git \ 
 reach-live-firebase@git+https://github.com/lalitmohan001/reach-live-firebase.git \ 
 reach-live-paypal@git+https://github.com/lalitmohan001/reach-live-paypal.git; \
 fi

感谢 Pavittar Singh 帮助我们解决这个问题!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-29
    • 2018-12-06
    • 2014-07-19
    • 2019-07-06
    • 1970-01-01
    • 2019-04-22
    • 1970-01-01
    • 2017-12-22
    相关资源
    最近更新 更多