README.md (3629B)
1 # packer-builds 2 3 This repo contains some snippets to build containers with Packer. 4 5 The notes in this Readme do not follow a particular structure, but should help using the scripts. 6 7 ## Build Templates 8 9 List templates (from `man lxc-create`): 10 11 ``` 12 /usr/share/lxc/templates/lxc-download -l 13 ``` 14 15 ## Build Instructions 16 17 For instance, to build the MariaDB image with debug logs: 18 ```bash 19 $ PACKER_LOG=1 packer build mariadb-lxc.json 20 ``` 21 22 For some newer, hcl-based Packer configurations, it is necessary to supply 23 extra variables through var files or the `-var` input flag: 24 25 ```bash 26 packer build -var "gatus_tls_nfs_server=server:/share" gatus-lxc.pkr.hcl 27 ``` 28 29 ### Suggested Build Order 30 31 The following sequence of builds allows you to spin up a small Nextcloud installation. 32 33 **1 Build Mariadb** 34 35 ``` 36 PACKER_LOG=1 packer build \ 37 -var 'mysql_root_password=123' \ 38 -var 'mysql_additional_passwords="456"' \ 39 mariadb-nextcloud.json 40 41 ./deploy-output.sh output-packer-mariadb-nextcloud mariadb 42 ``` 43 44 **2 Build Haproxy** 45 46 ``` 47 PACKER_LOG=1 packer build haproxy-lxc.json 48 ./deploy-output.sh output-packer-haproxy haproxy 49 ``` 50 51 **3 Nextcloud** 52 53 First, start the previously built components: 54 ```bash 55 lxc-start haproxy 56 lxc-start mariadb 57 ``` 58 59 Afterwards, build the Nextcloud container: 60 ```bash 61 # install nextcloud with the correct database password from step (1) 62 PACKER_LOG=1 packer build \ 63 -var 'nextcloud_admin_user=admin' \ 64 -var 'nextcloud_admin_pass=abc' \ 65 -var 'nextcloud_database_pass=456' \ 66 -var 'certbot_mail=root@dev.mail' \ 67 -var 'overwrite_cli_url=nextcloud.com' \ 68 nextcloud-lxc.json 69 70 ./deploy-output.sh output-packer-nextcloud nextcloud 71 ``` 72 73 ## Manual Container Install to LXC Directory 74 75 Prepare container name and target config file variables: 76 ```bash 77 name="container_name" 78 target_config="/srv/lxc/$name/config" 79 mkdir "/srv/lxc/$name" 80 ``` 81 82 Extract the rootfs tarball and add the config: 83 ```bash 84 # exctract root fs 85 tar -xvf output-lxc/rootfs.tar.gz -C "/srv/lxc/$name" 86 87 # copy config 88 cp output-lxc/lxc-config $target_config 89 90 # adjust rootfs path 91 echo -e "\nlxc.rootfs.path = dir:/srv/lxc/$name/rootfs" >> $target_config 92 ``` 93 94 The script `deploy-output.sh` automates the above steps. Usage: 95 ``` 96 $ ./deploy-output.sh 97 This script deploys templates built with the lxc builder for Packer: 98 https://www.packer.io/docs/builders/lxc.html 99 100 No arguments supplied, required args: 101 PACKER_OUTPUT_DIR: Packer lxc output directory with rootfs and config template 102 CONTAINER_NAME: Name of the new container 103 104 Usage: deploy-output.sh PACKER_OUTPUT_DIR CONTAINER_NAME 105 106 Copy lxc template from PACKER_OUTPUT_DIR to /var/lib/lxc/CONTAINER_NAME 107 ``` 108 109 ## Haproxy stats 110 ```bash 111 echo "show stat" | socat stdio /run/haproxy/admin.sock 112 ``` 113 114 ## Isssues 115 ### Issue: Missing LXC Library Dir 116 117 ``` 118 Build 'lxc' errored: Error creating container: Command error: touch: /var/lib/lxc/packer-lxc/rootfs/tmp/.tmpfs: No such file or directory 119 ``` 120 121 For lxc on Turris, create a symbolic link: 122 ```bash 123 ln -s /srv/lxc/ /var/lib/lxc 124 ``` 125 (yep, it's [hardcoded](https://github.com/hashicorp/packer-plugin-lxc/blob/main/builder/lxc/step_lxc_create.go#L23)) 126 127 128 ### Issue: Script not Found 129 ```bash 130 chmod: cannot access '/tmp/script_9801.sh': No such file or directory 131 /bin/sh: 1: /tmp/script_9801.sh: not found 132 ``` 133 134 Fix: Retry, most likely a timing bug on repeated builds 135 136 137 ### Issue: Duplicate Nexctloud User 138 ``` 139 Username is invalid because files already exist for this user 140 ``` 141 142 Fix: Choose another Nextcloud admin user name 143 ```bash 144 $ PACKER_LOG=1 packer build -var 'nextcloud_admin_user=admin2' -var 'nextcloud_admin_pass=admin2' nextcloud-lxc.json 145 ```