dockervariablesgitlabdockerfilegitlab-ci

How to pass variables from .gitlab.ci.yml to Dockerfile


How can I pass one variables value from .gitlab-ci.yml to Dockerfile?

e.g. The .gitlab-ci.yml contains:

variables:
  var1: ex_variable_1
  var2: ex_variable_2

stages:
  - build

build:
  stage: build
  script:
    - sudo docker build . -t ${CI_PROJECT_PATH_SLUG}
                          --build-arg var1
                          --build-arg var2
    - sudo docker run -dit --name ${CI_PROJECT_PATH_SLUG} --cap-add=NET_ADMIN ${CI_PROJECT_PATH_SLUG}:latest

The Dockefile contains:

FROM centos:6.9

ENV var1 ${var1}
ENV var2 ${var2}

RUN echo "Print var1 $var1"
RUN echo "Print var2 $var2

So what I want is to pass var1 and var2 from .gitlab-ci.yml to Dockerfile.


Solution

  • To pass environment variables from .gitlab-ci.yml to an image or container: