Can I reference the service name docker-compose.yml
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 servicesdb
andweb
, then Compose starts containers namedmyapp_db_1
andmyapp_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
Related videos on Youtube
Mandragor
Updated on September 15, 2022Comments
-
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}