Skip to main content

Python package for working with databases in Docker containers

Project description

Docker-DB

A Python library for managing containerized databases through Docker. This library provides a simple interface for creating, configuring, and managing database containers for development and testing environments, from within python.

Features

The following features are supported:

  • Easy setup and management of database containers
  • Automated container lifecycle management
  • Standardized configuration interfaces
  • Connection management and health checking
  • Volume persistence capabilities
  • Initialization script support

The following databases are supported:

  • MongoDB
  • Microsoft SQL Server
  • PostgreSQL
  • MySQL

These databases might be added in the future:

  • Cassandra

Installation

pip install py-dockerdb

Requirements

  • Python 3.7+
  • Docker installed and running
  • Database-specific Python drivers:
    • pymongo for MongoDB
    • pyodbc for Microsoft SQL Server
    • psycopg2 for PostgreSQL
    • mysql-connector-python for MySQL

Basic Usage

Check out the following usage examples:

MongoDB Example

from docker_db.mongodb import MongoDBConfig, MongoDB

# Create configuration
config = MongoDBConfig(
    user="testuser",
    password="testpass",
    database="testdb",
    root_username="admin",
    root_password="adminpass",
    container_name="test-mongodb"
)

# Create and start MongoDB container
db = MongoDB(config)
db.create_db()

# Connect to database
client = db.connection
# Use the database...

# Stop the container when done
db.stop_db()

# Completely remove the container
db.delete_db()

PostgreSQL Example

from docker_db.postgres import PostgresConfig, PostgresDB

# Create configuration
config = PostgresConfig(
    user="testuser",
    password="testpass",
    database="testdb",
    container_name="test-postgres"
)

# Create and start PostgreSQL container
db = PostgresDB(config)
db.create_db()

# Connect to the database
conn = db.connection

# Create a cursor and execute a query
cursor = conn.cursor()
cursor.execute("SELECT version();")
version = cursor.fetchone()
print(f"PostgreSQL version: {version[0]}")

# Clean up
cursor.close()
db.stop_db()

Working with Initialization Scripts

All database managers support initialization scripts that can be executed when the container is created:

from pathlib import Path
from docker_db.mongodb import MongoDBConfig, MongoDB

config = MongoDBConfig(
    user="testuser",
    password="testpass",
    database="testdb",
    root_username="admin",
    root_password="adminpass",
    container_name="test-mongodb",
    init_script=Path("./path/to/init_script.js")
)

Custom Volume Paths

You can specify custom volume paths to persist data between container restarts:

from pathlib import Path
from docker_db.mssql import MSSQLConfig, MSSQLDB

config = MSSQLConfig(
    user="testuser",
    password="testpass",
    database="testdb",
    sa_password="StrongPassword123!",
    container_name="test-mssql",
    volume_path=Path("./data/mssql")
)

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_dockerdb-0.2.0.tar.gz (26.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_dockerdb-0.2.0-py3-none-any.whl (26.5 kB view details)

Uploaded Python 3

File details

Details for the file py_dockerdb-0.2.0.tar.gz.

File metadata

  • Download URL: py_dockerdb-0.2.0.tar.gz
  • Upload date:
  • Size: 26.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for py_dockerdb-0.2.0.tar.gz
Algorithm Hash digest
SHA256 3ce2556bcb4443430eed7acaa1585e17924daa15a177697fb1a435aff9f2466c
MD5 3a05649e56ec1c56d204baabe5843b37
BLAKE2b-256 981ad60a41e41db8a6a94b17bcca643cd9a44c119cee0df459ed7933bb594baf

See more details on using hashes here.

File details

Details for the file py_dockerdb-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: py_dockerdb-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 26.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for py_dockerdb-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b3f764b295536489f66120e2c521c6316e0e8f44d05c8ce439e7b91898e0deff
MD5 f9d9929bfcac441d278de7abc9207419
BLAKE2b-256 dd54fdd55122d7cbfe86448c1607280f8b93cfb45c9f79c574da0bb86284ffb5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page