Inject AWS Codebuild Environment Variables into Dockerfile
Asked Answered
G

2

7

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.

Gillenwater answered 13/6, 2019 at 19:38 Comment(0)
M
9

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}

Merras answered 13/6, 2019 at 19:44 Comment(2)
Just a side note on the buildspec.yml file itself, both ${ENV_VAR} and $ENV_VAR syntax are valid. Also, as i was struggling a bit on it, to pass multiples build-args to docker build command, you just need to rewrite the args with another variables. E.g : docker build --build-arg DOCKER_VAR_1=${CODEBUILD_VAR_1} --build-arg DOCKER_VAR_2=${CODEBUILD_VAR_2}. That will pass both variable to the DockerfileCamilla
@Camilla +1 for info on how to pass multiple build-argsBurgrave
C
1

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
Clermontferrand answered 19/4, 2022 at 2:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.