[SOLVED] Environment variables in LXC container

Aug 30, 2023
86
12
8
Luxembourg
I'm fairly new to LXC, mostly used podman containers, but I have recently rebuilt a PiHole server as an LXC container, using Debian 12, and everything seems fine so far. However I have an issue with environment variables - the old server has a daily cron job runs a script to backup the PiHole config, but it's not working because it cannot set a variable for the name of the log file. Here is the script:

Bash:
#logs
thislog=backups-$(date +%Y%m%d).log
echo "$(date +%Y%m%d): Backup script started" > /srv/backups/scripts/$thislog
echo "$(date +%Y%m%d): Cleaning up old logs" >> /srv/backups/scripts/$thislog
find /srv/backups/ -type f -name '*.log' -mtime +14 -exec rm {} \;
echo "$(date +%Y%m%d): Backing up PIHOLE" >> /srv/backups/scripts/$thislog
find /srv/backups/pihole/ -type f -name '*.tar.gz' -mtime +30 -exec rm {} \;
cd /srv/backups/pihole
pihole -a -t
echo "$(date +%Y%m%d): Backup script ended" >> /srv/backups/scripts/$thislog

I'm sure it can be improved but it's been working fine on the old RPi4 that was hosting PiHole before. But I find is that the "thislog" variable never gets so it doesn't work very - to work around it I have changed it to hardcode the log name as just "backups-$(date +%Y%m%d).log" on each command where "thislog" was used, but I'd like to go back to how it was.

I tried testing from the console of my container, both through the PvE console and through SSH, but I find I cannot set any variables at all:

Bash:
root@RALPH:~# env
SHELL=/bin/bash
PWD=/root
LOGNAME=root
SYSTEMD_EXEC_PID=574
MOTD_SHOWN=pam
HOME=/root
LANG=C
INVOCATION_ID=04cc6307f3b44c2ab78f4139e830cd61
TERM=linux
USER=root
SHLVL=1
HUSHLOGIN=FALSE
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MAIL=/var/mail/root
_=/usr/bin/env

root@RALPH:~# TESTVAR="wibble"

root@RALPH:~# env
SHELL=/bin/bash
PWD=/root
LOGNAME=root
SYSTEMD_EXEC_PID=574
MOTD_SHOWN=pam
HOME=/root
LANG=C
INVOCATION_ID=04cc6307f3b44c2ab78f4139e830cd61
TERM=linux
USER=root
SHLVL=1
HUSHLOGIN=FALSE
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MAIL=/var/mail/root
_=/usr/bin/env

I also tried with the export command.

Am I missing a setting for my container to allow variables to be created?

Oh and I also found that the "pihole" command also did not work without using the whole path even though that works fine from the console.
 
Did you ever figure this out?

I am working on creating a home lab dashboard using homepage. It supports environment variables which are easy to pass in Docker, but I cannot figure out where to set env variables in a LXC container.
 
Sorry, no I didn't, have stuck to the "hardcoded" values until someone provides help on how to allow environment variables to be created

Damn, I was being dumb, I looked at this again and found that simply replacing "thislog=backups-$(date +%Y%m%d)-test.log" with "export thislog=backups-$(date +%Y%m%d)-test.log" i.e. using "export" fixed it
 
Last edited:

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!