33

I have a simple frontend and backend app. And I have a docker-compose file but it's inside frontend folder. So when I run it both frontend and backend containers are under frontend container (it takes name of the folder) how can I rename this main container? I am using version 3.9

version: "3.9"
services:
  be_service:
    container_name: backend
    build:
      context: ../backend
      dockerfile: ./Dockerfile
    ports:
      - "8089:8080"
  fe_service:
    container_name: frontend
    build:
      context: ./
      dockerfile: ./Dockerfile
    ports:
      - "8088:80"
    depends_on:
      - be_service
Michaelo
  • 579
  • 1
  • 5
  • 13
  • 1
    Are both containers running the same code (I'd find that a little surprising)? Which thing "takes the name of the folder"? (Also see the [COMPOSE_PROJECT_NAME](https://docs.docker.com/compose/reference/envvars/#compose_project_name) environment variable, which is involved in naming Docker-level objects.) – David Maze Jul 13 '21 at 10:41
  • @DavidMaze Thanks that was the thing :) – Michaelo Jul 13 '21 at 12:48
  • Does this answer your question? [Set $PROJECT\_NAME in docker-compose file](https://stackoverflow.com/questions/44924082/set-project-name-in-docker-compose-file) – Tigerware May 28 '22 at 00:01

4 Answers4

39

When refering to your main container, you are probably refering to the project name, which you could usually set via the -p flag. (See other answers)

For docker-compose, you can set the top level variable name to your desired project name.

docker-compose.yml file:

version: "3.9"
name: my-project-name
services:
  myService:
    ...

If you are using Docker Desktop, make sure Use Docker Compose V2 is enabled there.

Tigerware
  • 3,196
  • 2
  • 23
  • 39
  • 12
    that's give error: ERROR: The Compose file '.\..\docker-compose.yml' is invalid because: 'name' does not match any of the regexes: '^x-'. EDIT: Enabling "Use Docker Compose V2" in Docker Desktop fixed the problem – Hugues Gauthier Aug 05 '22 at 12:38
  • 2
    I downvoted because I got the same error. Then the fix @HuguesGauthier came up with fixed it for me too. However, StackOverflow has not locked my downvote and I cannot change my vote until you update your answer with the fix. Just a friendly heads up. – Matt Sep 05 '22 at 09:02
  • yeah this is not working for me either in 2023. The solution suggested by @HuguesGauthier(aka Nguyen Ho) is not for docker-compose.yml file. Is there a way to set this directly in the file itself? – theprogrammer May 03 '23 at 00:39
  • Be carefull with the name of the project : From the doc : Project names must contain only lowercase letters, decimal digits, dashes, and underscores, and must begin with a lowercase letter or decimal digit. – G.Lebret Aug 14 '23 at 10:15
26

Related to Docker Compose docs you can set your project name with:

docker-compose -p app up --build

with -p app to set your compose container name to app.

Nguyen Ho
  • 409
  • 5
  • 6
4

I think that your docker compose file is right and to change the co you can use the containe_name instruction but I think you should run this command when you want to run your application :

docker-compose up --build
rassakra
  • 1,062
  • 5
  • 9
  • What did you change? Your docker-compose.yml looks exactly like in the question. – Tigerware May 27 '22 at 21:29
  • I mentioned that you should only use the command but the docker-compose file still the same please read what I mentioned before the command , thanks – rassakra May 28 '22 at 09:00
0

Use -p to specify a project name

Each configuration has a project name. If you supply a -p flag, you can specify a project name. If you don’t specify the flag, Compose uses the current directory name.

Calling docker-compose --profile frontend up will start the services with the profile frontend and services without specified profiles. You can also enable multiple profiles, e.g. with docker-compose --profile frontend --profile debug up the profiles frontend and debug will be enabled

Also refer https://docs.docker.com/compose/profiles/

San Jaisy
  • 15,327
  • 34
  • 171
  • 290