Michaelo
Michaelo

Reputation: 839

docker-compose change name of main container

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

Upvotes: 58

Views: 99572

Answers (5)

Raj
Raj

Reputation: 176

I struggled a bit with the given answers. Then I read the following

"Unlike Compose V1, Compose V2 integrates into the Docker CLI platform and the recommended command-line syntax is docker compose." Also, the version is deprecated.

Ex.: docker compose build (notice no - hyphen)

Upvotes: 0

Tigerware
Tigerware

Reputation: 3894

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.

Upvotes: 79

rassakra
rassakra

Reputation: 1121

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

Upvotes: 3

San Jaisy
San Jaisy

Reputation: 17048

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/

Upvotes: 0

Nguyen M. Ho
Nguyen M. Ho

Reputation: 539

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.

Upvotes: 39

Related Questions