colima
colima copied to clipboard
Ability to share bash env variables
Is there a way to pass ENV variables from the shell to the VM?
Possibly related to lima-vm/lima#412 - Using --env for nerdctl doesn't pass through current environment values
tl;dr - the nerdctl (cli for containerd runtime) implementation of lima doesn't appear to pass env variables into the VM properly, might be the same situation here?
Is there a way to pass ENV variables from the shell to the VM?
Is this with regards to docker or containerd runtime? Or you simply want to have some env vars available in the VM.
This is with docker runtime. I have a few environment variables that show up when I switch to my project directory (thanks to https://direnv.net/)
Is there a way to pass these to the VM.
Idea is to be able to use env vars in docker commands. docker build --build-arg SAMPLE_VAR=$EXAMPLE_VALUE -t test:latest .
EXAMPLE_VALUE is an environment variable in my host machine.
@mrsauravsahu if you are using docker runtime, the docker client is on your host machine so this should work already.
docker build --build-arg SAMPLE_VAR=$EXAMPLE_VALUE -t test:latest .
as long as echo $EXAMPLE_VALUE gives you the correct value in your current shell, it should work.
If you simply want some env vars to be available in the VM, there is an undocumented --env flag that does that. I made it hidden as the behaviour was inconsistent initially but seems it has now been fixed upstream in Lima.
colima start --env EXAMPLE_VALUE=some_value --env ANOTHER_VALUE=another_value
This will make those env vars available in the VM and you can verify with colima ssh env. It does not persist, so you need to do that on each start.
Note
Docker and containerd runtime are different in this regard as the docker client runs on the host, but the containerd client (nerdctl) runs in the VM.
Docker
For docker runtime, you most likely do not need this except you intend to use the VM directly for other purposes. Your env vars get passed as expected because the client is on your host.
Containerd
You can use the (VM) env vars for containerd runtime by escaping it or use single quote so it doesn't get translated in your current shell but passed down to the VM's shell.
nerdctl run --rm -it --env SAMPLE_VAR=\$EXAMPLE_VALUE alpine # this works
nerdctl run --rm -it --env 'SAMPLE_VAR=$EXAMPLE_VALUE' alpine # this works
nerdctl run --rm -it --env SAMPLE_VAR=$EXAMPLE_VALUE alpine # this does not work
Next steps
This can be revisited ahead of next release if there is enough interest.
Thanks @abiosoft I was facing this issue in containerd, I switched to docker recently. Works great. Thanks. Feel free to keep the thread going for containerd runtime.
@abiosoft So passing -e on a docker run does nothing, as the expectation all env vars are in the shell. This seems strange. Any work arounds for that?