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

Project name in docker-compose.yml #1123

Closed
fntlnz opened this issue Mar 17, 2015 · 25 comments
Closed

Project name in docker-compose.yml #1123

fntlnz opened this issue Mar 17, 2015 · 25 comments

Comments

@fntlnz
Copy link

fntlnz commented Mar 17, 2015

I have a number of projects which are using docker-compose and I'm starting to have the need to specify the project name directly into the docker-compose.yml

Consider this scenario:

I have two projects with the following directory structures

project1
|-- docker
|    |-- docker-compose.yml
|-- public
|    |--index.php
|-- vendor
|-- modules
project2
|-- docker
|    |-- docker-compose.yml
|-- public
|    |--index.php
|-- vendor
|-- modules

And with two docker-compose.yml like this:

web:
  image: fntlnz/nginx:1.6.2
  volumes:
    - ../:/var/www/project
  ports:
    - "127.0.0.2:80:80"
  links:
    - fpm:fpm

fpm:
  image: fntlnz/php-fpm:5.6.6
  volumes:
    - ../:/var/www/project
  links:
    - redis:redis

redis:
  image: redis

When I issue a docker-compose up in the project1 directory it will start three containers with the following names docker_web_1, docker_fpm_1, docker_redis_1.

If now I issue the same command in the project2 directory it recreates the same containers of project1 instead creating it's own.

I know that I can change the project name using the -p option or the COMPOSE_PROJECT_NAME environment variable but IMHO would be useful to have in the docker-compose.yml something like project_name: project1.

My docker-compose.yml would become:

project_name: project1
web:
  image: fntlnz/nginx:1.6.2
  volumes:
    - ../:/var/www/project
  ports:
    - "127.0.0.2:80:80"
  links:
    - fpm:fpm

fpm:
  image: fntlnz/php-fpm:5.6.6
  volumes:
    - ../:/var/www/project
  links:
    - redis:redis

redis:
  image: redis
@dnephin
Copy link

dnephin commented Mar 18, 2015

Probably a duplicate of #45 (#463) and #745

@mikehaertl
Copy link

+1 but agree, that this should be closed as duplicate of #45. I think #745 is a little different, no?

@fntlnz
Copy link
Author

fntlnz commented Mar 31, 2015

It is definitely a duplicate of #45

@fntlnz fntlnz closed this as completed Mar 31, 2015
@robeatoz
Copy link

Like @igor-lemon wrote in #45 (comment):

Right now we can use .env file with custom project name and set
COMPOSE_PROJECT_NAME=SOMEPROJECTNAME

You can find more options here: https://docs.docker.com/compose/env-file/

@isaac-weisberg
Copy link

Dawg this is such a basic feature

@Z-a-r-a-k-i
Copy link

Would it be so difficult to add the possibility of setting the project name directly from inside the docker-compose.yml file ?
I mean now that I put docker-compose.yml files everywhere in my projects like in some src folder, it's a bit painful to have to create a .env file all the time and I know that a lot of developers are facing the same problem.

@benjick
Copy link

benjick commented Nov 23, 2020

Right now we can use .env file with custom project name and set COMPOSE_PROJECT_NAME=SOMEPROJECTNAME

Not really helpful if you have several YAML files in the same directory that you wanna use different project names for. Can't believe this function doesn't exist.

version: '3.8'
project_name: foo
services:
  ...

would be so nice

@f1am3d
Copy link

f1am3d commented May 5, 2022

Any updates?

@glours
Copy link
Contributor

glours commented May 5, 2022

@f1ames0ff the support was introduce in compose-go v1.1.0 so it supported in Compose v2 since version v2.3.0

@Z-a-r-a-k-i
Copy link

@f1ames0ff the support was introduce in compose-go v1.1.0 so it supported in Compose v2 since version v2.3.0

Cannot find the information, could you be more explicit about "the support"?

Thank you!

@MatthewLymer
Copy link

MatthewLymer commented May 12, 2022

@Z-a-r-a-k-i

version: "3"
name: "my-project" # <--- this
services:
  mysql: ...

@nemanjam
Copy link

@Z-a-r-a-k-i

