Deploy a Neo4j standalone server using Docker Compose

You can deploy a Neo4j standalone server using Docker Compose by defining the container configuration in a docker-compose.yml file and authenticating with basic authentication or Docker secrets.

Deploy a Neo4j server using basic authentication mechanism

Before you start, verify that you have installed Docker Compose. For more information, see the Install Docker Compose official documentation.

  1. Create a project folder where you will store your docker-compose.yml file and run your Neo4j server.

  2. Prepare your docker-compose.yml file using the following example. For more information, see the Docker Compose official documentation on Docker Compose specification.

    Example of a docker-compose.yml file
    services:
      neo4j:
        image: neo4j:latest
        volumes:  (1)
            - /$HOME/neo4j/logs:/logs
            - /$HOME/neo4j/config:/config
            - /$HOME/neo4j/data:/data
            - /$HOME/neo4j/plugins:/plugins
        environment:
            - NEO4J_AUTH=neo4j/your_password (2)
        ports:
          - "7474:7474"
          - "7687:7687"
        restart: always
    1 Mount the /$HOME/neo4j/<..>: directories to local directories on your host machine to store logs, configuration, data, and plugins. For more information about mounting volumes, see Persisting data with Docker volumes.
    2 Set the neo4j username and password.
  3. Deploy your Neo4j server by running docker-compose up from your project folder.

    docker-compose up -d

    The -d flag starts the container in detached mode.

Deploy a Neo4j server with Docker secrets

It is advisable not to store sensitive information, such as the database username and password, in the docker-compose.yml file. You can instead store your credentials in files and use them in your docker-compose.yml file without exposing their values.

  1. Create a file, for example, neo4j_auth.txt, containing the username and password for the Neo4j server to be used as a Docker secret.

    neo4j/your_password
  2. Prepare your docker-compose.yml file using the following example. For more information, see the Docker Compose official documentation on Docker Compose specification.

    Example of a docker-compose.yml file
    services:
      neo4j:
        image: neo4j:latest
        volumes: (1)
            - /$HOME/neo4j/logs:/logs
            - /$HOME/neo4j/config:/config
            - /$HOME/neo4j/data:/data
            - /$HOME/neo4j/plugin:/plugins
        environment:
            - NEO4J_AUTH_FILE=/run/secrets/neo4j_auth_file (2)
        ports:
          - "7474:7474"
          - "7687:7687"
        restart: always
        secrets:
          - neo4j_auth_file (3)
    secrets:
      neo4j_auth_file: (4)
        file: ./neo4j_auth.txt (5)
    1 Mount the /$HOME/neo4j/<..>: directories to local directories on your host machine to store logs, configuration, data, and plugins.
    2 Path to the secret (neo4j_auth_file) containing the neo4j username and password. The secret value is read from the file specified in the file attribute of the neo4j_auth_file secret. Multiple secrets can be defined in the secrets section of the neo4j service. Secrets only support environment variables starting with NEO4J_ and ending with _FILE.
    3 The name of the secret, for example neo4j_auth_file.
    4 Path to the neo4j_auth.txt file.
    5 The name of the secret in the neo4j service.

    The secret value overrides the equivalent environment variable if they are both defined. So, for example, if you also define an environment variable NEO4J_AUTH=neo4j/your_other_password in the environment section of the neo4j service, the value of NEO4J_AUTH_FILE will be the one used.

  3. Deploy your Neo4j server by running docker-compose up from your project folder.

    docker-compose up -d

    The -d flag starts the container in detached mode.