How to Manage Memgraph Docker Instances in Python

How to Manage Memgraph Docker Instances in Python

Ivan Despot

When developing graph-based applications, it can become hard to manage different database server instances. Using the new instance_runner module, you will learn how to start, stop, connect to and monitor Memgraph instances with GQLAlchemy directly from your Python scripts.

First, perform all the necessary imports:

from gqlalchemy.instance_runner import (

Start the Memgraph instance

The following code will create a Memgraph instance, start it and return a connection object:

memgraph_instance = MemgraphInstanceDocker(
    docker_image=DockerImage.MEMGRAPH, docker_image_tag="latest", host="", port=7687
memgraph = memgraph_instance.start_and_connect(restart=False)

We used the default values for the arguments:

  • docker_image=DockerImage.MEMGRAPH: This will start the memgraph/memgraph Docker image.
  • docker_image_tag="latest": We use the latest tag to start the most recent version of Memgraph.
  • host="": This is the wildcard address which indicates that the instance should accept connections from all interfaces.
  • port=7687: This is the default port Memgraph listens to.
  • restart=False: If the instance is already running, it won’t be stopped and started again.

After we have created the connection, we can start querying the database:

memgraph.execute_and_fetch("RETURN 'Memgraph is running' AS result"))[0]["result"]

Pass configuration flags

You can pass configuration flags using a dictionary:

config={"--log-level": "TRACE"}
memgraph_instance = MemgraphInstanceDocker(config=config)

Stop the Memgraph instance

To stop a Memgraph instance, call the stop() method:


Check if a Memgraph instance is running

To check if a Memgraph instance is running, call the is_running() method:


Where to next?

Hopefully, this guide has taught you how to manage Memgraph Docker instances. If you have any more questions, join our community and ping us on Discord.

In this article
Sign up for our Newsletter

Get a personalized Memgraph demo
and get your questions answered.

Read next

In-Memory Databases That Work Great With Python

An in-memory database is a database that is kept in the main memory (RAM) of a computer and controlled by an in-memory database management system. When analyzing information in an in-memory database, only the RAM is used.

April 7, 2023
Dev Tools
Under the Hood
Memgraph MAGE
Synchronize Data Between Memgraph Graph Database and Elasticsearch

If you need to reap the benefits of both a graph database and Elasticsearch, the new module in Memgraph’s graph library MAGE enables you to easily synchronize those two components using triggers.

Andi Skrgat
February 3, 2023
Learn Graph Analytics With Python

With the Introduction to graph analytics with Python course, you will learn all about graphs and how to analyze them. Check out the overview of the graph analytics tools landscape and engaging examples to find out how to use the most powerful network analysis Python tools.

Katarina Supe
December 12, 2022