11

Problem statement: I need to pull docker(projectA and projectB) from two different urls based on the arg provided.

ARG url=docker-local.artifactory.com/projectA #By default its for A. RUN echo ${url} FROM $url 

Ideal Solution:

docker build -t hello . should build docker of project A

docker build --build-arg url="docker-local.artifactory.com/projectB" -t hello . should build docker of project B.

Current Issue:

"base name ($url) should not be blank" 
1
  • 1
    You should include a complete Dockerfile, because it's invalid to have a run step before the first from step. I'm assuming there's another from step, and that makes a difference in how this is answered. Commented Mar 5, 2021 at 15:33

3 Answers 3

19

Using the docs for reference, if you want to use ARG in FROM, ARG must be the first line in your Dockerfile (as reminded by koby.el's answer) and FROM must come immediately after. See this section for details.

This minimal Dockerfile works:

ARG url=docker-local.artifactory.com/projectA FROM $url 

Built using this command with a build arg:

docker build -t from --build-arg url=alpine:3.9 . [+] Building 0.1s (5/5) FINISHED => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 116B 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/alpine:3.9 0.0s => CACHED [1/1] FROM docker.io/library/alpine:3.9 0.0s => exporting to image 0.0s => => exporting layers 0.0s => => writing image sha256:352159a49b502edb1c17a3ad142b320155bd541830000c02093b79f4058a3bd1 0.0s => => naming to docker.io/library/from 

The docs also show an example if you want to re-use the ARG value after the first FROM command:

ARG url=docker-local.artifactory.com/projectA FROM $url ARG url RUN echo $url 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for help. But its strange that it works on my local. But when i run it on my git pipeline RUN echo $url Output is "url".
3

Jumping into an old thread here...

But after wasting a few hours on this one I think its worth mentioning - if you want to pass the image for the FROM part, the ARG MUST be the first line in the Dockerfile!

Otherwise, you'll get the following error:

ERROR: failed to solve: base name (${BASE_IMAGE}) should not be blank

Comments

2

Using the following build file,

ARG VERSION=busybox:latest FROM $VERSION ARG VERSION RUN echo $VERSION 

Running with the default value

docker build -t test . Sending build context to Docker daemon 16.38kB Step 1/4 : ARG VERSION=busybox:latest Step 2/4 : FROM $VERSION latest: Pulling from library/busybox 

Running with value changed during build

docker build -t test --build-arg VERSION="ubuntu:20.04" . Sending build context to Docker daemon 16.38kB Step 1/4 : ARG VERSION=busybox:latest Step 2/4 : FROM $VERSION 20.04: Pulling from library/ubuntu 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.