Cannot run multiple docker compose on centos7

I have two nuxt projects, so I have two docker-compose.yml files. I put them in different paths. I enter the two paths and start them using the command docker compose up -d. But I found that when I start the second one, the first project will be replaced. When I go back to start the first project again, the second project will be replaced. :rofl:Why is this?
Below are my two docker-compose.yml files.

services:
  frontend:
    build:
      context: .
    container_name: nuxt_1
    restart: on-failure
    ports:
      - '3010:3010'
    networks:
      - nuxt_1
    healthcheck:
      test: [ 'CMD', 'curl', '-f', 'http://localhost:3010/' ]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 10s
    extra_hosts:
      - 'host.docker.internal:host-gateway' # 使得容器可以通过 host.docker.internal 访问宿主机的网络

networks:
  nuxt_1:
    driver: bridge

services:
  frontend:
    build:
      context: .
    container_name: nuxt_2
    restart: on-failure
    ports:
      - '3011:3011'
    networks:
      - nuxt_2
    healthcheck:
      test: [ 'CMD', 'curl', '-f', 'http://localhost:3011/' ]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 10s
    extra_hosts:
      - 'host.docker.internal:host-gateway' # 使得容器可以通过 host.docker.internal 访问宿主机的网络

networks:
  nuxt_2:
    driver: bridge

This is my system information:

CentOS Linux release 7.9.2009 (Core)
3.10.0-1160.76.1.el7.x86_64
Docker version 26.1.4, build 5650f9b

I asked github copilot chat, and it said it might be a network problem, but after I modified the network it still didn’t work. It might also be a system resource limit such as memory, but this is a new server, and there are a lot of idle memory, CPU and other resources.

Finally, my two docker composes both use the host’s redis:6379. Will this cause this problem? In addition, I did not download docker-compose because docker compose is built-in after docker version 20, so I have always used docker cli to run docker compose.

Looking forward to your reply.

The services only replace each other if they have the same service and project name

The default project name is the name of the directory the compose file is in

So either:
A. The compose files are within directories of the same name - In this case, add the name property to choose the project name
B. You have the COMPOSE_PROJECT_NAME environment variable set, which affects both files

1 Like

Thanks for your answer, the parent folder of my docker-compose.yml did have the same name, I solved this problem by using docker compose -p

Also an option, just make sure to always remember to set that flag, or just define it in the compose file with the name property

After all, compose was made specifically so you do not have to specify many flags in the run command, so why specify those in the compose command?

1 Like

Yes, using name is more elegant. I have added the name option to my compose file.