I’m trying to get some environment variables to show up inside a container, but I’m having problems.
I did lxc config set ubuntu environment.ZZZ Something. After that lxc exec ubuntu -- env shows the variable, so LXD probably did something right.
However, doing lxc console ubuntu and then env doesn’t show the variable. Even doing lxc restart ubuntu && lxc console ubuntu and then running env doesn’t show the variable.
I’m confused. I’d expect those variables set through LXD to be global for the machine running inside the container. What is the point of having this configuration option if only LXD can see them?
That’s not how this works unfortunately. Those environment variables are passed to anything you lxc exec and is passed to the container’s init system.
Unfortunately init systems usually don’t care much for those environment variables and never propagate them to their children, meaning that they’re effectively just present in lxc exec sessions or to scripts which directly look at PID 1’s environment.
I see this file shows just those that I set - there’s nothing else. Would it be safe to propagate this unconditionally through an init script or something of the sort?
I mean, who else might touch this besides lxc config?
lxc config is instant. I like that. Pity even /proc/1/environ isn’t instant. Guess what I see happen is just lxc console or lxc exec setting these variables before it shows a prompt.
So yeah I guess restarting is required for config changes to be picked up.
How does docker do it? /proc/1/environ has them, env has them, but /etc/environment doesn’t even exist. On lxd the last one just has PATH on ubuntu and nothing besides comments on alpine.
user.* keys aren’t environment keys, they’re used to attach extra metadata to the container but are only visible from inside the container if you query them through /dev/lxd.
To do what you’re showing above, you’d want to use environment.fqdn instead.
That would be because init (systemd) clears its own environment, so even though we do expose those env variables to it on startup, it doesn’t really do anything.