Skip to content

Create VM Template with Docker installed

I will be creating a number of Virtual Machines in proxmox, which will run docker stacks on them. To make life a bit easier, I created a template virtual machine, which will have the docker, and the base packages i need installed.

Upload Ubuntu Server ISO to proxmox

  1. Select the storage that you want to store the ISO, and then click Download from URL.
  2. Past download URL from Get Ubuntu Server site into the URL box. https://releases.ubuntu.com/20.04.4/ubuntu-20.04.4-live-server-amd64.iso
  3. Click Query URL button.
  4. Click Download Download from URL

Create Virtual Machine

Create a virtual machine, i called it template-docker-vm, give it the settings you want, this is up to you, then boot it. Then install ubuntu server, at one point it will ask for additional packages to install, do not select any. Once its installed, reboot and login with your user.

Install updates and packages

> sudo apt-get update && sudo apt-get dist-upgrade
> sudo apt-get install git curl net-tools

Install and configure docker

With the convenience script

> curl -fsSL https://get.docker.com -o get-docker.sh
> sudo sh get-docker.sh

Configure docker to start at boot

> sudo systemctl enable docker.service
> sudo systemctl enable containerd.service

Configure user to have the docker group

> sudo usermod -aG docker $USER
> newgrp docker

Test Docker

> docker run hello-world

Allow docker to listen on TCP port

This is usefull if you intend to use something like portainer to manage your docker instances.

  1. Open the override file for docker.service
    > sudo systemctl edit docker.service
    
  2. Add or replace the contents of the file with
    [Service]
    ExecStart=
    ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2375
    
  3. Reload the systemctl configuration.
    > sudo systemctl daemon-reload
    
  4. Restart Docker
    > sudo systemctl restart docker.service
    

Configure to start at next boot with unique machine id

Each vm will require a unique machine id, to do this empty /etc/machine-id

> sudo truncate -s 0 /etc/machine-id

Configure to get fresh ssh host keys on first boot

SSH will require unique host keys for each vm, for this we can setup a service that will run once and re-create the keys.

Create service

Create the following file (you will need to edit as root)

> sudo -e /etc/systemd/system/regenerate_ssh_host_keys.service

/etc/systemd/system/regenerate_ssh_host_keys.service

[Unit]
Description=Regenerate SSH host keys
Before=ssh.service
ConditionFileIsExecutable=/usr/bin/ssh-keygen

[Service]
Type=oneshot
ExecStartPre=-/bin/dd if=/dev/hwrng of=/dev/urandom count=1 bs=4096
ExecStartPre=-/bin/sh -c "/bin/rm -f -v /etc/ssh/ssh_host_*_key*"
ExecStart=/usr/bin/ssh-keygen -A -v
ExecStartPost=/bin/systemctl disable regenerate_ssh_host_keys

[Install]
WantedBy=multi-user.target

Enable service

> sudo systemctl enable regenerate_ssh_host_keys.service

Finally

Poweroff the VM, and create a backup. Then right-click and select "Create Template". From there you can create as many virtual machines preconfigured with docker.

References

Back to top