Reputation: 23
I'm using Google Cloud Container Builder to build my images. I connected it to my GitHub repository so it is triggered by push to specific branch. I provide some env variables to my build step explicitly and using prepositions.
cloudbuild.yaml
steps:
...
- name: gcr.io/cloud-builders/docker
args: ['build', '-t', 'eu.gcr.io/${PROJECT_ID}/project-name:${SHORT_SHA}', '.']
env:
- 'TEST_ENV=test123'
- 'TEST=${_TEST}'
...
Dockerfile
FROM alpine:latest
RUN printenv
...
I can see my env variables in build details at Env. Vars section of this build step when build is triggered: TEST_ENV=test123 TEST=test_prepositon
But when RUN printenv
is executed I see only HOSTNAME, SHLVL, HOME, PATH, PWD
env vars.
Where is my mistake? Thank you in advance.
Upvotes: 2
Views: 5183
Reputation: 854
Here is a full example of what you are looking for.
cloudbuild.yaml:
steps:
- name: "gcr.io/cloud-builders/docker"
args: [
"build",
"-t",
"gcr.io/$PROJECT_ID/$REPO_NAME:$SHORT_SHA",
".",
"--build-args", "SOME_ARG=${_VAR_NAME_FROM_GOOGLE_TRIGGERS}"
]
Dockerfile:
ARG SOME_ARG
ENV SOME_ENV_VAR_NAME=${SOME_ARG}
The variable SOME_ENV_VAR_NAME
could be accessed right in the docker container
Also, I would like to add 2 great articles that explain docker arg and env:
Upvotes: 4
Reputation: 1268
Your example is passing your environment variables to the docker
command but not to the docker build context.
When you set environment variables via env
, these envvars are set in the running container. But when docker executes a docker build
it does not by default pass the environment though to the build context.
To set environment variables such that they are accessible in the build context via a RUN
directive, pass your docker build
a --build-arg
.
Here is Docker's documentation on setting build-time variables.
Upvotes: 2