[BUG] docker compose down --env does not translate ~/.env to .env file in home directory

Description

If I docker compose down with an env file as arguments using the tilde notation for the home directory as such:

username@hostname:~/some-folder-in-home$ docker compose -f docker-compose.yml --env-file=~/.env down
couldn't find env file: /home/username/some-folder-in-home/~/.env

It will print the error couldn't find env file: /home/username/some-folder-in-home/~/.env, which indicates that it doesn't translate the tilde symbol to the home directory.

  • what it should use: /home/username/.env
  • what it translates to instead: /home/username/some-folder-in-home/~/.env

Steps To Reproduce

1-2. Start your container with docker compose
3. docker compose it down with an env file that exists in ~/.env like this:

username@hostname:~/some-folder-in-home$ docker compose -f docker-compose.yml --env-file=~/.env down
  1. See error couldn't find env file: /home/username/some-folder-in-home/~/.env

Compose Version

docker compose version: Docker Compose version v2.40.0
docker-compose version: bash: docker-compose: command not found

Docker Environment

Client: Docker Engine - Community
 Version:    28.5.1
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.29.1
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.40.0
    Path:     /usr/libexec/docker/cli-plugins/docker-compose

Server:
 Containers: 7
  Running: 7
  Paused: 0
  Stopped: 0
 Images: 25
 Server Version: 27.5.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 splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 nvidia runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: b98a3aace656320842a23f4a392a33f46af97866
 runc version: v1.3.0-0-g4ca628d1
 init version: de40ad0
 Security Options:
  seccomp
   Profile: builtin
  cgroupns
 Kernel Version: 5.15.136-tegra
 Operating System: Ubuntu 22.04.4 LTS
 OSType: linux
 Architecture: aarch64
 CPUs: 6
 Total Memory: 7.442GiB
 Name: hostname
 ID: 3bfb5b23-6373-4e14-9bd5-8235431a397c
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

Anything else?

No response