Can I reference the service name docker-compose.yml

10,863

Solution 1

I suppose you can use the variable COMPOSE_PROJECT_NAME: https://docs.docker.com/compose/reference/envvars/#compose_project_name

Sets the project name. This value is prepended along with the service name to the container on start up. For example, if your project name is myapp and it includes two services db and web, then Compose starts containers named myapp_db_1 and myapp_web_1 respectively.

Solution 2

The docker-compose documentation does not provide for that.

You might want to use the common pattern of creating volumes within your stack file for use in containers.

version: "2"

services:
  web:
    volumes:
      - web-logs:/var/log/web

volumes:
  web-logs:
    external: true

https://docs.docker.com/compose/swarm/

https://docs.docker.com/compose/compose-file/#variable-substitution https://docs.docker.com/compose/compose-file/#volumes-volume-driver

Share:
10,863

Related videos on Youtube

Mandragor
Author by

Mandragor

Updated on September 15, 2022

Comments

  • Mandragor
    Mandragor over 1 year

    In docker-compose.yaml, is there a way to reference the service name (web, database), so that in the code below, the volumes would be created as /store/web for web and /store/database for database ?

    ---
    version: '2'
    
    services:
      web:
        volumes:
        - /store/${reference_service_name_above}
    
      database:
        volumes:
        - /store/${reference_service_name_above}