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

Loading Environment Variables in Windows with WSL2 #1638

Open
HFR1994 opened this issue Feb 11, 2025 · 1 comment
Open

Loading Environment Variables in Windows with WSL2 #1638

HFR1994 opened this issue Feb 11, 2025 · 1 comment
Labels

Comments

@HFR1994
Copy link

HFR1994 commented Feb 11, 2025

What happened?

I created a dynamic environment variable inside my WSL2 installation with:

echo 'export DISPLAY=$(grep nameserver /etc/resolv.conf | awk "{print \$2}"):0.0' >> ~/.bashrc

I have tried the following configurations inside my devcontainer.json:

...
  "remoteEnv": {
    "DISPLAY": "${localEnv:DISPLAY}"
  },
  "runArgs": [
      "--volume", "/tmp/.X11-unix:/tmp/.X11-unix"
  ],
...

and

...
  "runArgs": [
     "--env", "DISPLAY",
      "--volume", "/tmp/.X11-unix:/tmp/.X11-unix"
  ],
...

But the variable is always empty inside the container.

What did you expect to happen instead?

When I run the following command inside the devcontainer, I should get a similar result:

vscode ➜ /workspaces/image-processing (main) $ echo $DISPLAY
192.168.160.1:0.0

Which has the same value if I run the same command in WSL2.

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

My devcontainer.json:

{
    "build": {
      "dockerfile": "Dockerfile",
      "context": "..",
      "args": {
        "VARIANT": "3.10-bullseye",
        // Options
        "NODE_VERSION": "lts/*"
      }
    },
    "remoteEnv": {
      "DISPLAY": "${localEnv:DISPLAY}"
    },
    "runArgs": [
        "--volume", "/tmp/.X11-unix:/tmp/.X11-unix"
    ],
    "remoteUser": "vscode"
}

My Dockerfile:

ARG VARIANT=3-bullseye
FROM mcr.microsoft.com/vscode/devcontainers/python:${VARIANT}

# [Choice] Node.js version: none, lts/*, 16, 14, 12, 10
ARG NODE_VERSION="none"
RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi

ENV DISPLAY=${DISPLAY}

# [Optional] If your pip requirements rarely change, uncomment this section to add them to the image.
# COPY requirements.txt /tmp/pip-tmp/
 RUN pip3 --disable-pip-version-check --no-cache-dir install opencv-python==4.11.0.86 \
    && rm -rf /tmp/pip-tmp

# [Optional] Uncomment this section to install additional OS packages.
 RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
     && apt-get purge -y imagemagick imagemagick-6-common \
     && apt-get -y install --no-install-recommends \
        libopencv-dev \
        python3-opencv 

# [Optional] Uncomment this line to install global node packages.
# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g <your-package-here>" 2>&1

Run in WSL:

echo 'export DISPLAY=$(grep nameserver /etc/resolv.conf | awk "{print \$2}"):0.0' >> ~/.bashrc

Once executed the devcontainer, try to run:

vscode ➜ /workspaces/image-processing (main) $ echo $DISPLAY

It should come back empty

Local Environment:

  • DevPod Version: 0.6.1
  • Operating System: windows
  • ARCH of the OS: AMD64

DevPod Provider:

  • Local/remote provider: docker

Anything else we need to know?

If I add the variable to Powershell using :

$DISPLAY_IP = wsl cat /etc/resolv.conf | Select-String "nameserver" | ForEach-Object { $_ -replace "nameserver\s+", "" }
$env:DISPLAY = "$DISPLAY_IP`:0.0"

and run it using

devpod up --workspace-env DISPLAY=$env:DISPLAY <<DEVPOD_CONTAINER_NAME>> 

It works! It seems that the GUI is not picking the variables inside ~/.bashrc

@bkneis
Copy link
Contributor

bkneis commented Mar 3, 2025

Hi @HFR1994, how are you launching devpod desktop? I am by no means a windows expert but I am not sure if environment variables in ~/.bashrc are expected to be sourced by desktop applications. If you launch devpod desktop from a terminal, does the environment variable work?

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

No branches or pull requests

2 participants