passing in environment variables into docker running in kubernetes

8/2/2018

I have a dockerfile that looks like this at the moment:

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

and the pod config looks like this:

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

I want to be able to pass in these environment vars:

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"

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?

-- lightweight
docker
kubernetes

1 Answer

8/2/2018

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?

There is no need to specify any ENV directive in a Dockerfile; those directives only provide defaults in the case where (as in your example PodSpec) they are not provided at runtime.

The "how" is to do exactly what you have done in your example PodSpec: populate the env: array with the environment variables you wish to appear in the Pod

-- mdaniel
Source: StackOverflow