Run in Docker
Run the Apollo Router container image in Docker
This guide provides the following examples of running an Apollo Router container image in Docker:
- Running a basic example with default configuration.
- Customizing your configuration to override the default configuration.
- Debugging your containerized router.
- Manually specifying a supergraph for your router.
- Building your own router Docker image.
The documentation for the
docker run command is a helpful reference for the examples in this guide.
The exact image version to use depends on which release you wish to use. In the following examples, replace
<image version> with your chosen version, for example
The Apollo Router source code and all its distributions are made available under the Elastic License v2.0 (ELv2) license.
Here's a basic example of running a router image in Docker. Make sure to replace
<router-image-version> with whichever version you want to use, such as
docker run -p 4000:4000 \--env APOLLO_GRAPH_REF="<your-graph-ref>" \--env APOLLO_KEY="<your-graph-api-key>" \--rm \ghcr.io/apollographql/router:<router-image-version>
This command downloads your supergraph schema from Apollo and uses a default configuration that listens for connections on port
For more complex configurations, such as overriding subgraph URLs or propagating headers, see Configuring the Apollo Router.
Apollo's default Docker images include a basic router configuration. Inside the container, this file is located at
If you wish to override the default configuration, it is important preserve aspects of the default configuration. In particular, it is generally important for the router to bind to and listen on the special address of
0.0.0.0 (for all interfaces) to ensure it's exposed on a network interface that's accessible outside of the local container. Without this configuration, the router will only listen on
You can provide your own configuration from the host environment to the router by mounting your configuration to
/dist/config/router.yaml as follows:
docker run -p 4000:4000 \--env APOLLO_GRAPH_REF="<your-graph-ref>" \--env APOLLO_KEY="<your-graph-api-key>" \--mount "type=bind,source=/home/user/router.yaml,target=/dist/config/router.yaml" \--rm \ghcr.io/apollographql/router:<router-image-version>
Both local and container paths must be specified as absolute paths.
In this example we are mounting a file from the host system (
/home/user/router.yaml) in place of the default configuration provided in the image at
By default, the
router command invoked inside the published container doesn't set any of the available command-line options. To set them, append the desired option(s) to the
docker run command.
For example, to start the router using the
--log debug option, use the following
docker run command with the option added at the end:
docker run -p 4000:4000 \--env APOLLO_GRAPH_REF="<your-graph-ref>" \--env APOLLO_KEY="<your-graph-api-key>" \--rm \ghcr.io/apollographql/router:<router-image-version> --log debug
You can debug your container by setting the
entrypoint in a
docker run command:
docker run -p 4000:4000 \--env APOLLO_GRAPH_REF="<your graph>" \--env APOLLO_KEY="<your key>" \--mount "type=bind,source=/router.yaml,target=/dist/config/router.yaml" \--rm \--interactive \--tty \--entrypoint=bash \ghcr.io/apollographql/router:<image version>dist# pwd/distdist# lsconfig router schemadist# exitexit
In this example, we've added both interactive and tty flags, and we've changed the entrypoint of the image to be a bash shell.
docker run -p 4000:4000 \--env APOLLO_GRAPH_REF="<your graph>" \--env APOLLO_KEY="<your key>" \--mount "type=bind,source=/data,target=/dist/data"--rm \ghcr.io/apollographql/router:<image version>-debug
The router runs under the control of heaptrack. The heaptrack output is saved to the
/data directory. The output can be analyzed directly using
heaptrack_print or shared with Apollo support.
If you don't want to automatically update your supergraph via Apollo Uplink, or you don't have connectivity to access Apollo Uplink from your environment, you can manually specify the details of your supergraph in a
docker run command:
docker run -p 4000:4000 \--mount "type=bind,source=/docker.graphql,target=/dist/schema/local.graphql" \--rm \ghcr.io/apollographql/router:<image version> -c config/router.yaml -s schema/local.graphql
In this example, we have to mount the local definition of the supergraph into our image, and specify the location of the file. It doesn't have to be mounted in the
/dist/schema directory, but it's a reasonable location to use. We must specify the configuration file location as well, since overriding the default params will override our default config file location. In this case, since we don't want to change our router configuration but want to make sure it's used, we just specify the default location of the default configuration.
This section is aimed at developers familiar with tooling such as
git who wish to make their own DIY container images. The script documented here is not a part of the router product, but an illustrative example of what's involved in making your own images.
dockerfiles/diy directory, we now provide a script,
build_docker_image.sh which illustrates how to build your own docker images from either our released tarballs or from a git commit hash or tag. Here's how to use it:
% ./build_docker_image.sh -hUsage: build_docker_image.sh [-b [-r <repo>]] [-d] [<release>]-b build docker image from the default repo, if not present build from a released version-d build debug image, router will run under control of heaptrack-r build docker image from a specified repo, only valid with -b flag<release> a valid release. If [-b] is specified, this is optionalExample 1: Building HEAD from the repobuild_docker_image.sh -bExample 2: Building HEAD from a different repobuild_docker_image.sh -b -r /Users/anon/dev/routerExample 3: Building tag from the repobuild_docker_image.sh -b v0.9.1Example 4: Building commit hash from the repobuild_docker_image.sh -b 7f7d223f42af34fad35b898d976bc07d0f5440c5Example 5: Building tag v0.9.1 from the released versionbuild_docker_image.sh v0.9.1Example 6: Building a debug image with tag v0.9.1 from the released versionbuild_docker_image.sh -d v0.9.1
The example uses debian:bullseye-slim image for the final image build. Feel free to modify the script to use images which better suit your own needs, but be careful if using the
-d flag because it makes the assumption that there is a
heaptrack package available to install.