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.
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
~/code/web-development/dgraph-demo/. You should replace that file path with your file path to your
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:
--name <container-name>: This creates and runs a container with the name that you specified (e.g.
-p 8000:8000 -p 8080:8080 -p 9080:9080: This exposes a few ports:
8000is used for Ratel (the Dgraph web UI).
8080is used for the REST API.
9080is used for the gRPC API.
-v ~/<path>/<to>/<project-directory>/dgraph: This will create a
dgraphdirectory inside your
dgraphdirectory 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: This is the second part of the
-vcommand 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
dgraph/standalone:latest: This specifies the Docker image to use when creating the container.
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:
$ docker stop <container_name>
$ docker stop dgraph-demo
And you can start the container with this command:
$ docker start <container_name>
$ 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.
$ docker container rm <container_name>
$ docker container rm dgraph-demo
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.
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.