In my Dockerfile I have a typical setup for an image tag but I’m trying to make a multi-stage build:
FROM docker.io/library/alpine as first-stage
RUN echo "test"
ARG VERSION="latest"
FROM docker.io/library/hello-world:${VERSION} as second-stage
but when I try to build it with a command like:
docker build \
--tag=img-name-dev:latest \
.
(running from the directory that the Dockerfile lives in)
I get in my real code:
failed to solve with frontend dockerfile.v0: failed to create LLB definition: failed to parse stage name "link.to.my/image:": invalid reference format
or in the minimal reproducible example:
Step 4/4 : FROM docker.io/library/hello-world:${VERSION} as second-stage
invalid reference format
which indicates to me that the VERSION is empty, rather than being filled with latest. If I remove the ${VERSION} and replace it simply with latest it works (so it’s not a permissions issue pulling the image).
What am I doing wrong?
Docker version:
Client: Docker Engine - Community
Version: 20.10.23
API version: 1.41
Go version: go1.18.10
Git commit: 7155243
Built: Thu Jan 19 17:34:12 2023
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.23
API version: 1.41 (minimum version 1.12)
Go version: go1.18.10
Git commit: 6051f14
Built: Thu Jan 19 17:32:03 2023
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.15
GitCommit: 5b842e528e99d4d4c1686467debf2bd4b88ecd86
runc:
Version: 1.1.4
GitCommit: v1.1.4-0-g5fd4c4d
docker-init:
Version: 0.19.0
GitCommit: de40ad0
Docker Compose version v2.13.0
>Solution :
Any ARG that is used within a FROM command must occur before the first FROM statement. So move your ARG to the very top of your file — above the additional first stage that you mentioned in the comments. See the Dockerfile reference.