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 features are implemented but not tested:
- Enable/Disable error if already created
- startdb
- restartdb
- init scripts to examples
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:
pymongofor MongoDBpyodbcfor Microsoft SQL Serverpsycopg2for PostgreSQLmysql-connector-pythonfor 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file py_dockerdb-0.4.0.tar.gz.
File metadata
- Download URL: py_dockerdb-0.4.0.tar.gz
- Upload date:
- Size: 27.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5e182e4ea11905de62191fcd279a4c7d1002a5e92f8cf8f722b676060f39ea03
|
|
| MD5 |
38a0136f10ba5682d532a44211082a88
|
|
| BLAKE2b-256 |
af3a6a8bd0fd85ee020f56cbce6cef84ef3d4e21937bed7f9ce6c1e4103a13c9
|
File details
Details for the file py_dockerdb-0.4.0-py3-none-any.whl.
File metadata
- Download URL: py_dockerdb-0.4.0-py3-none-any.whl
- Upload date:
- Size: 25.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6e2869838c4055dabfbbe5213860c3ec51a4a50f44f2a618f6cac68acb081b9e
|
|
| MD5 |
5489cfacd3381d54322610768254e04b
|
|
| BLAKE2b-256 |
46a533747b1381e5e6a55e593ff02fbdcbb969d4f5ac95123f554887ab9cd9fb
|