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

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

Related

docker env variable only local

I have a Dockerfile that sets environment variables that are common to all environments, whether dev, test, or production ones, but I have to set another environment variable that is only applicable to my development environment, so I can't set it in the Dockerfile because such file is managed by the version control, so the change would be deployed to all environments.
How can add an environment variable to a docker container only in my local development environment?
In case that the env variable can be specified when the image is being used, then just supplying the variable then makes more sense. For instance, if you are locally testing the image, by using the docker cli, you can set the variable with:
docker run -e KEY=VALUE $image
If you are using other tools to test the image, there are always other methods to set env keys.
If it's required for you to have set the variable at build time, you can specify built args inside the Dockerfile.
An example for that would be:
FROM someimage:v1
ARG DEV_ONLY_VAR
ENV KEY=$DEV_ONLY_VAR
Using this, you can specify the build arg DEV_ONLY_VAR in the build command by writing:
docker build --build-arg DEV_ONLY_VAR=VALUE .
Note, even without the ENV KEY=$DEV_ONLY_VAR line the build arg will be available like a env variable during build time, on other run steps.
More on build args here

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.

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

Dockerfile with an external arguments file

I have 2 Dockerfiles that have common arguments (ARG) that are passed to the actual commands (RUN) to build the images.
Is it possible to provide an external file with the arguments so that when I need to update one of them I don't need to touch both Dockerfiles?
An ARG is designed to be modified from the build command line, so you'd run:
docker build --build-arg VAR=value -t your_image .
That can be placed inside of a shell script to automate it and pass the same arg to each build.
You can also use a compose file, and the compose file may use environment variables or a .env file, to set variables used inside the compose file, e.g.
build:
context: ./your_app_dir
dockerfile: Dockerfile
args:
VAR: ${VALUE}
And the .env would contain:
VALUE=your_value
For more details on compose files, see the build syntax and also the environment file syntax.

How can the current build context directory be referenced inside of a Dockerfile?

I'm trying to set an environment variable to the current build directory inside of a Dockerfile. For example, I'm trying to do something like this:
ENV APP_SRC $BUILD_CONTEXT # Save the current host directory to an env variable
COPY . /$APP_SRC # Copy the app source code to a directory of the same name in the container
I know it sounds like a weird thing to do, but I need my directory name to be the same in the container as it is on my host machine. Is this possible?
With docker 1.9, you can pass build-time environment variable:
docker build --build-arg APP_SRC=$BUILD_CONTEXT -y tag .
$APP_SRC will then be valued like $BUILD_CONTEXT.
Note that this is not yet supported by docker compose: both issue 2111 and 2163 are asking for that feature.

Resources