Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] --env-file option is ignored from docker compose --env-file=file.env up #10838

Closed
astrolemonade opened this issue Jul 21, 2023 · 1 comment
Assignees

Comments

@astrolemonade
Copy link

astrolemonade commented Jul 21, 2023

Description

Hey!
docker compose --env-file=file.env up is ignoring the --env-file option.

Steps To Reproduce

  1. have a docker-compose.yaml file that should use some environment variables from an environment variables file.
    example:
version: '3.9'
services:
  node:
    image: node:18
    container_name: live_dev
    # it was specifically chosen to hang the container so that we can inspect it
    command: tail -F anything
  1. have an file.env file:
    example:
MY_VAR=my_var
  1. have the following directory structure:
[~/live_dev] $ tree -a
.
├── docker-compose.yaml
└── file.env

1 directory, 2 files
  1. run the docker compose --env-file=file.env up
  2. enter the container via docker exec -it live_dev /bin/bash
  3. run printenv to see if MY_VAR exists
[~/live_dev] $ docker exec -it live_dev /bin/bash
root@741ec06639ca:/# printenv
HOSTNAME=741ec06639ca
YARN_VERSION=1.22.19
PWD=/
HOME=/root
TERM=xterm
SHLVL=1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
NODE_VERSION=18.17.0
_=/usr/bin/printenv
  1. notice that MY_VAR doesn't exist

Compose Version

Docker Compose version v2.18.1

Docker Environment

Client: Docker Engine - Community
 Version:    24.0.1
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.11.1
    Path:     /usr/lib/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.18.1
    Path:     /usr/lib/docker/cli-plugins/docker-compose

Server:
 Containers: 4
  Running: 4
  Paused: 0
  Stopped: 0
 Images: 14
 Server Version: 24.0.1
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: systemd
 Cgroup Version: 2
 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.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 0cae528dd6cb557f7201036e9f43420650207b58
 runc version: 5fd4c4d144137e991c4acebb2146ab1483a97925
 init version: 0b44d36
 Security Options:
  apparmor
  seccomp
   Profile: builtin
  cgroupns
 Kernel Version: 6.3.12-241.current
 Operating System: Solus 4.4 Harmony
 OSType: linux
 Architecture: x86_64
 CPUs: 12
 Total Memory: 15.5GiB
 Name: catalin
 ID: QGPZ:6HOM:HUOC:EWS6:42UO:UZ47:5PEO:NKUW:VUDH:LQQ7:GI5X:RAY6
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

Anything else?

Adding env_file to the docker-compose.yaml and running docker compose up loads the file and makes the variable present.

version: '3.9'
services:
  node:
    image: node:18
    env_file:
      - file.env
    container_name: live_dev
    # it was specifically chosen to hang the container so that we can inspect it
    command: tail -F anything

Did some investigation and it seems it was removed from build, up and down some time ago(maybe in the Python version and got ported to the Go version, I am not sure). #6800 and also has something to do with #10650
I don't know if this is supposed to happen.

@milas
Copy link
Contributor

milas commented Aug 21, 2023

The environment variable handling is a bit confusing; there's a lot of backwards compatibility that's resulted in several similar features.

If you want to pass environment variables to a service's container, you have two options:

<svc>.environment

services:
  opt1:
     image: alpine
     command: ["env"]
     environment:
        KEY=value

<svc>.env_file

services:
  opt2:
     image: alpine
     command: ["env"]
     env_file: ["src.env"]

svc.env

KEY=value

--env-file

The --env-file command-line argument overrides Compose behavior that automatically looks for a file named .env in the current directory. The .env file (or file at path specified by --env-file) is merged with the system environment to perform variable resolution inside the Compose YAML. That is, this let's you do things like image: ${IMAGE} in your YAML, and then have it substituted by putting IMAGE=alpine in a .env file (or running IMAGE=alpine docker compose ...).

Unfortunately, this is named the same as the env_file YAML field, both of which are taken from Docker CLI's --env-file option, which is confusing, as the YAML field is for passing through to containers, while the CLI flag is for substitution within YAML.


I'm closing this issue, but cc @aevesdocker on our docs team, we improved this recently around precedence, syntax & mechanisms, but we might need an extra call-out for this distinction.

@milas milas closed this as not planned Won't fix, can't repro, duplicate, stale Aug 21, 2023
@milas milas self-assigned this Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants