compose icon indicating copy to clipboard operation
compose copied to clipboard

Docker Compose does not load the .env from project directory

Open michaelkrieger opened this issue 4 years ago • 4 comments

Description

When using $COMPOSE_FILE, project directory is not obeyed for the loading of .env files. This behaviour differs from docker-compose 1.29.2 which correctly found the appropriate .env file.

Test case, with DOMAINNAME set in the .env file and used in the docker-compose file. All files are in /srv folder.

# docker compose version
Docker Compose version v2.5.0

# echo $COMPOSE_FILE
/srv/docker-compose.yml
# ls -1 /srv/.env
/srv/.env

# cd /srv
# docker compose stop homer
 ⠿ Container homer  Stopped                                                                                          10.4s

# docker compose up -d homer
...

# cd /media
# docker compose stop homer
WARN[0000] The "DOMAINNAME" variable is not set. Defaulting to a blank string. 
 ⠿ Container homer  Stopped                                                  

Describe the results you received: DOMIANNAME variable was not set and inaccessible if started outside of the /srv folder, which tells me it is not loading the .env file from /srv either due to a bug in the .env file or its treatment of the project directory.

Describe the results you expected: I would expect docker compose to identify the project directory (which it states in docker compose help as "default: the path of the Compose file")

Additional information you deem important (e.g. issue happens only occasionally): Happens every time.

Output of docker compose version:

Docker Compose version v2.5.0

Output of docker info:

Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Docker Buildx (Docker Inc., v0.8.2-docker)
  compose: Docker Compose (Docker Inc., v2.5.0)
  scan: Docker Scan (Docker Inc., v0.17.0)

Server:
 Containers: 45
  Running: 35
  Paused: 0
  Stopped: 10
 Images: 49
 Server Version: 20.10.16
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  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.runtime.v1.linux runc io.containerd.runc.v2
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 212e8b6fa2f44b9c21b2798135fc6fb7c53efc16
 runc version: v1.1.1-0-g52de29d
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: default
  cgroupns
 Kernel Version: 5.15.0-0.bpo.3-amd64
 Operating System: Debian GNU/Linux 11 (bullseye)
 OSType: linux
 Architecture: x86_64
 CPUs: 4
 Total Memory: 7.639GiB
 Name: breach
 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

Additional environment details:

michaelkrieger avatar May 26 '22 17:05 michaelkrieger

@michaelkrieger

Thanks for reporting issue,

I've tried to replicate on my end but everything is working correctly here.

~/workspace/test-compose-9503/compose.yml

services:
  app:
    image: alpine
    command: echo "${VAR}"

~/workspace/test-compose-9503/.env

VAR="HelloWorld!"

Within directory

> cd ~/workspace/test-compose-9503 && docker compose up
< Container test-compose-9503-app-1  Created
< Attaching to test-compose-9503-app-1
< test-compose-9503-app-1  | HelloWorld!
< test-compose-9503-app-1 exited with code 0

Outside directory

> cd /tmp && docker compose -f ~/workspace/test-compose-9503/compose.yml up
< Container test-compose-9503-app-1  Created
< Attaching to test-compose-9503-app-1
< test-compose-9503-app-1  | HelloWorld!
< test-compose-9503-app-1 exited with code 0

Am I missing something ?

maxcleme avatar May 31 '22 07:05 maxcleme

Below is my test case. Note that I don't even need to start the container to trigger the error as it happens on something as simple as a PS command.

Current Bash shell environment

root@breach:/# env
SHELL=/bin/bash
LANGUAGE=en_CA:en
PWD=/
LOGNAME=root
HOME=/root
LANG=en_CA.UTF-8
TERM=linux
USER=root
COMPOSE_FILE=/test/docker-compose.yml
SHLVL=1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MAIL=/var/mail/root
OLDPWD=/test
_=/usr/bin/env

My test files

root@breach:/test# ls -la

total 16
drwxr-xr-x  2 root root 4096 May 31 10:45 .
drwxr-xr-x 23 root root 4096 May 31 10:29 ..
-rw-r--r--  1 root root  240 May 31 10:44 docker-compose.yml
-rw-------  1 root root   26 May 31 10:45 .env

root@breach:/test# cat .env

DOMAINNAME=testing123.com

root@breach:/test# cat docker-compose.yml

