Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use COMPOSE_PROJECT_NAME? #3431

Closed
Mahmoudz opened this issue May 8, 2016 · 17 comments
Closed

How to use COMPOSE_PROJECT_NAME? #3431

Mahmoudz opened this issue May 8, 2016 · 17 comments

Comments

@Mahmoudz
Copy link

Mahmoudz commented May 8, 2016

I'm using the latest version of everything and the v2 of docker compose.

  • I tried to set it in the docker-compose.yml file with no luck as there's no clear reference on where to place it exactly?
  • I tried to add it in an .env file but didn't read it!
  • I also tried to add it in the command and it keeps giving an error.
docker-compose up -d -p anything   nginx php mysql
docker-compose up -d --project-name anything   nginx php mysql

I'm more interested in adding it to the docker-compose file.
The main reason is I want to override the default project name which is the directory name.

@ryanweal
Copy link

ryanweal commented May 8, 2016

I have to do this to work around the issue you have been running into:

COMPOSE_PROJECT_NAME=project docker-compose up

... and within my docker-compose.yml file I use the variable with the following syntax:

${COMPOSE_PROJECT_NAME}

So essentially I have the same problem that you do, but fortunately this workaround has been serving me well.

Note that if you set the variable on a separate line of the script it will not be read... same as setting it in .env as you have noted.

I would really like for the -p switch to work, it is much easier to remember and to type!

@ryanweal
Copy link

ryanweal commented May 8, 2016

It may be worth noting that I'm on Ubuntu 14.04 in this case... I'm curious if the -p switch works on other platforms.

@nschoe
Copy link

nschoe commented May 24, 2016

Where are we on this? Is it solved for you @ryanweal, @Mahmoudz?

From docker-compose --help:

Usage:
docker-compose [-f ...] [options] [COMMAND] [ARGS...]

So you have to use the -p before the up which is a command.

So it's expected that docker-compose up -p projectName doesn't work as it should be docker-compose -p projectName up.

Can you try and see if this solves your problems?

Hope this helped :-)

@ryanweal
Copy link

I could not get the -p switch to work at all. The environment variable only worked if it was passed on the same line as the up command. I could have used an entirely different variable but I'm hoping that this flag will start working at some point.

@ryanweal
Copy link

ryanweal commented May 24, 2016

Here is what I get when I try the suggested format:
rcw@mojito:~/katli$ docker-compose -p sf up
WARNING: The COMPOSE_PROJECT_NAME variable is not set. Defaulting to a blank string.
Starting sf_db_1
Recreating sf_php56_1
Recreating sf_php7_1
Attaching to sf_db_1, sf_php56_1, sf_php7_1

What is curious is that it appears to have named the containers correctly in spite of the warning. I've seen it start up boxes with a null value before though, not sure if it was this same syntax or not. So I guess it might work... but with an unexpected warning.

@nschoe
Copy link

nschoe commented May 24, 2016

@ryanweal Yes, it's perfectly normal:

  • when you previously saw boxes started with a null value before, it was because your -p was misplaced.
  • Now that -p is placed correctly, docker-compose created the containers with the right name.

Now for COMPOSE_PROJECT_NAME, I think you have mistaken its usage, here is how it works:

when you use docker-compose to create containers, it needs a "project name" to preprend to the container's name, as you saw. If you don't specify -p option, docker-compose looks for an environment varaible named COMPOSE_PROJECT_NAME, and if it's empty, it defaults to the current working directory.

So you have three scenarii :

  1. if you don't care about project name because you will only launch ONE docker-compose stack, then don't use anything ^^
  2. if you simply need to set a project name to preprend the name of your containers, use the -p option
  3. if you need to set the project name to preprend thename of your container AND you need to access the project name inside the docker-compose file, via ${COMPOSE_PROJECT_NAME} then you should use export the ENV variable COMPOSE_PROJECT_NAME.

From what I understand of your last post, it seems that you are setting the project name with -p (and it does preprend the name of the containers, as you saw) but then you expect the COMPOSE_PROJECT_NAME to be set, which is not. docker-compose does not (to my knowledge) set the COMPOSE_PROJECT_NAME variable when you set the name with -p, this ENV variable only used by docker-compose to look for a project name.

Hope this helped!

@ryanweal
Copy link

Ah, so basically if I need to use a custom variable I'll have to provide the variable twice... once for myself and once for docker-compose. That seems inefficient. I'll stick to setting the COMPOSE_PROJECT_NAME variable in my case then.

@nschoe
Copy link

nschoe commented May 26, 2016

No I don't understand why you would say that if you need to use a custom variable you have to define it twice. It's just that the -p option is used to give docker-compose a project-name so it can preprend the name of each containers (so that, in turn, you can launch multiple compose stacks).

It's just that you expected that it defined an ENV variable too, named COMPOSE_PROJECT_NAME, which is doesn't. So there is nothing wrong here.

If you need to define, go ahead and do it :-)

@shin- shin- closed this as completed Oct 17, 2016
@yashgt
Copy link

