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

Expose COMPOSE_PROJECT_NAME for interpolation #240

Closed
ahti opened this issue Mar 19, 2022 · 3 comments
Closed

Expose COMPOSE_PROJECT_NAME for interpolation #240

ahti opened this issue Mar 19, 2022 · 3 comments

Comments

@ahti
Copy link

ahti commented Mar 19, 2022

compose-spec/compose-spec#206 introduced a top-level project name property to the compose file spec.

#231 implemented support for this property, but I believe the implementation does not yet comply with the second sentence in the spec:

Whenever project name is defined by top-level name or by some custom mechanism, it MUST be exposed for
interpolation and environment variable resolution as COMPOSE_PROJECT_NAME

From what I can see in loader/loader.go, determining the name happens after parsing/interpolating, and indeed: With compose version 2.3.3, ${COMPOSE_PROJECT_NAME} is only replaced when the project name is set via the COMPOSE_PROJECT_NAME env variable.

If the name is set via command line parameter, the top-level name property, or left as the folder name default (all of which should work according to my interpretation of the spec text), ${COMPOSE_PROJECT_NAME} is not replaced.

Fixing this would also address the long-standing and much requested docker/compose#2294

(ping @ndeloof @ulyssessouza since you two authored the two PRs mentioned above)

@ahti ahti changed the title expose COMPOSE_PROJECT_NAME for interpolation Expose COMPOSE_PROJECT_NAME for interpolation Mar 19, 2022
@williamtrelawny
Copy link

williamtrelawny commented Sep 7, 2022

This is still an issue in latest Docker Compose v2.10.2, despite the alleged fix (#272) merged in compose-go v1.2.8 which was merged to Compose back in v2.6.1.

Using the below docker-compose.yml produces the following error output: service "es-open-00" refers to undefined network graylog-open-standalone-net: invalid compose project

It may also be that I'm not handling the variable properly (the documentation on this is extremely vague and confusing).

What I want to do: simply use the parent directory of the docker-compose.yml file ("graylog-test") as the prefix for the network name (and ideally volume names) that are created upon docker compose up. This way I can use the same docker-compose.yml file in multiple directories/hosts/etc. without having to maintain multiple different yml files for each.

I thought this functionality was added in #272 but it does not seem to be working.

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.10.2)
  scan: Docker Scan (Docker Inc., v0.17.0)
WARNING: Plugin "/usr/libexec/docker/cli-plugins/docker-compose.bak" is not valid: plugin candidate "compose.bak" did not match "^[a-z][a-z0-9]*$"

Server:
 Containers: 1
  Running: 1
  Paused: 0
  Stopped: 0
 Images: 11
 Server Version: 20.10.17
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: syslog
 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 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 9cd3357b7fd7218e4aec3eae239db1f68a5a6ec6
 runc version: v1.1.4-0-g5fd4c4d
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: default
  cgroupns
 Kernel Version: 5.10.0-17-amd64
 Operating System: Debian GNU/Linux 11 (bullseye)
 OSType: linux
 Architecture: x86_64
 CPUs: 4
 Total Memory: 7.771GiB
 Name: docker-dev.lab
 ID: GARJ:SHJK:4U4X:5B2I:5PI2:U4YF:QKY3:UTA5:4XJA:FZ4O:SFYP:LFPB
 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

/home/user/graylog-test/docker-compose.yml:

version: "3.8"

services:
  mg-open-00:
    image: "mongo:4.4"  # MongoDB > v4.4 has not been verified to work with Graylog.
    volumes:
      - "mg-open-00-data:/data/db"
    restart: "unless-stopped"
    networks:
      - ${COMPOSE_PROJECT_NAME}-net

  es-open-00:
    environment:
      - "ES_JAVA_OPTS=-Xms1g -Xmx1g -Dlog4j2.formatMsgNoLookups=true"
      - "bootstrap.memory_lock=true"
      - "discovery.type=single-node"
      - "http.host=0.0.0.0"
      - "action.auto_create_index=false"
    image: "docker.elastic.co/elasticsearch/elasticsearch-oss:7.10.2"  # Graylog does NOT support >v7.10.x!
    ulimits:
      memlock:
        hard: -1
        soft: -1
    volumes:
      - "es-open-00-data:/usr/share/elasticsearch/data"
    restart: "unless-stopped"
    networks:
      - ${COMPOSE_PROJECT_NAME}-net

  gl-open-00:
    image: "${GRAYLOG_IMAGE:-graylog/graylog:4.3}"  # https://docs.docker.com/compose/environment-variables/
    depends_on:
      - "es-open-00"
      - "mg-open-00"
      - "caddy"
    entrypoint: "/usr/bin/tini -- wait-for-it es-open-00:9200 --  /docker-entrypoint.sh"
    env_file: ".env"
    ports:
      - "5044:5044"   # Beats
      - "514:514/udp"   # Syslog
      - "1514:1514"   # Syslog
      - "5555:5555"   # RAW TCP
      - "5555:5555/udp"   # RAW TCP
      - "9000"   # Server API/Web UI
      - "12201:12201" # GELF TCP
      - "12201:12201/udp" # GELF UDP
      - "13301:13301" # Forwarder data
      - "13302:13302" # Forwarder config
    volumes:
      - "gl-open-00-data:/usr/share/graylog/data/data"
      - "gl-open-00-journal:/usr/share/graylog/data/journal"
    restart: "unless-stopped"
    networks:
      - ${COMPOSE_PROJECT_NAME}-net
      - caddy-net

networks:
  caddy-net:
    external: true
  ${COMPOSE_PROJECT_NAME}-net:
    name: ${COMPOSE_PROJECT_NAME}-net  #<- not sure if this is even necessary

volumes:
  mg-open-00-data:
  es-open-00-data:
  gl-open-00-data:
  gl-open-00-journal:

@williamtrelawny
Copy link

So I found a docker-compose.yml that works:

/home/user/project1/docker-compose.yml:

services:

  service1:
    networks:
      - default
      
  service2:
    networks:
      - default
      
  service3:
    networks:
      - default
    
networks:
  default:
    name: ${COMPOSE_PROJECT_NAME}-net

This produces a network named project1-net as expected.

If this behavior is detailed somewhere in the compose file reference I could not find it.

@ndeloof
Copy link
Collaborator

ndeloof commented May 15, 2023

see docker/compose#9530

@ndeloof ndeloof closed this as completed May 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants