colima icon indicating copy to clipboard operation
colima copied to clipboard

Ability to share bash env variables

Open mrsauravsahu opened this issue 3 years ago • 6 comments
trafficstars

Is there a way to pass ENV variables from the shell to the VM?

mrsauravsahu avatar Dec 23 '21 14:12 mrsauravsahu

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?

oscarlofwenhamn avatar Jan 11 '22 07:01 oscarlofwenhamn

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.

abiosoft avatar Jan 13 '22 14:01 abiosoft

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 avatar Jan 17 '22 05:01 mrsauravsahu

@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.

abiosoft avatar Jan 17 '22 05:01 abiosoft

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.

abiosoft avatar Jan 17 '22 06:01 abiosoft

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.

mrsauravsahu avatar Jan 27 '22 13:01 mrsauravsahu

@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?

michaelajr avatar Mar 30 '23 20:03 michaelajr