`--env-file` doesn't support inline comments
Description
Currently docker run --env-file someenvfile.env sometag doesn't remove inline comments -- so if something like SOMEVAR=somevalue # comment is written, SOMEVAR is set to somevalue # comment instead of somevalue.
Similar issue for Docker Compose was fixed about 1.5 years ago.
Reproduce
echo 'SOMEVAR=somevalue # comment' > test.envdocker run --env-file test.env bash bash -c 'echo $''SOMEVAR'
Expected behavior
somevalue is expected but somevalue # comment is actual.
docker version
Client: Docker Engine - Community
Cloud integration: v1.0.35
Version: 24.0.2
API version: 1.43
Go version: go1.20.4
Git commit: cb74dfc
Built: Thu May 25 21:52:17 2023
OS/Arch: linux/amd64
Context: default
Server: Docker Desktop
Engine:
Version: 24.0.2-38-g8e70a1b23e
API version: 1.43 (minimum version 1.12)
Go version: go1.20.4
Git commit: 8e70a1b23e965d86ec8c2feb77605196ae124630
Built: Fri Jun 2 15:58:50 2023
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.21
GitCommit: 3dce8eb055cbb6872793272b4f20ed16117344f8
runc:
Version: 1.1.7
GitCommit: v1.1.7-0-g860f061
docker-init:
Version: 0.19.0
GitCommit: de40ad0
docker info
Client: Docker Engine - Community
Version: 24.0.2
Context: default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc.)
Version: v0.11.0
Path: /usr/local/lib/docker/cli-plugins/docker-buildx
compose: Docker Compose (Docker Inc.)
Version: v2.19.1
Path: /usr/local/lib/docker/cli-plugins/docker-compose
dev: Docker Dev Environments (Docker Inc.)
Version: v0.1.0
Path: /usr/local/lib/docker/cli-plugins/docker-dev
extension: Manages Docker extensions (Docker Inc.)
Version: v0.2.20
Path: /usr/local/lib/docker/cli-plugins/docker-extension
init: Creates Docker-related starter files for your project (Docker Inc.)
Version: v0.1.0-beta.6
Path: /usr/local/lib/docker/cli-plugins/docker-init
sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc.)
Version: 0.6.0
Path: /usr/local/lib/docker/cli-plugins/docker-sbom
scan: Docker Scan (Docker Inc.)
Version: v0.26.0
Path: /usr/local/lib/docker/cli-plugins/docker-scan
scout: Command line tool for Docker Scout (Docker Inc.)
Version: 0.16.1
Path: /usr/local/lib/docker/cli-plugins/docker-scout
Server:
Containers: 17
Running: 6
Paused: 0
Stopped: 11
Images: 40
Server Version: 24.0.2-38-g8e70a1b23e
Storage Driver: stargz
driver-type: io.containerd.snapshotter.v1
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: runc io.containerd.runc.v2
Default Runtime: runc
Init Binary: docker-init
containerd version: 3dce8eb055cbb6872793272b4f20ed16117344f8
runc version: v1.1.7-0-g860f061
init version: de40ad0
Security Options:
seccomp
Profile: builtin
Kernel Version: 5.15.90.1-microsoft-standard-WSL2
Operating System: Docker Desktop
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 7.235GiB
Name: docker-desktop
ID: 4359e80c-f658-4395-9bc1-c38e42346fb7
Docker Root Dir: /var/lib/docker
Debug Mode: false
HTTP Proxy: http.docker.internal:3128
HTTPS Proxy: http.docker.internal:3128
No Proxy: hubproxy.docker.internal
Experimental: false
Insecure Registries:
hubproxy.docker.internal:5555
127.0.0.0/8
Live Restore Enabled: false
WARNING: No blkio throttle.read_bps_device support
WARNING: No blkio throttle.write_bps_device support
WARNING: No blkio throttle.read_iops_device support
WARNING: No blkio throttle.write_iops_device support
Additional Info
No response
This is by design (but can be confusing, because compose also implemented an --env-file option, which is not the equivalent.
The compose --env-file option specifies the path of the .env file in compose, which is a compose feature (and also allows, e.t.c export FOO=bar.
The --env-file on docker run is strictly a list of key=value pairs, and only allows line comments, not inline comments;
https://github.com/docker/cli/blob/dc2eb3bf7c835ff820e0a38de123a846b9e887e9/docs/reference/commandline/run.md#-set-environment-variables--e---env---env-file
Changing that would be a breaking change, so not likely something we could do
You can also load the environment variables from a file. This file should use the syntax
=value (which sets the variable to the given value) or (which takes the value from the local environment), and # for comments.
It isn't really clear from this that line comments are supported but not inline... So maybe the documentation can be updated?
Can i work on This? @thaJeztah
Is this issue still open or was it fixed in #4464 ?
open bro
I would like to work on it @thaJeztah
Yes, looks like https://github.com/docker/cli/pull/4464 was closed before it was reviewed/merged.
We should probably use similar wording as we do for describing how comments are handled in the Dockerfile syntax; https://docs.docker.com/engine/reference/builder/
/cc @dvdksn
@desmond3th feel free to open a PR!