docker-compose change name of main container

Viewed 15544

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
4 Answers

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.

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.

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

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/

Related