I'm currently using AWS ECR to host my project images and I need the parameter FROM to be dynamic at Dockerfile according to --build-arg sent from the command line. Example:
$ docker build --build-args region=us-east-1 .
// Dockerfile
FROM aws.ecr.huge.url.${region}/repo:php-apache
WORKDIR /var/www
RUN echo "@@@"
${region} never gets replaced and I get an error saying the image doesn't exist.
If I RUN echo ${region} it works, the problem seems to be with FROM instruction.
Is there any way to achieve that?
Updated answer:
With the introduction of multi-stage builds, docker added the ability to provide an ARG
before the FROM
line. This will look like:
# default to us-east-1
ARG region=us-east-1
FROM aws.ecr.huge.url.${region}/repo:php-apache
Note that ARG
's are namespaced. When defined before a FROM
line, they are available to the FROM
lines. And within a stage, they are available until the end of that stage. If you need to use an ARG
in multiple namespaces, you can define it multiple times.
Original answer from before the introduction of multi-stage builds:
This is not supported as you've found, and as documented in the variable expansion limitations:
Environment variables are supported by the following list of instructions in the Dockerfile:
- ADD
- COPY
- ENV
- EXPOSE
- LABEL
- USER
- WORKDIR
- VOLUME
- STOPSIGNAL
as well as:
- ONBUILD (when combined with one of the supported instructions above)
To workaround this, I suspect you'll need to generate your Dockerfile with an external process first, e.g. with a sed "s/__region__/$region/" <Dockerfile.in >Dockerfile
command.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With