0

I want to create an instance container with a Wordpress image.

I can create the container with:

gcloud compute instances create-with-container test-container --container-image=registry.hub.docker.com/library/wordpress --tags=http-server --zone=europe-west1-b --machine-type=n1-standard-1

How do I also pass the environment variables of the image?

-e WORDPRESS_DB_HOST=10.0.0.0
-e WORDPRESS_DB_USER="test"
-e WORDPRESS_DB_PASSWORD="test"
-e WORDPRESS_DB_NAME="test"
-e WORDPRESS_TABLE_PREFIX="wp_"

Can I do this with a flag in my create-with-container statement and how?

0

Similarly to the docker run command that allows passing environment variables:

$ docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  --env , -e        Set environment variables
  --env-file        Read in a file of environment variables

gcloud compute instances create-with-container supports similar functionality and command line parameters:

$ gcloud compute instances create-with-container INSTANCE_NAMES [INSTANCE_NAMES …]

  --container-env=[KEY=VALUE, …,…]
    Declare environment variables KEY with value VALUE passed to container. 
    Only the last value of KEY is taken when KEY is repeated more than once. 
    Values, declared with --container-env flag override those with the same KEY from file, provided in --container-env-file. 

  --container-env-file=CONTAINER_ENV_FILE
    Declare environment variables in a file. 
    Values, declared with --container-env flag override those with the same KEY from file.
    File with environment variables in format used by docker (almost). This means:
    • Lines are in format KEY=VALUE.
    • Values must contain equality signs.
    • Variables without values are not supported (this is different from docker format).
    • If # is first non-whitespace character in a line the line is ignored as a comment.
    • Lines with nothing but whitespace are ignored.

For example

CloudShell:$ gcloud compute instances create-with-container nginx-container --container-image gcr.io/cloud-marketplace/google/nginx1:1.14 --zone=europe-west3-c --tags=nginx --container-env=MYVAR1=myval1,MYVAR2=myval2

Alternatively, you may create a shell script like the create-vm.sh below:

#!/bin/bash
set -x
gcloud compute instances create-with-container nginx-container --container-image gcr.io/cloud-marketplace/google/nginx1:1.14 --zone=europe-west3-c --tags=nginx --container-env=MYVAR1=$1,MYVAR2=$2

launch it with the variables passed in the command line:

CloudShell:$ chmod +x create-vm.sh
CloudShell:$ ./create-vm.sh myval1 myval2

then connect via SSH to the newly created VM instance nginx-container and check variables within the Docker container:

nginx-container:$ sudo docker ps
CONTAINER ID    IMAGE                   COMMAND         CREATED     STATUS  NAMES 
8637cbacf2e1    gcr.io/cloud-marketplace/google/nginx1:1.14             "/usr/local/bin/dock…"  4 minutes ago   Up 4 minutes    klt-nginx-container-ikrz 
nginx-container:$ sudo docker exec 8637cbacf2e1 printenv MYVAR1 MYVAR2
myval1
myval2

Docker Doc > Command-line reference > Docker CLI (docker) > docker run

Developer Tools > Cloud SDK: Command Line Interface > Doc > Reference > gcloud compute instances create-with-container

2
  • I tried this also but I'm probably doing it wrong. I used --container-env=[WORDPRESS_DB_HOST=$1,WORDPRESS_DB_USER=wordpress, WORDPRESS_DB_PASSWORD=test,WORDPRESS_DB_NAME=$2,WORDPRESS_TABLE_PREFIX=wp_] With and without [] en also with "" and without. I still got the message :ERROR: (gcloud.compute.instances.create-with-container) unrecognized arguments: – kabouter Jun 8 '20 at 14:51
  • A working example has been added to the answer. – mebius99 Jun 26 '20 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.