Database containers

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

class testcontainers.mysql.MySqlContainer(image='mysql:latest', MYSQL_USER=None, MYSQL_ROOT_PASSWORD=None, MYSQL_PASSWORD=None, MYSQL_DATABASE=None, **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, driver='psycopg2', **kwargs)

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', **kwargs)

Oracle database container.

Example

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

ElasticSearch container.

Example

with ElasticSearchContainer() as es:
    connection_url = es.get_url()
class testcontainers.mongodb.MongoDbContainer(image: str = 'mongo:latest', port_to_expose: int = 27017, **kwargs)

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', dialect='mssql+pymssql', **kwargs)

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.

class testcontainers.clickhouse.ClickHouseContainer(image='clickhouse/clickhouse-server:latest', port=9000, user=None, password=None, dbname=None)

ClickHouse database container.

Example

The example spins up a ClickHouse database and connects to it using the clickhouse-driver.

with ClickHouseContainer("clickhouse/clickhouse-server:21.8") as clickhouse:
    with clickhouse_driver.Client.from_url(self.get_connection_url()) as client:
        result = client.execute("SELECT version()")
class testcontainers.neo4j.Neo4jContainer(image='neo4j:latest', **kwargs)

Neo4j Graph Database (Standalone) database container.

Example

::
with Neo4jContainer() as neo4j:
with neo4j.get_driver() as driver:
with driver.session() as session:

result = session.run(“MATCH (n) RETURN n LIMIT 1”) record = result.single()

class testcontainers.arangodb.ArangoDbContainer(image: str = 'arangodb:latest', port_to_expose: int = 8529, arango_root_password: str = 'passwd', arango_no_auth: Optional[bool] = None, arango_random_root_password: Optional[bool] = None, **kwargs)

ArangoDB container.

Example

The example will spin up a ArangoDB container. You may use the get_connection_url() method which returns a arangoclient-compatible url in format scheme://host:port. As of now, only a single host is supported (over HTTP).

with ArangoContainer("arangodb:3.9.1") as arango:
    client = ArangoClient(hosts=arango.get_connection_url())

    # Connect
    sys_db = arango_client.db(username="root", password="")

    # Create a new database named "test".
    sys_db.create_database("test")