DEV Community

loading...

Part 2 - Graph Databases: The Perfect Choice for Front-end Developers - Run Dgraph in Docker

Samuel Earl
・3 min read

Before I get into this tutorial, I assume that you are somewhat familiar with GraphQL. Dgraph's query language is called GraphQL+- and is (obviously) based on GraphQL. I will try to explain things as clearly as possible, but certainly having some familiarity with GraphQL would be helpful.

Run Dgraph in a Docker container

Install Docker for your operating system.

Create a project directory where all of your files and data for your project will be stored. My directory will be called dgraph-demo. This is the file path to my dgraph-demo directory: ~/code/web-development/dgraph-demo/. You should replace that file path with your file path to your dgraph-demo directory.

Then run the following command, which will pull the dgraph/standalone:latest image from Docker Hub and run Dgraph in a Docker container:

docker run -it --name dgraph-demo -p 8000:8000 -p 8080:8080 -p 9080:9080 -v ~/code/web-development/dgraph-demo/dgraph:/dgraph dgraph/standalone:latest

Let me explain the syntax of the previous command with this reference example and the notes that follow:

docker run -it --name <container_name> -p 8000:8000 -p 8080:8080 -p 9080:9080 -v ~/<path>/<to>/<project-directory>/dgraph:/dgraph dgraph/standalone:latest

This command does a few things:

  1. --name <container-name>: This creates and runs a container with the name that you specified (e.g. dgraph-demo).
  2. -p 8000:8000 -p 8080:8080 -p 9080:9080: This exposes a few ports:
    1. Port 8000 is used for Ratel (the Dgraph web UI).
    2. Port 8080 is used for the REST API.
    3. Port 9080 is used for the gRPC API.
  3. -v ~/<path>/<to>/<project-directory>/dgraph: This will create a dgraph directory inside your ~/<path>/<to>/<project-directory> folder. The dgraph directory that is inside of your project directory is where this project’s Dgraph data will be stored. You can name your Dgraph data directory anything, but the name dgraph makes sense.
  4. :/dgraph: This is the second part of the -v command from above. This will mount a volume into the directory that is listed just before it. Mounting a volume means that your data will be saved to your local computer at the specified location (e.g. ~/<path>/<to>/<project-directory>/dgraph) and those data will be made available inside your Docker container at the location that is specified after the : (e.g. /dgraph).
  5. dgraph/standalone:latest: This specifies the Docker image to use when creating the container.

Some useful Docker commands

You can list all of your Docker containers (both running and stopped) with this command:

$ docker container ls -a

You can stop the container with this command:

Syntax:

$ docker stop <container_name>

Example:

$ docker stop dgraph-demo

And you can start the container with this command:

Syntax:

$ docker start <container_name>

Example:

$ docker start dgraph-demo

If you want to delete your project’s Docker container for any reason and start over, you can run the following command. Note that this will only delete the Docker container. It will not delete the data that was mounted in your project directory, which was specified with the -v (volume) flag. You will have to have root privileges in order to delete the data that was mounted.

Syntax:

$ docker container rm <container_name>

Example:

$ docker container rm dgraph-demo

Launch Ratel (the Dgraph Web UI)

After running the docker run command above, you should have a Dgraph instance running in a Docker container. Now you can interact with Ratel by opening a browser, going to http://localhost:8000/, and clicking the "Launch Latest" option.

Onward

You can use Dgraph with or without creating a schema first. If you create a schema, then Dgraph will create a default API that allows you to interface with the database without manually creating resolvers first. More on that in Part 3 where we will learn how to design the schema.

Discussion (0)