Kouja
Kouja

Reputation: 183

docker-compose containers uses wrong container with multiple projects

I have two projects and I need two differents docker environnement (containers). I have two docker-compose.yml files in two different projects. foo project and bar project.

foo/src/website/docker-compose.yml #1 (foo)

version: '3'
services:
  db:
    env_file: .env
    image: mariadb:10.0.23
    container_name: foo-db
    ports:
      - "42333:3306"
    restart: always
  web:
    image: project/foo
    container_name: foo-web
    env_file: .env
    build: .
    restart: always
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails server -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/webapps/foo
    ports:
      - "3000:3000"
    depends_on:
      - db

bar/src/website/docker-compose.yml #2 (bar)

version: '3'
services:
  db:
    image: mysql:5.5.50
    container_name: bar-db
    ports:
      - "42333:3306"
    env_file: .env
    restart: always
  web:
    image: project/bar
    container_name: bar-web
    env_file: .env
    build: .
    restart: always
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails server -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/webapps/bar
    ports:
      - "3000:3000"
    depends_on:
      - db

I do this command for my foo project docker-compose build and docker-compose up, everything works. In Kitematic I see my two containers with the good names (foo-web).

  1. I do this command to stop my image docker-compose stop.
  2. I go to my second project (bar) and run docker-compose build and docker-compose up. everything works, but my container name in now replaced by bar-web.
  3. I stop my second image with docker-compose stop and I try to perform docker-compose up in my foo project folder again but it fails.

How can I keep two different containers and easily switch from one to the other with docker-compose stop and docker-compose up?

Edit 1

I found the issue, the main folder where my docker-compose.yml are located for my two projects have the same folder name. Can I fix this or I need to rename my folders?

Upvotes: 10

Views: 3111

Answers (3)

Vikram Hosakote
Vikram Hosakote

Reputation: 3664

Yes, the directory name is the default project name for docker-compose:

$ docker-compose --help
  ...
  -p, --project-name NAME    Specify an alternate project name (default: directory name)

Use the -p argument to specify a particular non-default project name.

Alternatively, you can also set the COMPOSE_PROJECT_NAME environment variable (which defaults to the basename of the project directory).

If you are sharing compose configurations between files and projects with multiple compose files, refer to this link for more info.

Upvotes: 12

Kouja
Kouja

Reputation: 183

I found the solution

docker-compose -p projectname build 
docker-compose -p projectname up

Upvotes: 5

Fiber Optic
Fiber Optic

Reputation: 194

Your problem comes from the ports. As you define public ports, each container must have its own. So in your second project, change the ports of the db and web (42334 and 3001 for example).

Upvotes: 0

Related Questions