7

I wanted to bootstrap my Azure VM with Docker and Docker-compose using cloud-init. So far I tried something like below.

#cloud-config

package_update: true
package_upgrade: true

groups:
  - docker: [default]

runcmd:
  - [ sh, -c, "curl -sSL https://get.docker.com/ | sh" ]
  - [ sh, -c, "sudo curl -L "https://github.com/docker/compose/releases/download/$(git ls-remote https://github.com/docker/compose | grep refs/tags | grep -oP "[0-9]+\.[0-9][0-9]+\.[0-9]+$" | tail -n 1)/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose" ]
  - [ sh, -c, "sudo chmod +x /usr/local/bin/docker-compose" ]

But it doesn't install the docker/ docker-compose and get below error

cloud-init[958]: 2019-02-28 00:51:02,447 - util.py[WARNING]: Failed loading yaml blob. Invalid format at line 11 column 32: "while scanning a plain scalar
[  333.241244] cloud-init[958]:   in "<unicode string>", line 11, column 32:
[  333.245521] cloud-init[958]:         - [ sh, -c, "sudo curl -L "https://github.com/docker/compos ...
2019/02/28 00:51:04.015216 INFO Daemon Wire protocol version:2012-11-30
[  333.245619] cloud-init[958]:                                    ^
[  333.259509] cloud-init[958]: found unexpected ':'
[  333.259584] 2019/02/28 00:51:04.019282 INFO Daemon Server preferred version:2015-04-05
cloud-init[958]:   in "<unicode string>", line 11, column 37:
[  333.474295] cloud-init[958]:         - [ sh, -c, "sudo curl -L "https://github.com/docker/compose/rel ...
[  333.514672] cloud-init[958]:                                         ^
[  333.554215] cloud-init[958]: Please check http://pyyaml.org/wiki/YAMLColonInFlowContext for details."
[  333.593764] cloud-init[958]: 2019/02/28 00:51:04.361760 INFO Daemon Waiting for ssh host key be generated at /etc/ssh/ssh_host_rsa_key.pub [1800 attempts remaining, sleeping 1s]
2019-02-28 00:51:02,470 - util.py[WARNING]: Failed loading yaml blob. Invalid format at line 11 column 32: "while scanning a plain scalar
[  333.705350] cloud-init[958]:   in "<unicode string>", line 11, column 32:
[  333.731074] cloud-init[958]:         - [ sh, -c, "sudo curl -L "https://github.com/docker/compos ...
[  333.769118] cloud-init[958]:                                    ^
[  333.789992] cloud-init[958]: found unexpected ':'
[  333.808855] cloud-init[958]:   in "<unicode string>", line 11, column 37:
[  333.831502] cloud-init[958]:         - [ sh, -c, "sudo curl -L "https://github.com/docker/compose/rel ...
[  333.863028] cloud-init[958]:                                         ^
[  333.885648] cloud-init[958]: Please check http://pyyaml.org/wiki/YAMLColonInFlowContext for details."
[  333.915174] cloud-init[958]: 2019-02-28 00:51:02,470 - util.py[WARNING]: Failed at merging in cloud config part from part-001

Apart from it I doubt how can I add Azure auto created default user (which was provided from UI) to docker group.

3 Answers 3

3

Just tested by myself on Azure using Ubuntu 18.04-LTS:

#cloud-config
package_upgrade: true

packages:
    - docker.io
    - docker-compose

# create the docker group
groups:
    - docker

# assign a VM's default user, which is mydefaultuser, to the docker group
users:
    - default
    - name: mydefaultuser
      groups: docker

runcmd can be left empty for this setup.

1

you have " inside of other " which doesnt work without escaping. as for the user. I think default user created has id of 1000, you can just use that (probably test this hypothesis before).

0

After spending some time I solved it myself.

#cloud-config

package_update: true

# Setup swap memory
disk_setup:
    ephemeral0:
        table_type: mbr
        layout: [66, [33, 82]]
        overwrite: True
fs_setup:
    - device: ephemeral0.1
      filesystem: ext4
    - device: ephemeral0.2
      filesystem: swap
mounts:
    - ["ephemeral0.1", "/mnt"]
    - ["ephemeral0.2", "none", "swap", "sw", "0", "0"]

# Enable Docker's swap limit support
bootcmd:
    - [ sh, -c, 'sudo echo GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1" >> /etc/default/grub' ]
    - [ sh, -c, 'sudo update-grub' ]
    - [ cloud-init-per, once, mymkfs, mkfs, /dev/vdb ]

# Install docker and docker-compose
runcmd:
    - [ sh, -c, 'curl -sSL https://get.docker.com/ | sh' ]
    - [ sh, -c, 'sudo curl -L https://github.com/docker/compose/releases/download/$(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep "tag_name" | cut -d \" -f4)/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose' ]
    - [ sh, -c, 'sudo chmod +x /usr/local/bin/docker-compose' ]

# Add default auto created user to docker group
system_info:
    default_user:
        groups: [docker]
1
  • 3
    Note with this setup is uses the docker convenience script which comes with a big warning Using these scripts is not recommended for production environments
    – icc97
    Commented Apr 27, 2020 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.