[英]Pulling private npm repository to docker container through kubernetes/skaffold
我是 skaffold、k8s、docker 集的新手,我在本地集群上構建應用程序時遇到了麻煩。
我有一個代碼存儲庫試圖提取私有 NPM package 但是在構建它時會丟失 .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 構建命令來做到這一點:
npm install
后立即刪除當我嘗試使用 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"]
還有腳手架文件:
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: .
幾點注意事項:
- 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.
任何見解都會令人驚嘆,我已經擺弄這個太久了。
謝謝!
構建和部署映像到 Kubernetes 分為三個級別:
Docker 不參與#3。 (這只是部分正確,因為一些集群也使用 Docker 來運行容器,但這是一個隱藏的細節並且也在發生變化。)
有兩個地方可以傳達秘密:
--build-args
或使用--secret
安裝機密(都需要 Buildkit) Skaffold 支持傳遞構建時機密,例如您的 npm 密碼,帶有 Docker 的--build-args
和--secret
標志,盡管它們略有重命名。
buildArgs
支持 Go 風格的模板,因此您可以將MYSECRET
等環境變量引用為{{.MYSECRET}}
:
build:
local:
useBuildkit: true
artifacts:
- image: auth
context: auth
docker:
buildArgs:
MYSECRET: "{{.MYSECRET}}"
然后您可以在Dockerfile
MYSECRET
ARG MYSECRET
RUN echo MYSECRET=${MYSECRET}
請注意,除非您通過ENV MYSECRET=${MYSECRET}
顯式分配它,否則 build-args 不會傳播到您的容器中。
如果秘密在本地文件中,您可以使用skaffold.yaml
中的secret
字段:
build:
local:
useBuildkit: true
artifacts:
- image: auth
context: auth
docker:
secret:
id: npmrc
src: /path/to/.npmrc
然后您將在Dockerfile
中引用該秘密:
RUN --mount=type=secret,mode=0644,id=npmrc,target=/app/.npmrc \
npm install
現在在您的Deployment
中,您正在嘗試為您的容器設置args
:
args: ["--no-cache", "--progress=plain", "--secret", "id=npmrc,src=.npmrc"]
args
字段會覆蓋映像中設置的CMD
指令。 此字段用於提供命令行 arguments 提供給圖像的入口點,可能是node
。 如果你想在集群上運行的容器中引用一個秘密,你可以使用Secret
或ConfigMap
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.