62

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
3
  • 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 environment variable, which is involved in naming Docker-level objects.) Commented Jul 13, 2021 at 10:41
  • @DavidMaze Thanks that was the thing :) Commented Jul 13, 2021 at 12:48
  • 1
    Does this answer your question? Set $PROJECT_NAME in docker-compose file Commented May 28, 2022 at 0:01

5 Answers 5

84

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
This work with window docker version 27.0.3, build 7d4bcd8
40

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.

1 Comment

This doesn't set the container name to app for me. It only sets the projectname in projectname-servicename-1, so app-yourService-1
3

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

Comments

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/

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.