How to manage Memgraph Docker instances in Python

Ivan Despot
How to manage Memgraph Docker instances in Python

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.

Table of Contents

Get a personalized Memgraph demo
and get your questions answered.

Continue Reading

Use Cases
Fraud Detection
Embark on the Fraud Detection Journey by Importing Data Into Memgraph With Python

Are you reluctant to switch from a relational database to a graph databases to explore fraud because you believe you first need to be proficient in Cypher to correctly import the data? Be rest assured - there is a Python-friendly approach available within Memgraph!

Bruno Sacaric
November 16, 2022
NetworkX Developers, Say Farewell to the Boilerplate Code

If you are spending more time writing code to develop, deploy and manage your graph projects, it’s time you tried Memgraph. It will allow you to focus on the data analysis and free you from all that time-consuming coding.

Katarina Supe
November 14, 2022
Who ranks better? Memgraph vs NetworkX PageRank

Are your NetworkX algorithms taking even more and more time to produce the results you need to finish up your research? Or the application reached a critical point and its starting to lag due to increase in data analysis? Could Memgraph tackle the same computations in less time? I think you probably know the answer is “Doh!” but here are the numbers to prove it.

Katarina Supe
November 8, 2022