'Docker build ARG always empty string
I have a dockerfile here:
FROM golang:1.13-alpine as build
ARG DIR=somevalue
RUN echo $DIR
Output is
Sending build context to Docker daemon 57.37MB
Step 1/3 : FROM golang:1.13-alpine as build
---> 2e384b27f926
Step 2/3 : ARG DIR=somevalue
---> Running in 3cd3457a795f
Removing intermediate container 3cd3457a795f
---> ecfa2f50c4fa
Step 3/3 : RUN echo $DIR
---> Running in aab4e31e0e14
Removing intermediate container aab4e31e0e14
---> 5351cb77c245
Successfully built 5351cb77c245
DIR is always empty when I try DIR to another name example DIR1 it is empty too. Docker version is here
Client:
Version: 18.09.8
API version: 1.39
Go version: go1.11
Git commit: 2c0a67b
Built: Fri Sep 6 02:50:44 2019
OS/Arch: linux/amd64
Experimental: false
Server:
Engine:
Version: 18.09.6
API version: 1.39 (minimum version 1.12)
Go version: go1.11
Git commit: 1d8275b
Built: Fri Sep 6 02:51:05 2019
OS/Arch: linux/amd64
Experimental: false
However, when I try to another machine, it is right, it will echo some value. Who can give me some tips to find the problem about the wrong machine? thanks.
Solution 1:[1]
This is obviously not the problem in your example, but I got this error when declaring an ARG before the FROM. Moving the ARG I needed below FROM resolved the problem.
Solution 2:[2]
Can you try running by passing the --no-cache option, it is working as expected for me
??? docker build . --no-cache
Sending build context to Docker daemon 194.7MB
Step 1/3 : FROM golang:1.13-alpine as build
---> 2e384b27f926
Step 2/3 : ARG DIR=somevalue
---> Running in c53bbb64dda1
Removing intermediate container c53bbb64dda1
---> b76b21aca433
Step 3/3 : RUN echo $DIR
---> Running in 6c49d289e258
somevalue
Removing intermediate container 6c49d289e258
---> d51e5579f1cb
Successfully built d51e5579f1cb
However, if it has built the same image once, it will not output anything as it has the layer cached:
??? docker build .
Sending build context to Docker daemon 194.7MB
Step 1/3 : FROM golang:1.13-alpine as build
---> 2e384b27f926
Step 2/3 : ARG DIR=somevalue
---> Using cache
---> b76b21aca433
Step 3/3 : RUN echo $DIR
---> Using cache
---> d51e5579f1cb
Successfully built d51e5579f1cb
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jim Hunziker |
| Solution 2 | Abhishek Jaisingh |
