Docker-compose.yml how to read value of --project option inside the file

I want to change the database volume depending on the -p options in docker-compose cli.
something like this

version: '2'

services:
    frontend:
        build:
            context: ../Front
        environment:
            DOCKERIZED: 1
        ports:
            - '${FRONTEND_PORT:-8090}:3000'
        depends_on: [ backend ]
    backend:
        build:
            context: .
            args:
                DOCKERIZED: 1
        networks: [ link ]
        environment:
            DOCKERIZED: 1
        depends_on: [ db ]
        ports:
            - '${BACKEND_PORT:-8080}:8080'
    db:
        image: mariadb:10.5.13
        environment:
            MYSQL_ROOT_PASSWORD: gpassword
            MYSQL_DATABASE: ateliers
        networks: [ link ]
        volumes: [ '${PROJECT}_db_data':/var/lib/mysql ]
        command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
networks:
    link:
        driver: bridge

volumes:
    '${PROJECT}_db_data':

where ${PROJECT} is the value of the -p option supplied when running for exampledocker-compose -p dev up -d.
is there something similar? I made ${PROJECT} from my mind just as an example.

Looks like you try to solve something that is already solved.

Docker compose indeed uses a project name, which uses the directory name where the compose file is located as a fallback, if it’s not specifically provided by the --project-name or -p argument.

All resources in a compose project are prefixed with ${project}_ by default. This allows to deploy the same compose file with different project names, and get individual resources per project name without having to change anything in the compose file.

If a volume in the top-level volumes section is called db_data, it will be created as ${project}_db_data in the docker engine. Your link network is called ${project}_link in the docker engine and so on.

Check the outputs of docker network ls, docker volume ls and docker container ls and you will see your resources are already prefixed with the project name.

1 Like