yashgt commented Mar 24, 2017

As reported on the forum at https://forums.docker.com/t/unable-to-use-compose-project-name-in-docker-compose/30157, the COMPOSE_PROJECT_NAME nor the -p seem to work in docker-compose 1.10.0. It did work for 1.8.0.

@yashgt
Copy link

yashgt commented Mar 24, 2017

COMPOSE_PROJECT_NAME is a documented env var. Refer to https://docs.docker.com/compose/reference/envvars/

jasonmp85 added a commit to citusdata/citus_docs that referenced this issue Sep 15, 2017
Figured we should stop directing users to an old docker-compose binary.

In addition, this switches from the -p flag to COMPOSE_PROJECT_NAME to
ensure that environment variable is set. -p behaves the same but does
not set the variable. See the following link for more.

docker/compose#3431 (comment)

Finally, I added the -v flag to docker-compose down, which ensures any
automatic volumes are removed when the cluster is stopped.
jasonmp85 added a commit to citusdata/citus_docs that referenced this issue Sep 28, 2017
Figured we should stop directing users to an old docker-compose binary.

In addition, this switches from the -p flag to COMPOSE_PROJECT_NAME to
ensure that environment variable is set. -p behaves the same but does
not set the variable. See the following link for more.

docker/compose#3431 (comment)

Finally, I added the -v flag to docker-compose down, which ensures any
automatic volumes are removed when the cluster is stopped.
begriffs pushed a commit to citusdata/citus_docs that referenced this issue Sep 28, 2017
Figured we should stop directing users to an old docker-compose binary.

In addition, this switches from the -p flag to COMPOSE_PROJECT_NAME to
ensure that environment variable is set. -p behaves the same but does
not set the variable. See the following link for more.

docker/compose#3431 (comment)

Finally, I added the -v flag to docker-compose down, which ensures any
automatic volumes are removed when the cluster is stopped.
naisila pushed a commit to citusdata/citus_docs that referenced this issue Jul 18, 2018
Figured we should stop directing users to an old docker-compose binary.

In addition, this switches from the -p flag to COMPOSE_PROJECT_NAME to
ensure that environment variable is set. -p behaves the same but does
not set the variable. See the following link for more.

docker/compose#3431 (comment)

Finally, I added the -v flag to docker-compose down, which ensures any
automatic volumes are removed when the cluster is stopped.
@melkamar
Copy link

Just stumbled upon this now. I think what is confusing about this scenario is that setting the COMPOSE_PROJECT_NAME environment variable will:

  1. allow that variable to be used in the docker-compose.yml file
  2. change the project name that docker compose will use.

Because -p is used to change the 2., it's reasonable to expect that it will also affect 1.. (Or, at least, it was reasonable for me and the original poster of this issue).

The key difference here is that the environment variable is only used for reading by docker compose. It will not be automatically populated. (Contrast that to e.g. CI environment where the environment variables are provided for you to consume.)

The current way this works is fine, but I think the documentation could use an explicit warning about this.

@borja
Copy link

borja commented Mar 13, 2020

I totally agree with @melkamar. If I want to define any other item (e.g. network names) using the project name as a prefix, the ENV variable is mandatory. Docs are a bit confusing at this point.

networks: ["${COMPOSE_PROJECT_NAME}-network"]

@RamsesRodenburg
Copy link

Thing is, though , the docs say: "If you do not set this, the COMPOSE_PROJECT_NAME defaults to the basename of the project directory."
In reality, the value remains empty when I run "docker-compose config" or "docker-compose up ...".
So, which is it?

@setop
Copy link

setop commented Jul 23, 2021

Thing is, though , the docs say: "If you do not set this, the COMPOSE_PROJECT_NAME defaults to the basename of the project directory."
In reality, the value remains empty when I run "docker-compose config" or "docker-compose up ...".
So, which is it?

Same concern here (v1.29.2) : I was expected when reading the documentation that COMPOSE_PROJECT_NAME will be set to the basename of the project directory but it stays empty.

@karthikcru
Copy link

The important thing to note is setting -p also changes the name of the docker compose volumes and network.

This is super useful when I have builds happening for different environments, avoids network and volume name clashes if the builds for different environments happen to run at the same time.

@freewilll
Copy link

I would also have liked COMPOSE_PROJECT_NAME to be set if the only -p was passed on the CLI.

In my scenario, I have one docker compose file I want to run for two instances of a huge application. I'd much rather use

docker-compose -p foo ...

than

COMPOSE_PROJECT_NAME=foo docker-compose ...

@Ajedi32
Copy link

Ajedi32 commented Jul 21, 2022

Looks like this is going to be fixed in newer versions of docker compose (anything using compose-go >= v1.2.8, which in this case means docker compose >= 2.6.1), since it's apparently part of the spec now that interpolating COMPOSE_PROJECT_NAME should always give you the correct project name, not just whatever that environment variable happens to be set to in your shell.

The compose spec says:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests