| files | ||
| .gitignore | ||
| Dockerfile | ||
| LICENSE | ||
| README.md | ||
PostgreSQL Docker Container
A Dockerized PostgreSQL database setup using Alpine Linux, designed for easy deployment and customization.
Overview
This repository contains the necessary files to build a PostgreSQL Docker container. The container is pre-configured to initialize databases, users, and execute custom scripts on startup. It uses the lightweight Alpine Linux base image for efficiency.
Prerequisites
- Docker installed on your system
- Access to the base image
git.esculta.es/aesculta/alpine-base(or modify theDockerfileto use a public base image)
Getting Started
Run the container
docker run -d \
--name my-postgres \
-e POSTGRES_PASSWORD=mysecretpassword \
-e POSTGRES_USER=customuser \
-e POSTGRES_DB=mydatabase \
-v postgres_data:/var/lib/postgresql/data \
-p 5432:5432 \
git.esculta.es/aesculta/postgres:latest
Environment Variables
Configure the container using these environment variables:
-
POSTGRES_USER (default: postgres): Database superuser name
-
POSTGRES_DB (default: same as POSTGRES_USER): Default database name
-
POSTGRES_PASSWORD: Password for the superuser (recommended for production)
Warning: If no password is set, the container will use trust authentication (insecure).
Initialization Scripts
Place SQL or Shell scripts in /docker-entrypoint-initdb.d to execute them during initialization:
-
Create a files directory
-
Add your scripts:
-
.sql files will be executed by psql
-
.sh files will be sourced in the shell
-
Rebuild the Docker image
Example structure:
/files
/docker-entrypoint-initdb.d
init.sql
setup.sh
Persistent Data
Data is stored in the Docker volume mounted at /var/lib/postgresql/data. To persist data between container restarts:
# Create a named volume
docker volume create postgres_data
# Use the volume in your container
docker run ... -v postgres_data:/var/lib/postgresql/data ...
Security Considerations
-
Always set POSTGRES_PASSWORD in production environments
-
The default configuration allows remote connections (
host all all 0.0.0.0/0) -
For production use:
-
Consider customizing pg_hba.conf
-
Use proper TLS certificates
-
Restrict network access
Exposing the Service
The container exposes PostgreSQL on port 5432. Map to host port using:
-p [HOST_PORT]:5432
Build the Docker Image
docker build -t my-postgres .