Reputation: 489
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?
Upvotes: 30
Views: 25901
Reputation: 3129
This one worked for me for deploying my app with docker compose at GCP:
#cloud-config
users:
- name: cloudservice
groups: [docker]
shell: /bin/bash
uid: 2000
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
- docker-compose-plugin
write_files:
- path: /etc/sysctl.d/enabled_ipv4_forwarding.conf
content: |
net.ipv4.conf.all.forwarding=1
- path: /etc/systemd/system/cloudservice.service
permissions: 0644
owner: root
content: |
[Unit]
Description=Start a simple docker container
[Service]
ExecStart=docker compose -f /home/cloudservice/my-service/beta-compose.yaml up
ExecStop=docker compose -f /home/cloudservice/my-service/beta-compose.yaml stop
ExecStopPost=docker compose -f /home/cloudservice/my-service/beta-compose.yaml down
runcmd:
- su - cloudservice -c "git clone https://github.com/my-user/my-service.git && cd /home/cloudservice/my-service/ && git checkout feature/my-branch"
- systemctl daemon-reload
- systemctl start cloudservice.service
Upvotes: 1
Reputation: 38545
Debian does not contain gpg by default so you have to do the following:
#cloud-config
write_files:
- path: /usr/share/keyrings/docker.asc
owner: root:root
permissions: '0644'
content: |
-----BEGIN PGP PUBLIC KEY BLOCK-----
paste content of https://download.docker.com/linux/debian/gpg (do not remove the blank line it fails if you do)
-----END PGP PUBLIC KEY BLOCK-----
apt:
sources:
docker.list:
source: deb [arch=amd64 signed-by=/usr/share/keyrings/docker.asc] https://download.docker.com/linux/debian $RELEASE stable
packages:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-compose-plugin
Source: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=970796
Upvotes: 1
Reputation: 213
simple code
#cloud-config
groups:
- docker
system_info:
default_user:
groups: [docker]
packages:
- docker.io
Upvotes: 0
Reputation: 24421
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.
#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]
#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]
#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]
Upvotes: 26
Reputation: 3966
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]
Upvotes: 47
Reputation: 10870
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
Upvotes: 4
Reputation: 950
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
Upvotes: 9