gpt4 book ai didi

docker - 将环境变量传递到在kubernetes中运行的docker中

转载 作者:行者123 更新时间:2023-12-02 21:14:59 29 4
gpt4 key购买 nike

我现在有一个看起来像这样的dockerfile:

FROM golang:1.8-alpine
COPY ./ /src
ENV GOOGLE_CLOUD_PROJECT = "snappy-premise-118915"
RUN apk add --no-cache git && \
apk --no-cache --update add ca-certificates && \
cd /src && \
go get -t -v cloud.google.com/go/pubsub && \
CGO_ENABLED=0 GOOS=linux go build main.go

# final stage
FROM alpine
ENV LATITUDE "-121.464"
ENV LONGITUDE "36.9397"
ENV SENSORID "sensor1234"
ENV ZIPCODE "95023"
ENV INTERVAL "3"
ENV GOOGLE_CLOUD_PROJECT "snappy-premise-118915"
ENV GOOGLE_APPLICATION_CREDENTIALS "/app/key.json"
ENV GRPC_GO_LOG_SEVERITY_LEVEL "INFO"
RUN apk --no-cache --update add ca-certificates
WORKDIR /app
COPY --from=0 /src/main /app/
COPY --from=0 /src/key.json /app/
ENTRYPOINT /app/main

和pod配置看起来像这样:
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: sensorpub
spec:
template:
metadata:
labels:
app: sensorpub
spec:
volumes:
- name: google-cloud-key
secret:
secretName: pubsub-key
containers:
- name: sensorgen
image: gcr.io/snappy-premise-118915/sensorgen:v1
volumeMounts:
- name: google-cloud-key
mountPath: /var/secrets/google
env:
- name: GOOGLE_APPLICATION_CREDENTIALS
value: /var/secrets/google/key.json

我希望能够通过以下环境变量:
ENV LATITUDE "-121.464"
ENV LONGITUDE "36.9397"
ENV SENSORID "sensor1234"
ENV ZIPCODE "95023"
ENV INTERVAL "3"
ENV GOOGLE_CLOUD_PROJECT "snappy-premise-118915"
ENV GOOGLE_APPLICATION_CREDENTIALS "/app/key.json"
ENV GRPC_GO_LOG_SEVERITY_LEVEL "INFO"

我希望能够在pod配置中设置环境变量,以便docker文件可以使用这些变量...我该怎么做,而不是直接将它们直接编码到docker镜像中?

最佳答案

I want to be able to set the environment variables in the pod config so that the docker file can use those...how do I do that instead of just coding them into the docker image directly?



无需在Dockerfile中指定任何 ENV指令 ;这些伪指令仅在(在您的示例PodSpec中)未在运行时提供它们的情况下提供默认值。

“方法”是完全按照您在示例PodSpec中所做的操作进行操作:使用希望出现在Pod中的环境变量填充env:数组

关于docker - 将环境变量传递到在kubernetes中运行的docker中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51657271/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com