README.md (3954B)
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 ## Build armv7l Minio 115 On amd64, cross compile for arm: 116 ``` 117 git clone --depth=1 https://github.com/minio/minio.git minio.git 118 cd minio.git 119 # https://github.com/golang/go/issues/65568 120 sed -i 's/go 1.22/go 1.22.0/' go.mod 121 122 GOARCH=arm CGO_ENABLED=0 go build -o minio github.com/minio/minio 123 ``` 124 125 Copy this binary to the arm host. 126 127 ## Isssues 128 ### Issue: Missing LXC Library Dir 129 130 ``` 131 Build 'lxc' errored: Error creating container: Command error: touch: /var/lib/lxc/packer-lxc/rootfs/tmp/.tmpfs: No such file or directory 132 ``` 133 134 For lxc on Turris, create a symbolic link: 135 ```bash 136 ln -s /srv/lxc/ /var/lib/lxc 137 ``` 138 (yep, it's [hardcoded](https://github.com/hashicorp/packer-plugin-lxc/blob/main/builder/lxc/step_lxc_create.go#L23)) 139 140 141 ### Issue: Script not Found 142 ```bash 143 chmod: cannot access '/tmp/script_9801.sh': No such file or directory 144 /bin/sh: 1: /tmp/script_9801.sh: not found 145 ``` 146 147 Fix: Retry, most likely a timing bug on repeated builds 148 149 150 ### Issue: Duplicate Nexctloud User 151 ``` 152 Username is invalid because files already exist for this user 153 ``` 154 155 Fix: Choose another Nextcloud admin user name 156 ```bash 157 $ PACKER_LOG=1 packer build -var 'nextcloud_admin_user=admin2' -var 'nextcloud_admin_pass=admin2' nextcloud-lxc.json 158 ```