Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker ARG variables not working (always empty)

Why are the ARG variables in my dockerfile are always empty?

Command

docker build --rm --force-rm --no-cache -f ./Dockerfile

Dockerfile

ARG APP_NAME='ground-station'

FROM node:current AS build-node
WORKDIR /${APP_NAME}
RUN echo "APP_NAME=${APP_NAME}"

Output

Sending build context to Docker daemon  1.199MB
Step 1/4 : ARG APP_NAME='ground-station'
Step 2/4 : FROM node:current AS build-node
 ---> 6e72986b1b6e
Step 3/4 : WORKDIR /${APP_NAME}
 ---> Running in 39f12e36d4a1
Removing intermediate container 39f12e36d4a1
 ---> 93f5cdef6402
Step 4/4 : RUN echo "APP_NAME=${APP_NAME}"
 ---> Running in a18ac6f3bee8
APP_NAME=
Removing intermediate container a18ac6f3bee8
 ---> 746cea84bb8f
Successfully built 746cea84bb8f

On step 4, APP_NAME is always empty.

I searched for a solution but all I've found is this. I tried with --no-cache but it still doesn't work.


Output of docker version

Client: Docker Engine - Community
 Version:           20.10.5
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        55c4c88
 Built:             Tue Mar  2 20:17:52 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.5
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       363e9a8
  Built:            Tue Mar  2 20:15:47 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.4
  GitCommit:        05f951a3781f4f2c1911b05e61c160e9c30eaa8e
 runc:
  Version:          1.0.0-rc93
  GitCommit:        12644e614e25b05da6fd08a38ffa0cfe1903fdec
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
like image 608
Elie G. Avatar asked Jul 15 '26 00:07

Elie G.


1 Answers

ARG steps are scoped. Before the first FROM step, the ARG only applies to FROM steps. And within each FROM step, it only applies to the lines after that ARG step until the next FROM (in a multistage build).

To fix this, reorder your steps:

FROM node:current AS build-node
ARG APP_NAME='ground-station'
WORKDIR /${APP_NAME}
RUN echo "APP_NAME=${APP_NAME}"
like image 114
BMitch Avatar answered Jul 17 '26 16:07

BMitch