Adding to the approach from Aaron, if you're building your own image, you can now just use multi-stage builds to copy over the docker binary from an existing external image, e.g.:
If you are installing docker from the offical package repositories as described in these instructions, you can simply install the docker-ce-cli package and omit the installation of docker-ce and containerd.io.
Full installation sequence (for Ubuntu):
sudo apt-get update
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
# Here is the part that is different
sudo apt-get install docker-ce-cli