---
version: "3.8"
services:
  pihole:
    container_name: pihole
    image: pihole/pihole:latest
    hostname: pihole
    environment:
      - DOMAIN_NAME=${DOMAINNAME}
      - VIRTUAL_HOST=pihole.${DOMAINNAME}
    restart: unless-stopped

A working test in the working folder /test

root@breach:/test# docker compose ps
NAME                COMMAND             SERVICE             STATUS              PORTS

The same command outside of that folder

root@breach:/test# cd /
root@breach:/# docker compose ps
WARN[0000] The "DOMAINNAME" variable is not set. Defaulting to a blank string. 
WARN[0000] The "DOMAINNAME" variable is not set. Defaulting to a blank string. 
NAME                COMMAND             SERVICE             STATUS              PORTS

Meanwhile the Python docker-compose v1.29.2 did not throw those errors (maybe silently ignored them or maybe didn't have the same problem):

root@breach:/# cd /test
root@breach:/test# docker-compose ps 
 Name       Command      State     Ports
----------------------------------------
root@breach:/# cd /
root@breach:/# docker-compose ps 
 Name       Command      State     Ports
----------------------------------------
root@breach:/# docker version
Client: Docker Engine - Community
 Version:           20.10.16
 API version:       1.41
 Go version:        go1.17.10
 Git commit:        aa7e414
 Built:             Thu May 12 09:17:39 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.16
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.17.10
  Git commit:       f756502
  Built:            Thu May 12 09:15:42 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.4
  GitCommit:        212e8b6fa2f44b9c21b2798135fc6fb7c53efc16
 runc:
  Version:          1.1.1
  GitCommit:        v1.1.1-0-g52de29d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
root@breach:/# docker compose version
Docker Compose version v2.5.0

michaelkrieger avatar May 31 '22 14:05 michaelkrieger

Using something similar to @maxcleme 's test case

The test files

root@breach:/test# ls -la
total 16
drwxr-xr-x  2 root root 4096 May 31 10:57 .
drwxr-xr-x 23 root root 4096 May 31 10:29 ..
-rw-r--r--  1 root root  105 May 31 10:57 docker-compose.yml
-rw-------  1 root root   19 May 31 10:57 .env

root@breach:/test# cat .env

VAR=testing123.com

root@breach:/test# cat docker-compose.yml

---
version: "3.8"
services:
  app:
    container_name: app
    image: alpine
    command: echo "${VAR}"

Running in the working directory...

root@breach:/test# docker compose ps
NAME                COMMAND             SERVICE             STATUS              PORTS
root@breach:/test# docker compose up app
[+] Running 1/1
 ⠿ Container app  Recreated                                                                                           0.1s
Attaching to app
app  | testing123.com
app exited with code 0

Running outside of the working directory...

root@breach:/test# cd /
root@breach:/# docker compose ps
WARN[0000] The "VAR" variable is not set. Defaulting to a blank string. 
NAME                COMMAND             SERVICE             STATUS              PORTS
root@breach:/# docker compose up app
WARN[0000] The "VAR" variable is not set. Defaulting to a blank string. 
[+] Running 1/1
 ⠿ Container app  Recreated                                                                                           0.1s
Attaching to app
app  | 
app exited with code 0

Meanwhile, the environment has COMPOSE_FILE set correctly

root@breach:/# env
SHELL=/bin/bash
LANGUAGE=en_CA:en
PWD=/
LOGNAME=root
HOME=/root
LANG=en_CA.UTF-8
TERM=linux
USER=root
COMPOSE_FILE=/test/docker-compose.yml
SHLVL=1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MAIL=/var/mail/root
OLDPWD=/test
_=/usr/bin/env

michaelkrieger avatar May 31 '22 15:05 michaelkrieger

@michaelkrieger Thanks for the report, it does look like the COMPOSE_FILE behavior was incorrect. This has been fixed with Compose v2.6.0, which is available with Docker Desktop v4.9.0+.

From a sample similar to the ones from this thread:

Compose v2.5.0

$ COMPOSE_FILE=./dcenv/compose.yaml ./compose-2.5.0 run app
WARN[0000] The "VAR" variable is not set. Defaulting to a blank string.

Compose v2.6.0

$ COMPOSE_FILE=./dcenv/compose.yaml ./compose-2.6.0 run app
HelloWorld!

Feel free to comment & re-open if you continue to see issues after upgrading. Thanks for filing an issue!

milas avatar Jun 16 '22 20:06 milas