For example there is
.gitlab-cy.yml
with ENV_BACKEND_URI
variable
build:
stage: build
variables:
ENV_BACKEND_URI: "http://localhost:4200"
script:
- docker-compose build
docker-compose
uses dockerfile
FROM node:10-alpine as build-stage
...
...
...
RUN ["chmod", "+x", "/dummy.sh"]
...
...
And I want to use this ENV_BACKEND_URI
variable in dummy.sh
script
For example just to echo it
echo $ENV_BACKEND_URI
How do I pass it there?
I've already tried to set it in docker-compose.yml
environment:
- ENV_BACKEND_URI=${ENV_BACKEND_URI}
But it is't available in dockerfile
nor dymmy.sh
You can use ARG variable defaultValue and during the run command you can even update this value using --build-arg variable=value . To use these variables in the docker file you can refer them as $variable in run command.
With GitLab CI, it is possible to build a Docker image from a Dockerfile kept in a GitLab repository and upload it to the GitLab registry (default case) or to any other Docker registry.
Passing Environment Variables Into a Dockerfile Dockerfile provides a dedicated variable type ENV to create an environment variable. We can access ENV values during the build, as well as once the container runs. Let's see how we can use it to pass value to our greetings script. There are two different ways to do it.
I'm working on a React app. After I set the environment variables in Gitlab UI, I used them in my build stage in .gitlab-ci.yml
like this:
build-image:
stage: build
tags:
- *TARGET_DEV
script:
- docker build --rm -f docker/build/Dockerfile
--build-arg REACT_APP_FSI_IMPORT_QLIKSENSE_URL=$REACT_APP_FSI_IMPORT_QLIKSENSE_URL
--build-arg REACT_APP_REPORT_QLIKSENSE_URL=$REACT_APP_REPORT_QLIKSENSE_URL
Simply declaring them worked for me at the top of the Dockerfile
, but below FROM
:
FROM ...
ARG REACT_APP_REPORT_QLIKSENSE_URL
ARG REACT_APP_FSI_IMPORT_QLIKSENSE_URL
If I understand you correctly, then you want to bake the value of an environment variable into your image. Note that you rather want the opposite, i.e., build a generic image and adjust its behavior by setting environment variables. Still, both approaches are similar.
In order to bake the value of an environment variable into an image, you have to specify the value of your environment variable using the ARG
keyword inside your Dockerfile and pass its value either via --build-arg
when using docker build
from the shell, or via the args
keyword inside your compose file. Consider the following Dockerfile and docker-compose file:
Dockerfile
FROM alpine
ARG ENV_BACKEND_URI=$ENV_BACKEND_URI
RUN mkdir -p /app
RUN echo $ENV_BACKEND_URI > /app/script.sh
CMD ["cat", "/app/script.sh"]
docker-compose.yml
version: "3.7"
services:
app:
build:
context: .
args:
ENV_BACKEND_URI: "google.de"
Using the ARG
keyword in the Dockerfile we state that we want to use the variable ENV_BACKEND_URI
at build time and its value shall be equal to the value of the environment ENV_BACKEND_URI
that we receive during building. We do so in the compose file by using the arg
keyword.
Upon invoking docker-compose build && docker-compose up
you will eventually see the ouput app_1 | google.de
. If you prefer using the docker build
command you have to invoke docker build --build-arg ENV_BACKEND_URI=google.de --tag=foo .
first and then execute it by calling docker run foo
. Both yield the same output, i.e., they print google.de
to stdout.
For your CI/CD use case, you have to set the value of the variable inside your compose file to the value of the variable in the environment, i.e., ENV_BACKEND_URI: $ENV_BACKEND_URI
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With