Tomislav Mikulin
Tomislav Mikulin

Reputation: 5966

Multiple databases in docker and docker-compose

I have a project consisting of two main Java apps that use eight Postgres databases, so is there a way in docker-compose to build eight different databases so that each one has a different owner and password? Can I even do that in docker-compose?

Example:

services:
    postgresql:
        build: db/.
        ports:
            - "5432:5432"
        environment:
          - POSTGRES_DB=database1
          - POSTGRES_USER=database1
          - POSTGRES_PASSWORD=database1

I know I can put all the .sql files in the docker-entrypoint-initdb.d and Postgres will make them automatically, but how do I declare what .sql file goes in what database?

Upvotes: 97

Views: 134265

Answers (3)

Bosko Mijin
Bosko Mijin

Reputation: 3347

Inspired by the accepted answer, but not satisfied with the fact that the db user is hardcoded, I've created a fully functional solution (docker-compose.yml file with related bash script) to solve that issue.

Same mechanism is used -> add the database name values to the POSTGRES_MULTIPLE_DATABASES variable and build or mount, but this time the related user will be responsible for these databases.

You can use the docker-compose file, replace the placeholders with the current values and everything is runnable. Everything is explained in the README file.

Here is the github repository.

Upvotes: 1

Ricardo Pedroni
Ricardo Pedroni

Reputation: 2060

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

Upvotes: 68

Sergiu
Sergiu

Reputation: 3195

According to this Github issue might be possible to achieve multiple databases by using bash scripts which you will have to pass in your Dockerfile

EDIT:

To create multiple Databases you could use the following script:

https://github.com/mrts/docker-postgresql-multiple-databases

or

https://github.com/MartinKaburu/docker-postgresql-multiple-databases

Which suggest that you have to clone one of the above git repos and mount it as a volume to: /docker-entrypoint-initdb.d then you would be able to pass multiple database names by using: POSTGRES_MULTIPLE_DATABASES variable

Upvotes: 54

Related Questions