Skip to content

Loading Environment Variables in Windows with WSL2 #1638

Closed
@HFR1994

Description

@HFR1994

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions