【发布时间】:2021-06-02 06:18:12
【问题描述】:
我是 skaffold、k8s、docker set 的新手,我在本地集群上构建应用程序时遇到了麻烦。
我有一个代码库试图拉取私有 NPM 包,但在构建它时会丢失 .npmrc 文件或 npm 机密。
npm ERR! code E404
npm ERR! 404 Not Found - GET https://registry.npmjs.org/@sh1ba%2fcommon - Not found
npm ERR! 404
npm ERR! 404 '@sh1ba/common@^1.0.3' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2021-06-02T06_08_57_246Z-debug.log
unable to stream build output: The command '/bin/sh -c npm install' returned a non-zero code: 1. Please fix the Dockerfile and try again..
理想情况下,我想避免将秘密硬编码到文件中,并使用 k8s 环境变量将密钥作为秘密传递给 docker。我可以(有点)使用 docker build 命令来做到这一点:
- 带有“--build-args”和 npm 秘密(不安全的方式)
- 带有“--secret”和 npm secret(更好的方法)
- 直接复制 .npmrc 文件,
npm installing 后立即删除
当我尝试使用 kubernetes/skaffold 构建它时,就会出现问题。运行后,似乎没有找到任何 args、env 变量,甚至 .npmrc 文件。在检查 dockerfile 以获取线索时,我能够确定没有任何内容从清单(定义的参数、.npmrc 文件等)传递到 dockerfile。
以下是应用程序的清单:
apiVersion: apps/v1
kind: Deployment
metadata:
name: auth-depl
spec:
replicas: 1
selector:
matchLabels:
app: auth
template:
metadata:
labels:
app: auth
spec:
containers:
- name: auth
image: auth
env:
- name: NPM_SECRET
valueFrom:
secretKeyRef:
name: npm-secret
key: NPM_SECRET
args: ["--no-cache", "--progress=plain", "--secret", "id=npmrc,src=.npmrc"]
这是 dockerfile 中的代码:
# syntax=docker/dockerfile:1.2
# --------------> The build image
FROM node:alpine AS build
WORKDIR /app
COPY package*.json .
RUN --mount=type=secret,mode=0644,id=npmrc,target=/app/.npmrc \
npm install
# --------------> The production image
FROM node:alpine
WORKDIR /app
COPY package.json .
COPY tsconfig.json .
COPY src .
COPY prisma .
COPY --chown=node:node --from=build /app/node_modules /app/node_modules
COPY --chown=node:node . /app
s
RUN npm run build
CMD ["npm", "start"]
还有 skaffold 文件:
apiVersion: skaffold/v2alpha3
kind: Config
deploy:
kubectl:
manifests:
- ./infra/k8s/*
- ./infra/k8s-dev/*
build:
local:
push: false
artifacts:
- image: auth
context: auth
docker:
dockerfile: Dockerfile
sync:
manual:
- src: 'src/**/*.ts'
dest: .
几点说明:
- 无论我复制和粘贴到何处(在 auth、清单、skaffold 和 ~/ 目录中),我都找不到 .npmrc 文件
- 我也想在生产中使其半可用(相当可重复使用),这样如果可能的话我不需要进行彻底的大修(但如果这是不好的做法,我想听听更多关于它的信息)
- 我已经能够使其与 skaffold.yaml 文件中的 buildArgs 一起工作,但我不确定这将如何转化为生产环境,因为我无法将构建 args 从 kubernetes 传递到 docker(我阅读了它不安全,应该使用秘密)
- 清单中的 args 也抛出此错误(如果 args 被注释掉,服务器运行):
- deployment/auth-depl: container auth terminated with exit code 9
- pod/auth-depl-85fb8975d8-4rh9r: container auth terminated with exit code 9
> [auth-depl-85fb8975d8-4rh9r auth] node: bad option: --progress=plain
> [auth-depl-85fb8975d8-4rh9r auth] node: bad option: --secret
- deployment/auth-depl failed. Error: container auth terminated with exit code 9.
任何见解都会令人惊叹,我已经摆弄这个太久了。
谢谢!
【问题讨论】:
-
你好@GerrySaporito,欢迎来到 StackOverflow!看起来您将错误的 URL 放入私有 NPM 包。尝试在浏览器中输入此地址。您还将获得 404 答案 - 未找到。你必须有你的 NPM 包的正确地址。另请参阅here。这是类似的问题。
标签: docker npm kubernetes skaffold .npmrc