Database containers

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

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

MySql database container.


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.


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.


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'wnameless/oracle-xe-11g-r2:latest', **kwargs)

Oracle database container.


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.


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

Mongo document-based database container.


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='', user='SA', password=None, port=1433, dbname='tempdb', dialect='mssql+pymssql', **kwargs)

Microsoft Sql Server database container.


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


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.


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.


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

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