ligowsky
ligowsky

Reputation: 2193

How to use variables from gitlab-ci.yml in dockerfile

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

Upvotes: 13

Views: 26009

Answers (2)

Ovidiu Cristescu
Ovidiu Cristescu

Reputation: 1043

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

Upvotes: 4

jboockmann
jboockmann

Reputation: 1025

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.

Upvotes: 14

Related Questions