How do I install docker using cloud-init?
Solution 1
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]
Solution 2
CAUTION: You should not use the Docker Convenience script (get.docker.com
), it carries a warning for production environments:
Using these scripts is not recommended for production environments
Here are three ways to install Docker on Ubuntu using cloud-init for all environments that don't use the Docker Convenience script.
Install via apt-source (recommended approach):
#cloud-config
apt:
sources:
docker.list:
source: deb [arch=amd64] https://download.docker.com/linux/ubuntu $RELEASE stable
keyid: 9DC858229FC7DD38854AE2D88D81803C0EBFCD88
packages:
- apt-transport-https
- ca-certificates
- curl
- gnupg-agent
- software-properties-common
- docker-ce
- docker-ce-cli
- containerd.io
# Enable ipv4 forwarding, required on CIS hardened machines
write_files:
- path: /etc/sysctl.d/enabled_ipv4_forwarding.conf
content: |
net.ipv4.conf.all.forwarding=1
# create the docker group
groups:
- docker
# Add default auto created user to docker group
system_info:
default_user:
groups: [docker]
Full install via cURL: (gist reference)
#cloud-config
packages:
- apt-transport-https
- ca-certificates
- curl
- gnupg-agent
- software-properties-common
# Enable ipv4 forwarding, required on CIS hardened machines
write_files:
- path: /etc/sysctl.d/enabled_ipv4_forwarding.conf
content: |
net.ipv4.conf.all.forwarding=1
# create the docker group
groups:
- docker
# Install Docker, for production, consider pinning to stable versions
runcmd:
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
- add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
- apt-get update -y
- apt-get install -y docker-ce docker-ce-cli containerd.io
- systemctl start docker
- systemctl enable docker
# Add default auto created user to docker group
system_info:
default_user:
groups: [docker]
Simplified install using the default package: (gist reference)
#cloud-config
packages:
- docker.io
# create the docker group
groups:
- docker
# Add default auto created user to docker group
system_info:
default_user:
groups: [docker]
Solution 3
There's a docker script which can be #include'd that's very handy for docker. Instead of #cloud-config, use
#include https://get.docker.com
Solution 4
Ricardo's solution is great if you only need to add docker to the deployed instance. But, in cases where you still DO need a #cloud-config
(to customize other stuff, like pre-installed packages), here is a simple solution inspired by his answer, just add this command:
#cloud-config
# ... more config here
runcmd:
- curl -fsSL https://get.docker.com -o get-docker.sh; sh get-docker.sh
Related videos on Youtube
lgdelacruz
Updated on June 04, 2022Comments
-
lgdelacruz over 1 year
I want to create instances in Openstack that will have Docker in them already installed prior to ssh to them. So naturally I got interested in Cloud-init technology because it allows us to install packages on virtual machines during first boot time. So now I'm trying to install Docker on my instances during boot time, here is my code that I'm passing to the user data;
#cloud-config packages: - docker.io
This doesn't work obviously, so how can I make it work?
-
Everett Toews over 9 yearsWhat operating system are you using for your OpenStack instance?
-
-
Phil Frost about 3 yearsOr better yet,
curl -fsSL https://get.docker.com | sh
? -
G_G over 2 yearsgreat answer, confirmed this work, the
| gpg
is an awesome timesaver -
Florian Falk about 2 years!!! See Highway of Life's answer on this! stackoverflow.com/a/62540068/4907101