Multiple databases in docker and docker-compose

Usually when I need more than one database in a docker project it’s a test database. I find it easier to simply spin up a second docker container, without worrying about scripts or volume separation.
The main trick is to not conflict the default ports (e.g. 5432 for postgres) and you’re good to go.
Then docker-compose can be something as simple as this:

version: '3.0'

services: 

  db:
    image: postgres
    environment: 
      - POSTGRES_DB
      - POSTGRES_USER
      - POSTGRES_PASSWORD
    ports:
      - ${POSTGRES_DEV_PORT}:5432
    volumes:
      - app-volume:/var/lib/postgresql/data

  db-test:
    image: postgres
    environment: 
      - POSTGRES_DB
      - POSTGRES_USER
      - POSTGRES_PASSWORD
    ports:
      - ${POSTGRES_TEST_PORT}:5432
    # Notice I don't even use a volume here since I don't care to persist test data between runs

volumes:
  app-volume: #

Caveat: Obviously, more containers will typically imply in a higher memory footprint

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)