version: "3"
name: "my-project" # <--- this
services:
  mysql: ...

Is name supported? I get this:

ERROR: The Compose file './docker-compose.test.yml' is invalid because:
'name' does not match any of the regexes: '^x-'

@aaroncrawford
Copy link

@nemanjam It's working for me - I'm using Docker compose v2 which I believe is required. If you have docker desktop, you can go to settings and flip on v2 support.

@nemanjam
Copy link

I use v3.8 and I had to do it like this docker-compose -f docker-compose.test.yml -p npb-test up -d.

@MatthewLymer
Copy link

I use v3.8 and I had to do it like this docker-compose -f docker-compose.test.yml -p npb-test up -d.

I believe you must use the newer docker<space>compose, not docker<dash>compose.

@sskras
Copy link

sskras commented Jun 3, 2022

@MatthewLymer ** commented 16 days ago:

I use v3.8 and I had to do it like this docker-compose -f docker-compose.test.yml -p npb-test up -d.

I believe you must use the newer docker<space>compose, not docker<dash>compose.

Umm, no:

$ docker compose
docker: 'compose' is not a docker command.
See 'docker --help'

$ docker --version
Docker version 20.10.14, build a224086

$ docker-compose --version
docker-compose version 1.29.2, build 5becea4c

@sskras
Copy link

sskras commented Jun 3, 2022

@aaroncrawford commented 16 days ago:

@nemanjam It's working for me - I'm using Docker compose v2 which I believe is required. If you have docker desktop, you can go to settings and flip on v2 support.

Can you show an example? I use CLI only, and setting version: "2.2" does not help.
IOW, I confirm what @nemanjam says.

@boxertech
Copy link

When I just set version: '2.2' I got the error "'name' does not match any of the regexes: '^x-'". When I went into docker desktop and turned on v2 compose support, I got the error "(root) Additional property name is not allowed". When I used docker compose instead of docker-compose, I go the same property name is not allowed error.

I dropped name from my compose file, and I used the env variable. It worked, but it's not ideal for reasons stated above.

@sskras
Copy link

sskras commented Jul 1, 2022

OK, so now I get the idea. There are two seemingly related things that can have the second version:

The latter works as a Docker plugin, not as a standalone binary like V1.
Hence the dash disappearing from V1 docker-compose to become V2 which is invoked by docker compose now.


So, my summary is that you probably need to install Docker Compose V2 according to your setup:
https://docs.docker.com/compose/install/#compose-prerequisites

... and maybe then the name:  starts working. I have yet to check that (to upgrade to Compose V2).

@sskras
Copy link

sskras commented Jul 1, 2022

@boxertech commented 2 hours ago:

When I went into docker desktop and turned on v2 compose support, I got the error "(root) Additional property name is not allowed". When I used docker compose instead of docker-compose, I go the same property name is not allowed error.

Hey, what's the exact v2 version number of your Compose?
Folks say it should be there since v2.3.0

@johnthagen
Copy link
Contributor

johnthagen commented Sep 13, 2022

Documented here:

Implementation:

@uuf6429
Copy link

uuf6429 commented Aug 13, 2023

Documented here:

Implementation:

That first link is "broken" - I believe it should now be https://docs.docker.com/compose/compose-file/04-version-and-name/

@jakubgs
Copy link

jakubgs commented Sep 28, 2023

From what version of Docker Compose is this field available? I am running 1.29.2 and it doesn't work:

The Compose file './docker-compose.yml' is invalid because:
'name' does not match any of the regexes: '^x-'
You might be seeing this error because you're using the wrong Compose file version. Either specify a supported version (e.g "2.2" or "3.3") and place your service definitions under the `services` key, or omit the `version` key and place your service definitions at the root of the file to use version 1.
For more on the Compose file format versions, see https://docs.docker.com/compose/compose-file/

@glours
Copy link
Contributor

glours commented Sep 28, 2023

@jakubgs Compose v1 is EOL, please switch to Compose v2.
Here a link to install the latest version of Compose

@jakubgs
Copy link

jakubgs commented Sep 28, 2023

Ah, indeed, cheers.

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

No branches or pull requests