Sign inSign up

imansadatii/python-base

By imansadatii

Updated over 1 year ago

a ready-to-use python image for django projects. contains a scripts to checking postgres readiness

Image
Languages & frameworks
Developer tools
1

178

imansadatii/python-base repository overview

Python Service Base Image

A lightweight base image for Python microservices and monolithic, built on python:3.12-slim-bullseye, with shared tools and configurations for consistency and efficiency.

Features

  • Pre-configured environment variables:
    • PYTHONDONTWRITEBYTECODE=1: Disables .pyc files.
    • PYTHONUNBUFFERED=1: Enables unbuffered logging.
  • Lightweight and extendable for microservices.
  • Includes check_postgres.sh for checks postgres readiness. you can use this ready script in your own Dockerfile by adding CMD ["check_postgres.sh"].

Dockerfile

FROM python:3.12-slim-bullseye
LABEL authors="[email protected]"

ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1

COPY /check_postgres.sh /usr/local/bin/check_postgres.sh

RUN apt-get update && \
 apt-get install -y postgresql-client && \
 pip install --upgrade pip && \
 apt-get install -y build-essential libpq-dev && \
 pip install psycopg2-binary && \
 chmod +x /usr/local/bin/check_postgres.sh 

CMD ["python"]

Script

That one script i included:

#!/bin/bash

set -o errexit
set -o pipefail
set -o nounset

postgres_ready() {
 python <<END
import sys
import os
import psycopg2
try:
 conn = psycopg2.connect(
     dbname=os.environ["DB_NAME"],
     user=os.environ["DB_USER"],
     password=os.environ["DB_PASSWORD"],
     host=os.environ["DB_HOST"],
     port=int(os.environ["DB_PORT"]),
 )
 conn.close()
except psycopg2.OperationalError as e:
 print(f"Postgres connection error: {e}")
 sys.exit(-1)
sys.exit(0)
END
}

until postgres_ready; do
 echo "Waiting for PostgreSQL (${DB_HOST}:${DB_PORT}) to become available..."
 sleep 3
done

echo >&2 "PostgreSQL is available!"

exec "$@"

Usage

Pull the Image:

docker pull imansadatii/python-base:1.1

Tag summary

Content type

Image

Digest

sha256:28da0989a

Size

159.3 MB

Last updated

over 1 year ago

docker pull imansadatii/python-base:1.1