docker / docker/cli

ARG vs ENV Precedence

Open
#2,892 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

area/builder kind/bug
Dominant language
Go
Stars
6.1k
Forks
2.2k
Avg merge
1d 15h
Merged PRs (30d)
43

Description

Description
I apologize if this is the wrong place to post about this issue. Please point me to the right place if this is so.

When the base image has an environment variable and the same environment variable is being passed as a build argument but with a different value, the precedence order isn't consistent. On AWS linux, the image environment variable takes precedence. On my local Mac, the build argument takes precedence. I think the Mac behavior is the correct one since the build argument is more recent. But they should be consistent whatever the correct choice is.

Describe the results you expected:
I expected both to use the build argument.

Steps to reproduce the issue:

  1. Build a base image with any environment variable.
    docker build -t base_image . -f base.Dockerfile
  2. Use the base image to build a new image and pass it a build argument with a different value.
    docker build -t arg_image --build-arg VAR=val2 . -f arg.Dockerfile

Files

base.Dockerfile

FROM python:3.7.9
ENV VAR=val1

arg.Dockerfile

FROM base_image
ARG VAR
RUN $VAR

Linux

Output of docker version:

Client: Docker Engine - Community
 Version:           20.10.1
 API version:       1.40
 Go version:        go1.13.15
 Git commit:        831ebea
 Built:             Tue Dec 15 04:34:58 2020
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          19.03.13
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       4484c46d9d
  Built:            Wed Sep 16 17:01:20 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.3
  GitCommit:        269548fa27e0089a8b8278fc4fc781d7f65a939b
 runc:
  Version:          1.0.0-rc92
  GitCommit:        ff819c7e9184c13b7c2607fe6c30ae19403a7aff
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

Output of docker info:

Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.5.0-docker)

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 295
 Server Version: 19.03.13
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 269548fa27e0089a8b8278fc4fc781d7f65a939b
 runc version: ff819c7e9184c13b7c2607fe6c30ae19403a7aff
 init version: fec3683
 Security Options:
  apparmor
  seccomp
   Profile: default
 Kernel Version: 5.4.0-1030-aws
 Operating System: Ubuntu 20.04.1 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 2
 Total Memory: 7.698GiB
 Name: ip-10-1-6-74
 ID: TQCU:3JXP:WGDF:KATD:3FAS:MZIY:IXAL:P3D6:BMD5:K3VV:CYPX:P4KT
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

WARNING: No swap limit support

Output from steps

$ docker build -t base_image . -f base.Dockerfile 
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM python:3.7.9
 ---> 7fefbebd95b5
Step 2/2 : ENV VAR=val1
 ---> Using cache
 ---> 802e36d3b625
Successfully built 802e36d3b625
Successfully tagged base_image:latest

$ docker build -t arg_image --build-arg VAR=val2 . -f arg.Dockerfile
Sending build context to Docker daemon  3.072kB
Step 1/3 : FROM base_image
 ---> 802e36d3b625
Step 2/3 : ARG VAR
 ---> Running in d0d13d54d20d
Removing intermediate container d0d13d54d20d
 ---> 1b00b76e968a
Step 3/3 : RUN $VAR
 ---> Running in 03e5e4bb6421
/bin/sh: 1: val1: not found
The command '/bin/sh -c $VAR' returned a non-zero code: 127

NOTE: it says val1: not found

Mac

Output of docker version:

Client: Docker Engine - Community
 Cloud integration: 1.0.4
 Version:           20.10.0
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        7287ab3
 Built:             Tue Dec  8 18:55:43 2020
 OS/Arch:           darwin/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.0
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       eeddea2
  Built:            Tue Dec  8 18:58:04 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          v1.4.3
  GitCommit:        269548fa27e0089a8b8278fc4fc781d7f65a939b
 runc:
  Version:          1.0.0-rc92
  GitCommit:        ff819c7e9184c13b7c2607fe6c30ae19403a7aff
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Output of docker info:

Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.4.2-docker)
  scan: Docker Scan (Docker Inc., v0.5.0)

Server:
 Containers: 4
  Running: 4
  Paused: 0
  Stopped: 0
 Images: 56
 Server Version: 20.10.0
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runtime.v1.linux runc io.containerd.runc.v2
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 269548fa27e0089a8b8278fc4fc781d7f65a939b
 runc version: ff819c7e9184c13b7c2607fe6c30ae19403a7aff
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 4.19.121-linuxkit
 Operating System: Docker Desktop
 OSType: linux
 Architecture: x86_64
 CPUs: 4
 Total Memory: 5.809GiB
 Name: docker-desktop
 ID: JND3:6LEO:X5NW:QRCE:TQKP:P62X:K6FZ:7V2O:3SW6:ON5E:ZLMP:PNSN
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 HTTP Proxy: gateway.docker.internal:3128
 HTTPS Proxy: gateway.docker.internal:3129
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false
 Product License: Community Engine

Output from steps

$ docker build -t base_image . -f base.Dockerfile 
[+] Building 3.4s (5/5) FINISHED                                                                                                                                                              
 => [internal] load build definition from base.Dockerfile                                                                                                                                0.1s
 => => transferring dockerfile: 78B                                                                                                                                                      0.0s
 => [internal] load .dockerignore                                                                                                                                                        0.1s
 => => transferring context: 2B                                                                                                                                                          0.0s
 => [internal] load metadata for docker.io/library/python:3.7.9                                                                                                                          3.1s
 => CACHED [1/1] FROM docker.io/library/python:3.7.9@sha256:91fc2946b9275bb838a468a71e26a96eddd6c31bcf8178cf52108b8cac17240e                                                             0.0s
 => => resolve docker.io/library/python:3.7.9@sha256:91fc2946b9275bb838a468a71e26a96eddd6c31bcf8178cf52108b8cac17240e                                                                    0.0s
 => exporting to image                                                                                                                                                                   0.0s
 => => exporting layers                                                                                                                                                                  0.0s
 => => writing image sha256:bc676b1fca1432f5b26bab380ea7b8372411b9aae48bafbff32c4a0f83e117f7                                                                                             0.0s
 => => naming to docker.io/library/base_image


$ docker build -t arg_image --build-arg VAR=val2 . -f arg.Dockerfile
[+] Building 0.4s (5/5) FINISHED                                                                                                                                                              
 => [internal] load build definition from arg.Dockerfile                                                                                                                                 0.0s
 => => transferring dockerfile: 40B                                                                                                                                                      0.0s
 => [internal] load .dockerignore                                                                                                                                                        0.0s
 => => transferring context: 2B                                                                                                                                                          0.0s
 => [internal] load metadata for docker.io/library/base_image:latest                                                                                                                     0.0s
 => CACHED [1/2] FROM docker.io/library/base_image                                                                                                                                       0.0s
 => ERROR [2/2] RUN val2                                                                                                                                                                 0.3s
------                                                                                                                                                                                        
 > [2/2] RUN val2:
#5 0.277 /bin/sh: 1: val2: not found
------
executor failed running [/bin/sh -c $VAR]: exit code: 127

NOTE: it says val2: not found

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the two Dockerfiles and build commands from the issue with the Docker Engine versions and builder modes shown for Linux and Mac. Compare how ARG and inherited ENV are resolved, then verify that the chosen precedence is consistent across the reported environments and that the reproduction no longer differs.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, go
Domain
build-system, cli
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.