76

I am using the same docker-compose.yml file for multiple projects. I am really lazy, so I don't want to start them with docker-compose -p $PROJECT_NAME up.

As of Docker version 17.06.0, is it possible to set the variable directly in the docker-compose.yml file?

2
  • 2
    what is the name of the variable? I cannot find in the docs Commented Mar 19, 2018 at 16:43
  • Please clarify or add a link to the documentation. Commented Feb 18, 2020 at 12:12

5 Answers 5

111

UPDATE: You can now use the top-level name property in your docker-compose YAML file. This is available from Docker Compose v2.3.3

This is the result of the #745 proposal. An issue which persevered for about 8 years.

Previously: Right now, we can use the .env file to set the custom project name like this:

COMPOSE_PROJECT_NAME=SOMEPROJECTNAME

It's not flexible, but it's better than nothing. Currently, there is an open issue regarding this as a proposal.

Sign up to request clarification or add additional context in comments.

4 Comments

Compose supports declaring default environment variables in an environment file named .env placed in the folder where the docker-compose command is executed (current working directory).
It silly that for all this time they still can't add this little feature directly in compose file. Whats their reasoning for not doing it? What if I want to run docker-compose in same dir for multiple projects? Then .env file is useless
With newer versions of Docker Compose, this can now be done in the docker-compose.yml file with the name: property (see @Michal's answer).
Example docker-compose.yml (replace \n with a new line in your editor): version: "3"\n name: projectName\n services:\n
17

I know this question was asked a long time ago, but I ran into the same problem. There's a suggestion to add the feature https://github.com/docker/compose/issues/745, but they don't want to.

However, I have a Makefile in the root of the directory and then you can add something like in the Makefile:

.PHONY: container-name
container-name:
    docker-compose -p $PROJECT_NAME up -d container-name

and then run make container-name

I know it isn't what you asked for, but could maybe make your life a bit easier.

2 Comments

great solution, better than creating an .env file, thanks!
Hey, it looks like they might be implementing this soon. github.com/docker/compose/issues/745#issuecomment-1025146862
9

220806 UPDATE: you can now use the top-level name property in your docker-compose YAML file.

This is the result of the #745 proposal.

1 Comment

According to the comments in the GitHub ticket, from at least Docker Compose v2.3.3.
4

Update as on Docker Compose version 2.3.3, name can be given in the compose file, but please note the following as per documentation compose-spec at github.com., Compose official documentation

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.

name: stitch
services:
  foo:
    image: busybox
    environment:
      - COMPOSE_PROJECT_NAME
    command: echo "I'm running ${COMPOSE_PROJECT_NAME}"

Previously proposed solution : You can add them as your environment variables which are available through the session in which you are bringing up your containers using the docker compose.

Ie, if you wanted to use $PROJECT_NAME somewhere inside your docker-compose.yaml then if this variable has a value in your session, then it would be picked up.

Inside the yaml you can assign it to anything as you want it. You want as a commandline arg to some script, even that is also possible. ie,

working_dir: /opt
command: /bin/bash -c './script.sh ${PROJECT_NAME}'
volumes:
    - /var/run/:/host/var/run/

I'm using docker version : Docker version 17.09.0-ce, build afdb6d4 docker-compose version : docker-compose version 1.14.0, build c7bdf9e

Comments

0

in your project ROOT dir make .env file put line

PROJECT_NAME=yourprojectname

no need to add any key in docker-compose.yml

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.