How to use a --build-arg parameter before FROM in a Dockerfile?

0 votes

I'm working on AWS ECR for hosting my project images. I'm using the FROM parameter to dynamically set the Dockerfile according to --build-arg input in the CLI.

$ docker build --build-args region=eu-north-1 .

// Dockerfile
FROM aws.ecr.huge.url.${region}/repo:php-apache
WORKDIR /var/www
RUN echo "@@@"

Here I want to replace the ${region} but that never happens and an error show up. But, when I run echo ${region} it works leading me to believe the problem is in FROM instruction

Can someone please help??

Apr 12, 2018 in Docker by Atul
• 10,240 points

1 answer to this question.

0 votes

In the earlier docker version this wasn't possible, but now after the introdiction of multi-stage builds, the user can provide an argument before FROM. SOmething like this:

# default to eu-north-1
ARG region=eu-north-1
FROM aws.ecr.huge.url.${region}/repo:php-apache

Also, the arguments are namespaced. Therefore, when you define them before a FROM line, they're available to FROM until the end of stage. To use args in multiple namespaces, you'll have to define it every time.

answered Apr 12, 2018 by ajs3033
• 7,280 points

Related Questions In Docker

0 votes
1 answer

How to add a comment in a Dockerfile?

Here is what you have to do. Try ...READ MORE

answered Nov 13, 2018 in Docker by Damon Salvatore
• 5,540 points
+1 vote
0 answers

How can I map a docker location to my Jenkins workspace so that I can use the files in my Jenkins

Hey guys, I was trying to execute few ...READ MORE

Feb 25 in Docker by dineshkumar
• 130 points
+4 votes
4 answers

How To Access a Service on Host From a Docker Container?

Adding to kalgi's answer, You can also ...READ MORE

answered Oct 16, 2018 in Docker by lina
• 8,200 points

edited Oct 16, 2018 by lina 11,762 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Unable to switch filesystem to overlay in Docker in Ubuntu.

From the official docs: To configure Docker ...READ MORE

answered Jul 18, 2018 in Docker by ajs3033
• 7,280 points
0 votes
1 answer

How do I know which datastore indexes are in error?

There was no public API for managing ...READ MORE

answered Jul 27, 2018 in Docker by ajs3033
• 7,280 points