How to parametrized docker registery in a dockerfile - docker

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 .

Related

How to refer another label in Dockerfile?

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.

Retrieving a CI variable from Gitlab project and use it within Dockerfile

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

define a locale environment variable in Dockerfile

Is there any way to create local variables in Dockerfile that only available during the build process? What I can see is if I define a variable with the ENV keyword, then it will be available later in the image as an exported environment variable. But I would like to have a "technical" variable with build scope only.
I would like to avoid repetition in my Doclerfile so I would like to have a variable available only from the Dockerfile:
ENV MY_JAR=myJar.jar
COPY bin/$MY_JAR $ORACLE_HOME/user_projects/domains/$DOMAIN_NAME/lib/
COPY bin/$MY_JAR $ORACLE_HOME/wlserver/server/lib/mbeantypes/
But the MY_JAR variable appears in the container. I do not need it there. It just confuses users. Can I do this somehow?
Use ARG instead of ENV
ARG MY_JAR=myJar.jar # ARG is only available during the build of a Docker image
COPY bin/$MY_JAR $ORACLE_HOME/user_projects/domains/$DOMAIN_NAME/lib/
COPY bin/$MY_JAR $ORACLE_HOME/wlserver/server/lib/mbeantypes/
see also ARG or ENV, which one to use in this case?
You can use the --build-arg parameter to pass environment variables that lives just during docker building process.
So your docker build command will look something like this
docker build --build-arg HTTP_PROXY=http://10.20.30.2:1234 -t sample:v1 .
Where HTTP_PROXY is just available during the build process.

How to build Docker images with the commit's hash

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

Inject AWS Codebuild Environment Variables into Dockerfile

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

Resources