My skill in Docker is little. I am using Docker desktop for Windows version 4.9.1 (81317) (latest version at this time). My web-app is multi-tenant system, need 3 databases. This is my docker-compose.yml
(run 1 database ok)
version: '2.1'
services:
postgres:
image: postgres
ports:
- "5433:5432"
restart: always
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: acc_spring
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres -d acc_spring"]
interval: 10s
timeout: 5s
retries: 3
Please guide me how to write/revise the above docker-composite.yml
for running 3 databases at the same time:
tenant_master
: port5432
, database name:tenant_master
, usernamepostgres
, passwordpostgres
.tenant_1
: port5432
, database name:tenant_1
, usernamepostgres
, passwordpostgres
.tenant_2
: port5432
, database name:tenant_2
, usernamepostgres
, passwordpostgres
.
Notice: 3 databases in a database instance/server.
Please also explain for me what/how/ when to use restart: always
, Do I have another option(s) for always
?
CodePudding user response:
Besides the port mapping (5433
seems wrong) you shouldn't need to change anything (well - probably the admin password shouldn't be 'postgres' in production).
You need to connect to this instance and create your databases (SQL or some app) and users. This is not related to Docker or docker compose
. POSTGRES_DB
is just the default database (not THE database).
However, if you want to do this as part of your container setup, you could do it like follows. Create a new project directory with these files:
File create-databases.sh
#!/bin/bash
set -eu
function create_database() {
local database=$1
echo " Creating database '$database'"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
CREATE DATABASE $database;
GRANT ALL PRIVILEGES ON DATABASE $database TO $POSTGRES_USER;
EOSQL
}
create_database $POSTGRES_DB2
create_database $POSTGRES_DB3
File docker-compose.yml
services:
postgres:
image: postgres:latest
container_name: postgres
ports:
- "5433:5432"
restart: unless-stopped
volumes:
- ./create-databases.sh:/docker-entrypoint-initdb.d/create-databases.sh
- ./db_persist:/var/lib/postgresql/data
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: tenant_master
POSTGRES_DB2: tenant_1
POSTGRES_DB3: tenant_2
While this is not best practice (I'm not a Postgres user) it will get you started. By using volumes, an init script is put into a folder where it is picked up by container initialization and the database is persisted, just in case. I left out the health-check for better clarity. It can be re-added of course.
Having said this, there would be another solution: just copy the postgres service (postgres2, postgres3) to be used with different ports (5434, 5435). Although this would run 3 independent database instances (needs more ressources), it might have its advantages as well.
As regards the restart policy: instead of "always" you might consider "unless-stopped" (see https://docs.docker.com/engine/reference/run/#restart-policies---restart) like it is done in my suggestion above.