How to create Neo4j database using docker and docker compose

作者: Calpa Liu
字數:2197
出版日期:August 17, 2022
更新日期:August 17, 2022

We can use the following command to start the Neo4j Container.

We can use the following command to start the Neo4j Container.

docker run \
    --name neo4j \
    -p 7474:7474 -p 7687:7687 \
    -d \
    -v $HOME/neo4j/data:/data \
    -v $HOME/neo4j/logs:/logs \
    -v $HOME/neo4j/import:/var/lib/neo4j/import \
    -v $HOME/neo4j/plugins:/plugins \
    --env NEO4J_AUTH=neo4j/test \
    neo4j:4.4.9-community

And the image is started smoothly in MacOS M1.

The list of Neo4j docker images are available in: https://hub.docker.com/_/neo4j/tags

Docker Compose

version: '3'

services:
  neo4j:
    image: neo4j:4.4.9-community
    network_mode: "bridge"
    ports:
      - "7474:7474"
      - "7687:7687"
    volumes:
      - $HOME/neo4j/data:/data
      - $HOME/neo4j/logs:/logs
      - $HOME/neo4j/import:/var/lib/neo4j/import
      - $HOME/neo4j/plugins:/plugins
    environment:
      - NEO4J_AUTH=neo4j/test

We can also starts the neo4j service by using the following command:

docker-compose up

To run the service in background, we can pass the -d flag (detached mode)

docker-compose up -d

You may also change the location of the database to current folder by changing the volume.

version: '3'

services:
  neo4j:
    image: neo4j:4.4.9-community
    network_mode: "bridge"
    ports:
      - "7474:7474"
      - "7687:7687"
    volumes:
      - ./neo4j/data:/data
      - ./neo4j/logs:/logs
      - ./neo4j/import:/var/lib/neo4j/import
      - ./neo4j/plugins:/plugins
    environment:
      - NEO4J_AUTH=neo4j/test

Neo4j Browser

We can run the following cypher query in http://localhost:7474/browser/ to check if we have an empty database.

MATCH (m)
RETURN m

To create a node, we can use the following cypher query:

CREATE (n:Node)
RETURN n

And one node is created.

Now when we query all nodes, we can see the same result.

MATCH (m)
RETURN m

Reference

  1. How-To: Run Neo4j in Docker
感謝您閱讀我的文章。歡迎隨時分享你的想法。
關於 Calpa

Calpa 擅長使用 TypeScript、React.js 和 Vue.js 開發Responsive Web Design網站。

此外,Calpa 積極參與香港和台灣的開源社區,曾在2019年的香港開源大會上擔任講者,提供工作經驗和見解。此外,他也在 GitHub 上公開分享個人博客程式碼,已獲得超過300顆星星和60個分支的支持。

更多前端開發技術文章:傳送門