Created
March 14, 2021 14:33
-
-
Save d3vAdv3ntur3s/38c51019e41944a347c772b3326888ed to your computer and use it in GitHub Desktop.
Example docker-compose with initialise script mount, network, healthcheck and volume
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Exit immediately, do not continue running commands after error | |
set -o errexit | |
##################################### NOTE ############################################ | |
# Anything copied into the /docker-entrypoint-initdb.d will be executed automatically # | |
# in alphanumeric order, can be useful 1_setup.sql, 2_data.sql # | |
# This script will work for dockerised or system installed Postgres # | |
####################################################################################### | |
# Common configuration properties | |
psqluser=${POSTGRES_USER:-contact_store} | |
psqlpasswd=${POSTGRES_PASSWORD:-contact_store} | |
psqldb=${POSTGRES_DB:-contact_store} | |
psqlschema=contact_store | |
## Create user | |
#createuser $psqluser && echo "CREATE ROLE" | |
psql -U $psqluser -c "SELECT 1 FROM pg_user WHERE usename = '$psqluser'" | grep -q 1 \ | |
|| psql -U postgres -c "CREATE ROLE $psqluser LOGIN PASSWORD $psqlpasswd;" | |
# Create database and user as owner | |
psql -U $psqluser -c "SELECT 1 FROM pg_database WHERE datname = '$psqldb'" | grep -q 1 || psql -U $psqluser -c "CREATE DATABASE $psqldb" | |
echo "DATABASE $psqldb" | |
# Grant all privileges for user on the database | |
psql -U "$psqluser" -c "GRANT ALL PRIVILEGES ON DATABASE $psqldb TO $psqluser;" | |
echo "Granted all privileges on $psqldb for $psqluser" | |
## Create schema under the database | |
psql -U "$psqluser" -c "CREATE SCHEMA IF NOT EXISTS $psqlschema AUTHORIZATION $psqluser;" | |
echo "SCHEMA $psqlschema authorised by $psqluser" | |
## Create extension uuid-ossp | |
psql -U "$psqluser" -c "CREATE EXTENSION IF NOT EXISTS \"pgcrypto\" with SCHEMA $psqlschema;" | |
echo "EXTENSION created pgcrypto for SCHEMA $psqlschema" | |
## Alter user to add a password | |
#psql "$psqldb" -c "ALTER USER $psqluser WITH PASSWORD '$psqlpasswd';" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: '3.8' | |
services: | |
db: | |
container_name: contact_store_db | |
image: postgres:12 | |
restart: always | |
environment: | |
POSTGRES_USER: contact_store | |
POSTGRES_PASSWORD: contact_store | |
POSTGRES_DB: contact_store | |
# volume pg_data - data remains after shutting down the container | |
volumes: | |
- ./gradle/script/:/docker-entrypoint-initdb.d/ | |
- pg_data:/var/lib/postgresql/data/ | |
ports: | |
- "5433:5432" | |
networks: | |
- backend | |
healthcheck: | |
test: pg_isready -U contact_store | |
interval: 1m | |
timeout: 15s | |
retries: 2 | |
app: | |
image: contact-store:0.0.1 | |
container_name: app | |
depends_on: | |
- db | |
environment: | |
- SPRING_PROFILES_ACTIVE=local | |
- SPRING_DATASOURCE_URL=r2dbc:pool:postgresql://db:5432/contact_store | |
- SPRING_DATASOURCE_USERNAME=contact_store | |
- SPRING_DATASOURCE_PASSWORD=contact_store | |
- JAVA_TOOL_OPTIONS=-agentlib:jdwp=transport=dt_socket,address=5005,server=y,suspend=n | |
#HTTP, HTTPS, DEBUG | |
ports: | |
- "8080:8080" | |
- "8443:8443" | |
- "5005:5005" | |
networks: | |
- backend | |
volumes: | |
pg_data: | |
networks: | |
backend: | |
driver: bridge |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment