How to tell Docker to store and run containers on an external drive

I am running Docker on Ubuntu Linux. I have a Dockerfile that brings in a lot of data from elsewhere. I put the Dockerfile on an external drive that I have mounted to the local filesystem (ext4). Then I ran the build from within that external drive filesystem.

docker build -t mycontainer .

I watched the drive usage as the container was building. All of the drive usage was on the root filesystem instead of the attached drive.

I have read some articles on changing the Docker root/base from /var/lib/docker to another location, but so far all I get is docker.service: Failed with result 'exit-code'.

I also tried adding the -g options in the /lib/systemd/system/docker.service file:

ExecStart=/usr/bin/dockerd -H fd://  # original
ExecStart=/usr/bin/dockerd -g /new/path/docker -H fd://  # updated

This did not help either.

How can I tell Docker to use the external drive?

Several things helped me get this working. First, I was mounting the external filesystem in a home directory, which may have been causing some permissions issues. So I remounted the filesystem under /mnt and updated permissions and ownership as follows:

# chown -R root:root /mnt/external-drive/docker-base
# chmod 701 /mnt/external-drive/docker-base

Then I created the /etc/docker/daemon.json file with these contents:

{
    "graph": "/mnt/external-drive/docker-base"
}

I restarted the docker service and all seems to be well. Doing a docker build and the external drive is receiving all the contents.