Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set $PROJECT_NAME in docker-compose file

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?

like image 609
Hedge Avatar asked Jul 05 '17 10:07

Hedge


People also ask

How do I change the docker compose project name?

To provide a custom project name, we can use the --project-name or -p flag with the $ docker-compose command. You can also use the COMPOSE_PROJECT_NAME environment variable to do the same (ref).

Can I use environment variables in docker compose file?

Docker Compose allows us to pass environment variables in via command line or to define them in our shell. However, it's best to keep these values inside the actual Compose file and out of the command line.

How do I debug a docker compose file?

If you want to debug in Docker Compose, run the command Docker Compose Up using one of the two Docker Compose files as described in the previous section, and then attach using the appropriate Attach launch configuration. Launching directly using the normal launch configuration does not use Docker Compose.


2 Answers

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.

like image 59
Ayushya Avatar answered Sep 21 '22 06:09

Ayushya


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.

like image 29
Christian Hjelmslund Avatar answered Sep 19 '22 06:09

Christian Hjelmslund