Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LXC environment variables

Tags:

lxc

I'm new to LXC containers and am using LXC v2.0. I want to pass settings to the processes running inside my container (specifically command line parameters for their Systemd service files.

I'm thinking of passing environment variables to the container via the config file lxc.environment = ABC=DEF . (I intend to use SALT Stack to manipulate these variables). Do I manually have to parse /proc/1/environ to access these variables or is there a better way I'm missing?

like image 665
gavenant Avatar asked Nov 04 '25 00:11

gavenant


1 Answers

The documentation says:

If you want to pass environment variables into the container (that is, environment variables which will be available to init and all of its descendents), you can use lxc.environment parameters to do so.

I would assume that, since all processes - including the shell - are descendents of the init process, the environment should be available in every shell. Unfortunately, this seems not to be true. In a discussion on linuxcontainers.org, someone states:

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.

So yes, obviously parsing /proc/1/environ seems to be the only possibility here.

like image 50
carlfriedrich Avatar answered Nov 07 '25 14:11

carlfriedrich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!