Provisioning Postgresql Service the Docker-way
This post is an attempt to collect all the oh-I-didn't-know-that tidbits pertaining to the installation of Postgresql, as a docker container. Most are from Stack Overflow answers, some are my own improv (the Linux stuff).
1. ".env" - File for all environment variables
echo -e postgres | xargs -I{} echo curl -L -s 'https://registry.hub.docker.com/v2/repositories/library/{}/tags?page_size=100' | bash | jq '."results"[]["name"]' | grep alpine.$ | head -n 2 | awk '{gsub(/"/,"")} END{print}' | read TAG
PASSWORD=$(echo -n "md5"; echo -n "postgres" | md5sum | awk '{print $1}')
USER=postgres
DB=postgres
cat << EOF > .env
TAG=$TAG
POSTGRES_PASSWORD=$PASSWORD
POSTGRES_USER=$USER
POSTGRES_DB=$DB
EOF
2. postgresql.conf - Postgresql configuration file
curl -o postgresql.conf https://gist.githubusercontent.com/deepdev1/8ad577c5363d606d794267cc0a119d2b/raw/87c216b6da399b495a7f1ace3723d12f3787d490/postgresql.conf
3. "docker-compose.yml" - File with container instance configurations
x-restart-policy: &restart-policy
restart: unless-stopped
x-log-policy: &log-policy
logging:
driver: "json-file"
options:
max-file: "2"
max-size: "5m"
services:
postgres:
image: postgres:${TAG:-16-alpine}
environment:
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-postgres}
- POSTGRES_USER=${PASSWORD_USER:-postgres}
- POSTGRES_DB=${POSTGRES_DB:-postgres}
- TZ=Asia/Kolkata
ports:
- 5432:5432
volumes:
- ./postgresql.conf:/etc/postgresql.conf
- postgres-data:/var/lib/postgresql/data
#- ./logs:/logs
#command: "postgres -c config_file=/etc/postgresql.conf -c logging_collector=on -c log_destination=stderr -c log_directory=/logs"
command: "postgres -c config_file=/etc/postgresql.conf"
healthcheck:
test: ["CMD-SHELL", "pg_isready"]
interval: 10s
timeout: 5s
retries: 5
container_name: postgres
<<: *restart-policy
<<: *log-policy
volumes:
postgres-data:
4. Provision the service
docker compose up -d && \
docker logs -f postgres