Pass gitlab variables to a speccific path in Dockerfile

Hey,

I have a very basic gitlab pipeline that creates a docker image and push it to Gitlab container registry. However, I need t opass some variables from “gitlab variables” to the .env file in that path: /var/www/html/.env

Here are my configurations:

.gitlab-ci.yml file:

stages:
  - build-worker

image: docker:latest

services:
    - docker:dind

before_script:
  - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY

worker:
  stage: build-worker
  variables:
    ENVVARS: "${ENV_DEV}"
  script:
    - docker build -t "$CI_REGISTRY_IMAGE" .
    - docker push "$CI_REGISTRY_IMAGE"
  when: always

Dockerfile:

FROM php:7.2.19-cli

RUN export DEBIAN_FRONTEND=noninteractive \
    && apt-get update 

ARG ENVVARS
COPY ./project /var/www/html/
WORKDIR /var/www/html/
RUN echo "${ENVVARS}" > /var/www/html/.env

In gitlab variables section, here is my ENVVARS key:

However, when I pull the image from gitlab registry and run the container, I found nothing in the .env file… I tried so many things using ARG and ENV instructions.

So, what I’m missing here!

Thanks,
Ali

try docker build --build-arg ENVVARS=${ENVVARS} -t "$CI_REGISTRY_IMAGE" .

Though, wouldn’t it be better to create an entrypoint script that updates your configuration file when a container of the image is started? Do you realy want to be build an image for dev and a new one for prod?