'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.



Solution 1:[1]

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}

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 abelgana
Solution 2 ekeyser