I'm managing a GitLab CICD pipeline which builds Docker images in a couple of stages and now I want to include in each Docker image a label with git's commit hash (see label-schema's vcs-ref label).
I've noticed that GitLab already provides convenient env variables with that info (see CI_COMMIT_SHA and CI_COMMIT_SHORT_SHA in GitLab CI/CD environment variables) but I have no idea of how to include the info provided in those env variables in a Docker image. Does anyone know if it's possible to include git's commit hash in a label?
You need to pass the commit as a build argument.
For example, in the Dockerfile:
ARG CI_COMMIT_SHA
LABEL git-commit=$CI_COMMIT_SHA
And when you build:
docker build --build-arg CI_COMMIT_SHA .
If you don't supply a variable, e.g. --build-arg CI_COMMIT_SHA=abc123 then it will use the local environmental variable of the same name.
In your Dockerfile, use LABEL
LABEL commit_sha=YOUR_CI_COMMIT_SHA
LABEL commit_short_sha=YOUR_CI_COMMIT_SHORT_SHA
Use docker inspect to get the labels from the images
docker inspect --format='{{.Config.Labels.commit_sha}}' image:tag
Related
Is there a way to refer a label (in current Dockerfile or inherited labels) in a Docker file? For example lets say I want to have a Dockerfile like below
FROM nginx
LABEL firstlabel="first label"
LABEL secondlabel="I want to use value of ${firstlabel}"
where I want to use the value of label "firstlabel" in the secondlabel. Is that possible? I tried using the ${} variable but it is not working.
You can use build arguments and environment variables to do this kind of thing.
You should be aware though that there are two phases for every container i. e. building and running. Labels defined in a Dockerfile are associated to an image so are generally set at build time although you can set/overwrite labels on docker run using --label <label-name>=<label-value> option. So passing a value for an environment variable that is used within a label to docker run will not update that label as the label is set on build time.
Consider the following Dockerfile:
FROM alpine
# build argument of name buildDate
ARG buildDate
# environment variables for author name
ENV AUTHOR_FNAME=John \
AUTHOR_LNAME=McClain
# we can use both in our labels
LABEL testlabel="Author: ${AUTHOR_FNAME} ${AUTHOR_LNAME}"\
buildDate=${buildDate}
Now build the container and provide the build arguments:
docker build -t test/test --build-arg buildDate=$(date +'%Y-%m-%d') .
Run the container:
docker run --name test -it --rm test/test sh
No open up another Terminal session and use the following command to inspect the labels:
docker inspect test --format='{{json .Config.Labels}}'
Result:
{"buildDate":"2022-04-28","testlabel":"Author: John McClain"}
You can now type exit into to sh to leave the container and it will automatically be removed.
I have a CI variable that I would like to use within my docker file. I have tried to include it such as
ENV TESTING_UNIT=$TESTING_ID
It seems like that you need to specify to Dockerfile that it expects an argument variable
With the following approach it will be available in the container
Change:
ENV TESTING_UNIT=$TESTING_ID
To:
ARG TESTING_UNIT_ARG
ENV TESTING_UNIT=$TESTING_UNIT_ARG
and build the image: docker build --build-arg TESTING_UNIT_ARG=$TESTING_ID
I am sending my application code to bitbucket repo without .env file and enable bitbucket pipeline to build a docker image for my application through Dockerfile which is already in my repo.
But the issue is my build needs the .env file through out building the image and after building the image !! My image needs to have an .env file !!
I am trying to figure it out through bitbucket repository variables but maybe they are not available after building the image !! but i need them after building image
You can use docker --env-file argument. With that you can give env file to docker while running it.
If you are using docker-compose or k8s, there are other ways to inject env variables to containers.
https://docs.docker.com/compose/environment-variables/
https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/
I have a Dockerfile and would like to parameterize the docker registry so I can pass different values at runtime
FROM $REGISTERY/xyz/image_name:tag
ENV....
How can I do that
Yes, you can use global args, You should pass some default value to make it work.
ARG REGISTERY=default.registry.com
FROM $REGISTERY/xyz/image_name:tag
So if you want to override during the time just pass
docker build --build-arg REGISTERY="myregistry.com" -t testimage .
Is there a way to pass AWS Codebuild environment variables into a Dockerfile?
I'd like to be able to pull from ECR like this:
FROM $My_AWS_ACCOUNT.dkr.ecr.us-east-1.amazonaws.com/someimage:latest
Where $My_AWS_ACCOUNT references an environment variables within my codebuild project.
Yes, you can use FROM ${My_AWS_ACCOUNT}.xxx. My_AWS_ACCOUNT should be passed as an argument to the docker build.
This is how I would do it:
ARG My_AWS_ACCOUNT=SOME_DEFAULT_IMAGE
FROM ${My_AWS_ACCOUNT}.xxx
When you build:
docker build --build-arg My_AWS_ACCOUNT=${My_AWS_ACCOUNT}
Yet another amazingly annoying thing in Docker that doesn't actually need to be this difficult but for some reason is supremely complicated and/or non-intuitive.
command line:
docker build --build-arg My_AWS_ACCOUNT=${My_AWS_ACCOUNT}
Dockerfile:
ARG My_AWS_ACCOUNT
FROM ${My_AWS_ACCOUNT}.dkr.ecr.us-east-1.amazonaws.com/someimage:latest