Ileana Vasquez
Ileana Vasquez

Reputation: 51

Set a variable from Gitlab to Dockerfile

I am trying to set a enviroment variable from GITLAB to my Dockerfile ENV env_var_name=$CI_JOB_NAME but It seems that is not set

$CI_JOB_NAME is a GitLab enviroment variable

# start from base
FROM ubuntu:18.04
ENV env_var_name=$CI_JOB_NAME
LABEL maintainer="Your Name <email.com>"

RUN apt-get update -y && \
    apt-get install -y python-pip python-dev


 # We copy just the requirements.txt first to leverage Docker cache
COPY ./requirements.txt /app/requirements.txt

WORKDIR /app

RUN pip install -r requirements.txt

COPY . /app

CMD [ "python", "./app.py" ]

is there another way to set a ENV

Upvotes: 1

Views: 992

Answers (1)

Narda Monserrat
Narda Monserrat

Reputation: 131

Since $CI_JOB_NAME is a enviroment variable of GitLab you have to pass this variable when the Dockerfile is builing using --build-arg

docker build --build-arg var_name={$CI_JOB_NAME} -t my-app

and you have to add the variable in your dockerfile

1 FROM ubuntu:18.04
2 ARG var_name # you could give this a default value as well
3 ENV env_var_name=$var_name
4 LABEL maintainer="Your Name <email.com>"
5
6 RUN apt-get update -y && \
7    apt-get install -y python-pip python-dev
8
9
10 # We copy just the requirements.txt first to leverage Docker cache
11 COPY ./requirements.txt /app/requirements.txt
12
13 WORKDIR /app
14
15 RUN pip install -r requirements.txt
16
17 COPY . /app
18
19 CMD [ "python", "./app.py" ]

To set environment variables during your image build, you will need either ENV or ARG and ENV at the same time. Docker ENV and ARG are pretty similar, but not quite the same. One of the differences: ARG can be set during the image build with --build-arg, but there is no such flag for ENV. ARG values can’t do the job - you can’t access them anymore once the image is built. The ENTRYPOINT command, which runs inside the future container, can’t access those values.

ENV values are accessible during the build, and afterwards once the container runs.

Upvotes: 3

Related Questions