How to pass runtime ENV variables to a Docker container that's used to run a GitlabCI job?

31 Views Asked by At

Context:

We use self-hosted Docker containers to run our GitlabCI jobs in order to build/test mobile projects. For this we have a custom Docker image that we build based on a custom Dockerfile, then publish on Artifact Registry and subsequently use for containers in all our standalone mobile projects.

Each mobile project that we want to run on GitlabCI has its own .gitlab-ci.yml file in which we specify all the project specific build stages and also the image that should be used for all build stages mentioned previously like such (and like the official documentation), at the beginning of the file (not production code, just for demo purposes):

image: eu.gcr.io/android-build-image:1.0.0

Question:

We'd like to pass a runtime variable from each project's .gitlab-ci.yml file to the Docker container during the container initialisation time, so that we can configure some host ENVs inside the Dockerfile, before running any sort of project specific build steps. How can that be achieved?

1

There are 1 best solutions below

0
KamilCuk On BEST ANSWER

So create a dockerfile:

FROM eu.gcr.io/android-build-image:1.0.0
COPY ./yourentrypoint.sh /yourentrypoint.sh
ENTRYPOINT ["bash", "/yourentrypoint.sh"]

yourentrypoint.sh may look like this:

#!/bin/bash
if something; then
    export ENV=something
fi
exec "$@"

See https://docs.docker.com/build/guide/intro/ and https://mywiki.wooledge.org/BashGuide/Parameters#Variable_Types .

Build and publish that docker image - see https://docs.gitlab.com/ee/ci/docker/using_docker_build.html , https://docs.gitlab.com/ee/ci/docker/using_kaniko.html , and https://docs.gitlab.com/ee/user/packages/container_registry/ , also see https://docs.gitlab.com/ee/ci/docker/using_docker_images.html .

Then in image: use your image. You may consider docker inspect eu.gcr.io/android-build-image:1.0.0 to execute the original image entrypoint from your entrypoint.