If you want to install from the Docker repositories on an Ubuntu instance, and you don't especially like the idea of downloading and executing an arbitrary shell script, all you need is this:
#cloud-config
apt:
sources:
docker.list:
source: deb [arch=amd64] https://download.docker.com/linux/ubuntu $RELEASE stable
keyid: 9DC858229FC7DD38854AE2D88D81803C0EBFCD88
packages:
- docker-ce
- docker-ce-cli
cloud-init already knows how to get a GPG key, how to add an APT source (even if it is HTTPS), how to update APT before installing packages, and how to do all the other stuff you'll find in various shell script heavy ways of doing this.
If Docker should ever change their repo signing key, you can satisfy yourself that the change is legitimate and then get the new fingerprint with something like:
$ curl -sL https://download.docker.com/linux/ubuntu/gpg | gpg
gpg: keybox '/home/ubuntu/.gnupg/pubring.kbx' created
gpg: WARNING: no command supplied. Trying to guess what you mean ...
pub rsa4096 2017-02-22 [SCEA]
9DC858229FC7DD38854AE2D88D81803C0EBFCD88
uid Docker Release (CE deb) <[email protected]>
sub rsa4096 2017-02-22 [S]