Azr
Azr

Reputation: 1143

pass metadata to containers command in gce

I'd like to store an api key (eg:a twitter key) to metadata. Then use it from the command line of a sub container.

#api-keys.yaml:
TWITTER-CONSUMER-KEY: blahah
TWITTER-CONSUMER-SECRET: blihih

.

#google-container-manifest.yaml:
version: v1beta2
containers:
  - name: tweet-thing-one
    image: gcr.io/my-project/tweetfeed
    command:
     - --consumer-key=$TWITTER-CONSUMER-KEY
     - --consumer-secret=$TWITTER-CONSUMER-SECRET
     - --params-for=one
  - name: tweet-thing-two
    image: gcr.io/my-project/tweetfeed
    command:
     - --consumer-key=$TWITTER-CONSUMER-KEY
     - --consumer-secret=$TWITTER-CONSUMER-SECRET
     - --params-for=two
...

So I could run

$ gcloud compute instances create containervm-test-1 \
    --image container-vm \
    --metadata-from-file api-keys=api-keys.yaml \
                         google-container-manifest=google-container-manifest.yaml \
    --zone us-central1-a \
    --machine-type f1-micro

Thanks !

Upvotes: 1

Views: 1738

Answers (1)

Alexander Mohr
Alexander Mohr

Reputation: 61

Looks like two separate issues: (1) the api-keys.yaml file isn't translated into environment variables within the VM; instead, those need to be queried from the metadata server directly. See e.g. Instance environment variables and https://developers.google.com/compute/docs/metadata#querying.

(2) If you specify "command:" in the manifest, you need to specify the entire command you want to run, not just the flags. From https://cloud.google.com/compute/docs/containers/container_vms

containers[].command[] list of string The command line to run. If this is omitted, the container is assumed to have a command embedded in it."

Upvotes: 2

Related Questions