Database containers

Allows to spin up database images such as MySQL, PostgreSQL, MariaDB, Oracle XE, MongoDb or Neo4j.

class testcontainers.mysql.MySqlContainer(image='mysql:latest', **kwargs)

MySql database container.

Example

The example will spin up a MySql database to which you can connect with the credentials passed in the constructor. Alternatively, you may use the get_connection_url() method which returns a sqlalchemy-compatible url in format dialect+driver://username:password@host:port/database.

with MySqlContainer('mysql:5.7.17') as mysql:
    e = sqlalchemy.create_engine(mysql.get_connection_url())
    result = e.execute("select version()")
    version, = result.fetchone()
class testcontainers.mysql.MariaDbContainer(image='mariadb:latest', **kwargs)

Maria database container, a commercially-supported fork of MySql.

Example

with MariaDbContainer("mariadb:latest") as mariadb:
    e = sqlalchemy.create_engine(mariadb.get_connection_url())
    result = e.execute("select version()")
class testcontainers.postgres.PostgresContainer(image='postgres:latest', port=5432, user=None, password=None, dbname=None)

Postgres database container.

Example

The example spins up a Postgres database and connects to it using the psycopg driver.

with PostgresContainer("postgres:9.5") as postgres:
    e = sqlalchemy.create_engine(postgres.get_connection_url())
    result = e.execute("select version()")
class testcontainers.oracle.OracleDbContainer(image='wnameless/oracle-xe-11g-r2:latest')

Oracle database container.

Example

with OracleDbContainer():
    e = sqlalchemy.create_engine(oracle.get_connection_url())
    result = e.execute("select 1 from dual")
class testcontainers.elasticsearch.ElasticSearchContainer(image='elasticsearch:7.5.0', port_to_expose=9200)

ElasticSearch container.

Example

with ElasticSearchContainer() as es:
    connection_url = es.get_url()

Deprecated since version Use: ElasticSearchContainer with a capital S instead of ElasticsearchContainer.

class testcontainers.mongodb.MongoDbContainer(image='mongo:latest')

Mongo document-based database container.

Example

with MongoDbContainer("mongo:latest") as mongo:
    db = mongo.get_connection_client().test
    # Insert a database entry
    result = db.restaurants.insert_one(
        {
            "address": {
                "street": "2 Avenue",
                "zipcode": "10075",
                "building": "1480",
                "coord": [-73.9557413, 40.7720266]
            },
            "borough": "Manhattan",
            "cuisine": "Italian",
            "name": "Vella",
            "restaurant_id": "41704620"
        }
    )
    # Find the restaurant document
    cursor = db.restaurants.find({"borough": "Manhattan"})
    for document in cursor:
        # Do something interesting with the document
class testcontainers.mssql.SqlServerContainer(image='mcr.microsoft.com/mssql/server:2019-latest', user='SA', password=None, port=1433, dbname='tempdb', driver='ODBC Driver 17 for SQL Server')

Microsoft Sql Server database container.

Example

with SqlServerContainer() as mssql:
    e = sqlalchemy.create_engine(mssql.get_connection_url())
    result = e.execute("select @@VERSION")

Notes

Requires ODBC Driver 17 for SQL Server.