loft-sh / devpod

Codespaces but open-source, client-only and unopinionated: Works with any IDE and lets you use any cloud, kubernetes or just localhost docker.
https://devpod.sh
Mozilla Public License 2.0
9.45k stars 342 forks source link

Using env variables in the devcontainer.json #507

Closed misialq closed 1 year ago

misialq commented 1 year ago

What happened? I keep getting an error when trying to create a workspace using the SSH provider. Here is the relevant section of the log:

[11:24:40] info docker: invalid reference format.
[11:24:40] info exit status 125
[11:24:40] info start dev container
[11:24:40] info github.com/loft-sh/devpod/pkg/devcontainer.(*Runner).runSingleContainer
[11:24:40] info /home/runner/work/devpod/devpod/pkg/devcontainer/single.go:75
[11:24:40] info github.com/loft-sh/devpod/pkg/devcontainer.(*Runner).Up
[11:24:40] info /home/runner/work/devpod/devpod/pkg/devcontainer/run.go:130
[11:24:40] info github.com/loft-sh/devpod/cmd/agent/workspace.(*UpCmd).devPodUp
[11:24:40] info /home/runner/work/devpod/devpod/cmd/agent/workspace/up.go:373
[11:24:40] info github.com/loft-sh/devpod/cmd/agent/workspace.(*UpCmd).up
[11:24:40] info /home/runner/work/devpod/devpod/cmd/agent/workspace/up.go:167
[11:24:40] info github.com/loft-sh/devpod/cmd/agent/workspace.(*UpCmd).Run
[11:24:40] info /home/runner/work/devpod/devpod/cmd/agent/workspace/up.go:101
[11:24:40] info github.com/loft-sh/devpod/cmd/agent/workspace.NewUpCmd.func1
[11:24:40] info /home/runner/work/devpod/devpod/cmd/agent/workspace/up.go:61
[11:24:40] info github.com/spf13/cobra.(*Command).execute
[11:24:40] info /home/runner/work/devpod/devpod/vendor/github.com/spf13/cobra/command.go:916
[11:24:40] info github.com/spf13/cobra.(*Command).ExecuteC
[11:24:40] info /home/runner/work/devpod/devpod/vendor/github.com/spf13/cobra/command.go:1044
[11:24:40] info github.com/spf13/cobra.(*Command).Execute
[11:24:40] info /home/runner/work/devpod/devpod/vendor/github.com/spf13/cobra/command.go:968
[11:24:40] info github.com/loft-sh/devpod/cmd.Execute
[11:24:40] info /home/runner/work/devpod/devpod/cmd/root.go:89
[11:24:40] info main.main
[11:24:40] info /home/runner/work/devpod/devpod/main.go:8
[11:24:40] info runtime.main
[11:24:40] info /opt/hostedtoolcache/go/1.19.10/x64/src/runtime/proc.go:250
[11:24:40] info runtime.goexit
[11:24:40] info /opt/hostedtoolcache/go/1.19.10/x64/src/runtime/asm_amd64.s:1594
[11:24:40] info devcontainer up
[11:24:40] info github.com/loft-sh/devpod/cmd/agent/workspace.(*UpCmd).Run
[11:24:40] info /home/runner/work/devpod/devpod/cmd/agent/workspace/up.go:103
[11:24:40] info github.com/loft-sh/devpod/cmd/agent/workspace.NewUpCmd.func1
[11:24:40] info /home/runner/work/devpod/devpod/cmd/agent/workspace/up.go:61
[11:24:40] info github.com/spf13/cobra.(*Command).execute
[11:24:40] info /home/runner/work/devpod/devpod/vendor/github.com/spf13/cobra/command.go:916
[11:24:40] info github.com/spf13/cobra.(*Command).ExecuteC
[11:24:40] info /home/runner/work/devpod/devpod/vendor/github.com/spf13/cobra/command.go:1044
[11:24:40] info github.com/spf13/cobra.(*Command).Execute
[11:24:40] info /home/runner/work/devpod/devpod/vendor/github.com/spf13/cobra/command.go:968
[11:24:40] info github.com/loft-sh/devpod/cmd.Execute
[11:24:40] info /home/runner/work/devpod/devpod/cmd/root.go:89
[11:24:40] info main.main
[11:24:40] info /home/runner/work/devpod/devpod/main.go:8
[11:24:40] info runtime.main
[11:24:40] info /opt/hostedtoolcache/go/1.19.10/x64/src/runtime/proc.go:250
[11:24:40] info runtime.goexit
[11:24:40] info /opt/hostedtoolcache/go/1.19.10/x64/src/runtime/asm_amd64.s:1594
[11:24:40] debug done exec
[11:24:40] debug done inject
[11:24:40] debug done injecting
[11:24:40] debug Done InjectAgentAndExecute
[11:24:40] debug Done executing up command
[11:24:40] debug Done creating devcontainer
[11:24:40] fatal executing agent command: agent error: No root required, because neither docker nor agent daemon needs to be installed
Use /home/<redacted>/.devpod/agent/contexts/default/workspaces/<redacted> as workspace dir
exit status 125
start dev container
github.com/loft-sh/devpod/pkg/devcontainer.(*Runner).runSingleContainer
        /home/runner/work/devpod/devpod/pkg/devcontainer/single.go:75
github.com/loft-sh/devpod/pkg/devcontainer.(*Runner).Up
        /home/runner/work/devpod/devpod/pkg/devcontainer/run.go:130
