This is the sixth day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Docker Compose: Docker Compose: Docker Compose: Docker Compose: Docker Compose: Docker Compose: Docker Compose: Docker Compose

Docker Compose Docker Compose Docker Compose

First create a folder to store our files:

mkdir ~/postgres-demo && cd ~/postgres-demo
Copy the code

Then create a docker-comemage.yml file:

touch docker-compose.yml
Copy the code

Add the following to the file:

Version: '3' services: postgres: image: postgres:13.1 HealthCheck: test: [ "CMD", "pg_isready", "-q", "-d", "postgres", "-U", "root" ] timeout: 45s interval: 10s retries: 10 restart: always environment: - POSTGRES_USER=root - POSTGRES_PASSWORD=password - APP_DB_USER=docker - APP_DB_PASS=docker - APP_DB_NAME=docker volumes: - ./db:/docker-entrypoint-initdb.d/ ports: - 5432:5432Copy the code
  • imageSpecifies which Docker image to use, including the version number
  • healthcheckTo ensure that Postgres is running before other services that depend on it can run
  • restartA reboot always ensures that DB is started at system startup

The following environment variables are used to assign the username and password for the Postgres primary database:

  • POSTGRES_USERThe user
  • POSTGRES_PASSWORDpassword

The init script creates database users and database vendors using the following environment variables:

  • APP_DB_USERApplication database
  • APP_DB_PASSThe application database passes
  • APP_DB_NAMEApplication database name

For the volume option, we will map the local folder named DB in /docker-entrypoint-initdb.d/ to the folder in the container, which is where we will place the database init script in the next step.

We also expose the Postgres port to our server by assigning it to the port option, which will allow us to connect to the database from our local machine.

Create database init script

Create a folder named DB to store the init script.

mkdir db
Copy the code

Create a script named 01-init.sh.

touch db/01-init.sh
Copy the code

Add the following:

#! /bin/bashset -e
export PGPASSWORD=$POSTGRES_PASSWORD;
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
  CREATE USER $APP_DB_USER WITH PASSWORD '$APP_DB_PASS';
  CREATE DATABASE $APP_DB_NAME;
  GRANT ALL PRIVILEGES ON DATABASE $APP_DB_NAME TO $APP_DB_USER;
  \connect $APP_DB_NAME $APP_DB_USER
  BEGIN;
    CREATE TABLE IF NOT EXISTS event (
	  id CHAR(26) NOT NULL CHECK (CHAR_LENGTH(id) = 26) PRIMARY KEY,
	  aggregate_id CHAR(26) NOT NULL CHECK (CHAR_LENGTH(aggregate_id) = 26),
	  event_data JSON NOT NULL.version INT.UNIQUE(aggregate_id, version));CREATE INDEX idx_event_aggregate_id ON event (aggregate_id);
  COMMIT;
EOSQL
Copy the code

This script will:

  • Creates a new user whose name is assigned toapp_db_user, password assigned toapp_db_pass
  • With assigned toAPP_db_nameCreates a database with any name of
  • Grant all permissions to users on the database
  • Connect to the database and create a database namedeventThe table of

Three, the docker – compose up

Run Docker Compose to start the Postgres database and run the database init script.

docker-compose up
Copy the code

Boy, haven’t you seen enough? Click on the stone’s home page and take a look at it casually. Maybe there will be a surprise? Welcome to support the likes/attention/comments, your support is my biggest motivation, thank you!