kirastel
kirastel

Reputation: 39

running two identical docker-compose containers on the same server

I've run into a problem. There is a microservice, its docker-compose:

version: "3.3"

networks:
  default:
    external:
      name: traefik

services:
  app:
    build: .
    container_name: test
    command: sh -c "uvicorn app.main:app --reload --host 0.0.0.0 --port 7778"
    restart: always
    links:
      - db_test
      - worker_test
      - beat_test
      - rabbitmq_test
    volumes:
      - ./app/:/code/app
    labels:
      - "traefik.enable=true"
      - "traefik.subdomain=test"
      - "traefik.http.services.test.loadbalancer.server.port=7778"
      - "traefik.http.routers.test.tls.certresolver=letsEncrypt"

    env_file:
      - ./.env
    depends_on:
      - db_test

  db_test:
    image: postgres
    container_name: db-test
    restart: always
    environment:
      POSTGRES_USER: docker
      POSTGRES_PASSWORD: docker
      POSTGRES_DB: test
    volumes:
      - ./data/postgres:/var/lib/postgresql/data

  worker_test:
    build: .
    container_name: merge_duplicates_worker-test
    restart: always
    command: ./app/worker.sh
    env_file:
      - ./.env
    depends_on:
      - rabbitmq_test

  beat_test:
    build: .
    container_name: beat-test
    command: ./app/beat.sh
    restart: always
    env_file:
      - ./.env
    depends_on:
      - rabbitmq_test

  rabbitmq_test:
    container_name: rabbitmq-test
    restart: always
    image: "rabbitmq:3.10.5"

When I start everything works fine as it should, now I change the port for traefik, traefik.subdomain, container names, etc. and I start the second container, it works without errors, but the problem is that data from the first container of its database is pulled into the new container into the database. Links does not affect this in any way, I tried to delete it, it did not affect it in any way.

Upvotes: 0

Views: 138

Answers (1)

Garuno
Garuno

Reputation: 2200

If you want to have separate DBs with separate data, you need to change the volume of the DB-Container! Something like this:

db_test:
    image: postgres
    container_name: db-test
    restart: always
    environment:
      POSTGRES_USER: docker
      POSTGRES_PASSWORD: docker
      POSTGRES_DB: test
    volumes:
      - ./data/postgres2:/var/lib/postgresql/data

Notice the ./data/postgres2 part. Also running two instances of PostgreSQL on the same data directory sounds dangerous and may lead to data loss.

Upvotes: 2

Related Questions