diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml new file mode 100644 index 0000000..5e6114a --- /dev/null +++ b/.github/workflows/docker-image.yml @@ -0,0 +1,43 @@ +name: Create and publish a Docker image + +on: + workflow_dispatch: + push: + branches: [ "latest" ] + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + build-and-push-image: + runs-on: ubuntu-latest + + permissions: + contents: read + packages: write + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Log in to the Container registry + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + - name: Build and push Docker image + uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/Dockerfile b/Dockerfile index ee9c3d0..7f75bf5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,2 +1,4 @@ -FROM postgres:9.6 +FROM postgres:14 COPY create-multiple-postgresql-databases.sh /docker-entrypoint-initdb.d/ + +LABEL org.opencontainers.image.source="https://github.com/captmicr0/docker-postgresql-multiple-databases" diff --git a/README.md b/README.md index d4b6a15..bd44a78 100644 --- a/README.md +++ b/README.md @@ -10,48 +10,21 @@ entrypoint calls `initdb` to create the default `postgres` user and database, it will run any `*.sql` files and source any `*.sh` scripts found in that directory to do further initialization before starting the service.* -This directory contains a script to create multiple databases using that -mechanism. - -## Usage - -### By mounting a volume - -Clone the repository, mount its directory as a volume into -`/docker-entrypoint-initdb.d` and declare database names separated by commas in -`POSTGRES_MULTIPLE_DATABASES` environment variable as follows -(`docker-compose` syntax): +## Usage (docker-compose) myapp-postgresql: - image: postgres:9.6.2 + image: ghcr.io/captmicr0/docker-postgresql-multiple-databases:latest volumes: - ../docker-postgresql-multiple-databases:/docker-entrypoint-initdb.d environment: - - POSTGRES_MULTIPLE_DATABASES: db1,db2 - - POSTGRES_USER: myapp - - POSTGRES_PASSWORD: - -### By building a custom image - -Clone the repository, build and push the image to your Docker repository, -for example for Google Private Repository do the following: - - docker build --tag=eu.gcr.io/your-project/postgres-multi-db . - gcloud docker -- push eu.gcr.io/your-project/postgres-multi-db - -You still need to pass the `POSTGRES_MULTIPLE_DATABASES` environment variable -to the container: - - myapp-postgresql: - image: eu.gcr.io/your-project/postgres-multi-db - environment: - - POSTGRES_MULTIPLE_DATABASES: db1,db2 - - POSTGRES_USER: myapp - - POSTGRES_PASSWORD: + - POSTGRES_MULTIPLE_DATABASES=db1,db2 + - POSTGRES_USER=myapp + - POSTGRES_PASSWORD=mypassword ### Non-standard database names -If you need to use non-standard database names (hyphens, uppercase letters etc), quote them in `POSTGRES_MULTIPLE_DATABASES`: +If you need to use non-standard database names (hyphens, uppercase letters etc), no quotes are needed with this fork: environment: - - POSTGRES_MULTIPLE_DATABASES: "test-db-1","test-db-2" + - POSTGRES_MULTIPLE_DATABASES=radarr-main,radarr-log,sonarr-main,sonarr-log,bazarr,readarr-audiobook-main,readarr-audiobook-log,readarr-audiobook-cache,readarr-ebook-main,readarr-ebook-log,readarr-ebook-cache,prowlarr-main,prowlarr-log,jellyseerr + diff --git a/create-multiple-postgresql-databases.sh b/create-multiple-postgresql-databases.sh index aa665fa..fa2a3f5 100755 --- a/create-multiple-postgresql-databases.sh +++ b/create-multiple-postgresql-databases.sh @@ -4,19 +4,20 @@ set -e set -u function create_user_and_database() { - local database=$1 - echo " Creating user and database '$database'" - psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL - CREATE USER $database; - CREATE DATABASE $database; - GRANT ALL PRIVILEGES ON DATABASE $database TO $database; + local database=$1 + echo " Creating user and database '$database'" + psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL + CREATE USER "$database"; + CREATE DATABASE "$database"; + GRANT ALL PRIVILEGES ON DATABASE "$database" TO "$database"; + GRANT ALL PRIVILEGES ON DATABASE "$database" TO $POSTGRES_USER; EOSQL } if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then - echo "Multiple database creation requested: $POSTGRES_MULTIPLE_DATABASES" - for db in $(echo $POSTGRES_MULTIPLE_DATABASES | tr ',' ' '); do - create_user_and_database $db - done - echo "Multiple databases created" + echo "Multiple database creation requested: $POSTGRES_MULTIPLE_DATABASES" + for db in $(echo $POSTGRES_MULTIPLE_DATABASES | tr ',' ' '); do + create_user_and_database $db + done + echo "Multiple databases created" fi