github.com/loft-sh/devpod/cmd/agent/workspace.(*UpCmd).devPodUp
        /home/runner/work/devpod/devpod/cmd/agent/workspace/up.go:373
github.com/loft-sh/devpod/cmd/agent/workspace.(*UpCmd).up
        /home/runner/work/devpod/devpod/cmd/agent/workspace/up.go:167
github.com/loft-sh/devpod/cmd/agent/workspace.(*UpCmd).Run
        /home/runner/work/devpod/devpod/cmd/agent/workspace/up.go:101
github.com/loft-sh/devpod/cmd/agent/workspace.NewUpCmd.func1
        /home/runner/work/devpod/devpod/cmd/agent/workspace/up.go:61
github.com/spf13/cobra.(*Command).execute
        /home/runner/work/devpod/devpod/vendor/github.com/spf13/cobra/command.go:916
github.com/spf13/cobra.(*Command).ExecuteC
        /home/runner/work/devpod/devpod/vendor/github.com/spf13/cobra/command.go:1044
github.com/spf13/cobra.(*Command).Execute
        /home/runner/work/devpod/devpod/vendor/github.com/spf13/cobra/command.go:968
github.com/loft-sh/devpod/cmd.Execute
        /home/runner/work/devpod/devpod/cmd/root.go:89
main.main
        /home/runner/work/devpod/devpod/main.go:8
runtime.main
        /opt/hostedtoolcache/go/1.19.10/x64/src/runtime/proc.go:250
runtime.goexit
        /opt/hostedtoolcache/go/1.19.10/x64/src/runtime/asm_amd64.s:1594
devcontainer up
github.com/loft-sh/devpod/cmd/agent/workspace.(*UpCmd).Run
        /home/runner/work/devpod/devpod/cmd/agent/workspace/up.go:103
github.com/loft-sh/devpod/cmd/agent/workspace.NewUpCmd.func1
        /home/runner/work/devpod/devpod/cmd/agent/workspace/up.go:61
github.com/spf13/cobra.(*Command).execute
        /home/runner/work/devpod/devpod/vendor/github.com/spf13/cobra/command.go:916
github.com/spf13/cobra.(*Command).ExecuteC
        /home/runner/work/devpod/devpod/vendor/github.com/spf13/cobra/command.go:1044
github.com/spf13/cobra.(*Command).Execute
        /home/runner/work/devpod/devpod/vendor/github.com/spf13/cobra/command.go:968
github.com/loft-sh/devpod/cmd.Execute
        /home/runner/work/devpod/devpod/cmd/root.go:89
main.main
        /home/runner/work/devpod/devpod/main.go:8
runtime.main
        /opt/hostedtoolcache/go/1.19.10/x64/src/runtime/proc.go:250
runtime.goexit
        /opt/hostedtoolcache/go/1.19.10/x64/src/runtime/asm_amd64.s:1594
: exit status 1

I know that the problem is with my devcontainer.json file (see below) - I'm using an env variable to determine which Docker image should be used for the dev container. I'm also setting the respective variable in my ssh config (using the SetEnv directive), but that doesn't get picked up. Am I missing something? Is that even possible?

What did you expect to happen instead? The workspace to be created successfully.

How can we reproduce the bug? (as minimally and precisely as possible)

My devcontainer.json:

{
    "name": "Test",
    "image": "quay.io/qiime2/core:${localEnv:QIIME_VERSION}",
    "forwardPorts": []
}

Local Environment:

DevPod Provider:

Anything else we need to know? That's all I can think of :)

FabianKramm commented 1 year ago

@misialq thanks for creating this issue! Does this work with another provider? Also is the env variable QIIME_VERSION set within the machine where you ssh to?

misialq commented 1 year ago

Hey @FabianKramm, I only tried with the Docker provider - also doesn't work, although there I wouldn't know where the env variable would be exactly taken from... The variable is set on the remote - I'm explicitly doing that by providing the variable through SetEnv option in the ssh config.

pbialon commented 1 year ago

Hi @misialq I tried to reproduce the problem you encountered, but I didn't get any error. I'll share my steps with you to make sure we are on the same page. Please ensure you followed the following steps.

For the test purposes I forked anexample-node-app repository and updated a .devcontainer.json to include ${localEnv:CONTAINER} directive.

  1. Prepare an entry in a local .ssh/config:

    Host <my-remote-ssh-server>
    SetEnv CONTAINER=mcr.microsoft.com/devcontainers/javascript-node:0-18-bullseye
  2. Log to your remote machine and in the /etc/ssh/sshd_config add a line:

    AcceptEnv CONTAINER
  3. Restart ssh service on your remote machine (in my case it was Debian Linux so I used the following command):

    systemctl restart ssh.service
  4. Run a devpod up command:

devpod up --debug https://github.com/pbialon/vscode-remote-try-node --ide=none

I tried the following steps both for devpod v0.2.1 & older ssh provider v0.0.6 and for the newest version devpod v0.3.3 and ssh provider v0.0.8 and both worked.

misialq commented 1 year ago

Hi @pbialon,

many thanks for the clear instructions! I see already what went wrong for me - I forgot to add the "AcceptEnv" line... 🤦‍♂️ I tried it now and it all worked.

Thanks so much and sorry for polluting the issue tracker!

pbialon commented 1 year ago

Happy to hear it worked for you!

I think that the process to get it working is not clear, so I'll think about adding some instruction to our documentation.