I'm using the latest version of everything and the v2 of docker compose.
docker-compose.yml
file with no luck as there's no clear reference on where to place it exactly?.env
file but didn't read it!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.
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!
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.
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 :-)
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.
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.
@ryanweal Yes, it's perfectly normal:
-p
was misplaced.-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 :
-p
optionFrom 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!
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.
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 :-)
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.
COMPOSE_PROJECT_NAME is a documented env var. Refer to https://docs.docker.com/compose/reference/envvars/
Just stumbled upon this now. I think what is confusing about this scenario is that setting the COMPOSE_PROJECT_NAME
environment variable will:
docker-compose.yml
file 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.
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"]
Most helpful comment
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"]