Inject AWS Codebuild Environment Variables into Dockerfile - docker

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

Related

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

How to get bitbucket repository variables in my dockerfile and COPY them to my docker image like COPY bitbucket_variables /app

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/

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

Docker build argument

How does one pass arguments into a dockerfile?
Lets say I have the dockerfile:
FROM ubuntu:14.04
MAINTAINER Karl Morrison
sudo do-something-here myVarHere
I would want to build the file as so for example:
docker build basickarl/my-image-example /directory/of/my/dockerfile "my string to be passed to myVarHere here!"
Docker has ARG that you can use here
FROM ubuntu:14.04
MAINTAINER Karl Morrison
ARG myVarHere
RUN do-something-here myVarHere
And then build using --build-arg:
docker build --build-arg myVarHere=value
We've had a similar requirement and came up with a relatively simple script that does exactly that:
We create a file called dockerfile_template in which we use variables just like you describe. The script takes that file, performs string substitutions and copies it to dockerfile (no _template) before calling docker build dockerfile.
Works pretty good. Also very extensible for future requirements.
Update:
Scrap that. Use build-arg (here)

Resources