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] -p (aka --project-name) does not override the interpolated value of COMPOSE_PROJECT_NAME if COMPOSE_PROJECT_NAME is present in the environment or .env file #11734

Open
i-ky opened this issue Apr 16, 2024 · 0 comments

Comments

@i-ky
Copy link
Contributor

i-ky commented Apr 16, 2024

Description

When -p (aka --project-name) is explicitly passed to docker compose and COMPOSE_PROJECT_NAME is also present in the environment or .env file, then Docker Compose will use the value from --project-name as a name of the project, but would interpolate COMPOSE_PROJECT_NAME using the value from the environment or the .env file.

Steps To Reproduce

  1. Using the following compose.yaml:
services:
  db:
    image: postgres:${POSTGRES_VERSION_MAJOR}
    volumes:
    - db-data:/var/lib/postgresql/data

volumes:
  db-data:
    name: ${COMPOSE_PROJECT_NAME}_postgres_${POSTGRES_VERSION_MAJOR}_data
  1. And the following .env:
COMPOSE_PROJECT_NAME=foo
POSTGRES_VERSION_MAJOR=16
  1. Run docker compose config:
name: foo
services:
  db:
    image: postgres:16
    networks:
      default: null
    volumes:
      - type: volume
        source: db-data
        target: /var/lib/postgresql/data
        volume: {}
networks:
  default:
    name: foo_default
volumes:
  db-data:
    name: foo_postgres_16_data

Note that foo is used as the project name and as a prefix in volume's name:.
4. Run docker compose --project-name bar config:

name: bar
services:
  db:
    image: postgres:16
    networks:
      default: null
    volumes:
      - type: volume
        source: db-data
        target: /var/lib/postgresql/data
        volume: {}
networks:
  default:
    name: bar_default
volumes:
  db-data:
    name: foo_postgres_16_data

Notice that bar is used as the project name, but volume's name is still prefixed with foo - this is a bug.

Compose Version

$ docker compose version
Docker Compose version v2.26.1
$ docker-compose version
Command 'docker-compose' not found, but can be installed with:
sudo snap install docker          # version 24.0.5, or
sudo apt  install docker-compose  # version 1.29.2-1
See 'snap info docker' for additional versions.

Docker Environment

$ docker info
Client: Docker Engine - Community
 Version:    26.0.1
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.13.1
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.26.1
    Path:     /usr/libexec/docker/cli-plugins/docker-compose

Server:
 Containers: 6
  Running: 6
  Paused: 0
  Stopped: 0
 Images: 8
 Server Version: 26.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 splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: e377cd56a71523140ca6ae87e30244719194a521
 runc version: v1.1.12-0-g51d5e94
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: builtin
  cgroupns
 Kernel Version: 6.5.0-27-generic
 Operating System: Ubuntu 22.04.4 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 16
 Total Memory: 30.76GiB
 Name: i-ky-TUXEDO-Pulse-15-Gen1
 ID: 5f8d7612-5da9-4ce2-a156-9b2e1d694891
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

Anything else?

It is correct that the value of --project-name takes precedence over COMPOSE_PROJECT_NAME, see the docs here and here.

But it does not feel right that interpolation of COMPOSE_PROJECT_NAME follows the same logic as for a regular variable. Given that COMPOSE_PROJECT_NAME is a predefined variable with a special meaning, its interpolation should take the value of --project-name into account.

This is not directly related to a bug report, but I would like to share some background story for a better context.

I am working on a project using PostgreSQL. Docker Compose is used to run development environment. Docker volume is mounted into container to persist DB data between container restarts. The format of these data is dependent on the major version of PostgreSQL, container fails to start if data in the volume is not compatible. In case of PostgreSQL version bump this requires developers to manually delete the volume.

My idea was to "version" the volume together with PostgreSQL image and force Docker Compose to create a new volume for the database in case of a major version bump since the development database is ephemeral anyway. The only way to use variable in volume's name is to use name:, because keys of the top-level volumes: mapping are not interpolated. But name: is not automatically project-scoped, hence there is a need to explicitly use COMPOSE_PROJECT_NAME as a prefix.

COMPOSE_PROJECT_NAME is set in .env file, because Compose file is located inside a nested directory within the project, and otherwise the project wouldn't have a meaningful name. Finally, --project-name is also set, because we are using Visual Studio Code and Dev Containers extension and --project-name value differs from COMPOSE_PROJECT_NAME in .env file because of a bug in Dev Containers extension.

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