devpod icon indicating copy to clipboard operation
devpod copied to clipboard

Loading Environment Variables in Windows with WSL2

Open HFR1994 opened this issue 10 months ago • 1 comments

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

HFR1994 avatar Feb 11 '25 23:02 HFR1994

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?

bkneis avatar Mar 03 '25 09:03 bkneis

This issue is stale because it has been open for 60 days with no activity.

github-actions[bot] avatar May 03 '25 02:05 github-actions[bot]

This issue was closed because it has been inactive for 30 days since being marked as stale.

github-actions[bot] avatar Jun 02 '25 02:06 github-actions[bot]