Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to share bash env variables #92

Open
mrsauravsahu opened this issue Dec 23, 2021 · 7 comments
Open

Ability to share bash env variables #92

mrsauravsahu opened this issue Dec 23, 2021 · 7 comments

Comments

@mrsauravsahu
Copy link

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

@oscarlofwenhamn
Copy link

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?

@abiosoft
Copy link
Owner

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.

@mrsauravsahu
Copy link
Author

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.

@abiosoft
Copy link
Owner

@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
Copy link
Owner

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.

@mrsauravsahu
Copy link
Author

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.

@michaelajr
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants