Navneet Nandan Jha
Navneet Nandan Jha

Reputation: 1506

Passing a ENV variable for LABEL in Dockerfile

I need to pass an environment variable as LABEL in Dockerfile, my Dockerfile looks like below:

FROM nginx
LABEL maintainer="blabla"
ENV GROUP="default"
#Checking if Label values can be set from ENV Variables
LABEL group="${GROUP}"

below command to pass the build and run the docker file env variable:

docker build -t nginx:test_label .
docker run -d -e GROUP="mygroup" nginx:test_label

but when doing the inspect of the container, It is giving me label value as default instead of mygroup.

What am I doing wrong?

Upvotes: 5

Views: 9686

Answers (2)

Forketyfork
Forketyfork

Reputation: 7810

LABEL sets image-level metadata, so it will be the same for all containers created from this image.

If you want to override a label for a container, you have to specify it using a special --label key:

docker run --label GROUP=mygroup nginx:test_label

Currently you're passing the environment variable to the container during running. The Dockerfile is processed on build stage. If you want to have this variable substituted in the image, you have to pass it on build:

docker build --build-arg GROUP=mygroup -t nginx:test_label .

Upvotes: 3

Neo Anderson
Neo Anderson

Reputation: 6360

The docker image build and the docker run are two distinct steps. The problem here is that you cannot go back in time and build the image using a value that is supposed to be provided in a future step. You are instructing the image to LABEL group="${GROUP}", which at build-time is indeed equal to default.

Later on, when you run the container and inject the environment variable, you do it correctly, but there is no instruction executed at run-time that tells the container to do anything with that environment variable.

Upvotes: 0

Related Questions