This recently happened for me when running a build script for earthly/earthly.
OS: Arch Linux 5.14.8-arch1-1
Docker from official repository: Docker version 20.10.8, build 3967b7d28e
Solution (likely a Linux-only solution)
DNS was misconfigured for me. For some reason, docker pull golang:1.16-alpine3.14 worked fine but was failing when running the build script. This answer on r/docker helped.
Adding a DNS nameserver to my /etc/resolv.conf solved this issue for me:
cat /etc/resolv.conf
# Cloudflare
nameserver 1.1.1.1
Other Attempted Solutions
1. Disable Buildkit
From this answer to Docker build: failed to fetch oauth token for openjdk?, this did not solve the issue since I believe buildkit was required for the script I was running:
export DOCKER_BUILDKIT=0
export COMPOSE_DOCKER_CLI_BUILD=0
2. Manually pull image
3. Authenticating with Docker
The error looked like something that might happen when I was unauthenticated with hub.docker.com. After logging in with docker login --username <username> I still receieved the errors.