首页 > 解决方案 > 如何在容器化(docker)vs代码中显示GUI

问题描述

我正在尝试在 vs 代码中使用 docker 开发环境在 Ubuntu 20 上创建 OpenGL 应用程序

容器构建成功,但当我尝试显示任何内容时出现错误:

Could not initialize canvas

我执行以尝试显示任何内容的代码 - 它在上面给出了一个错误

apt-get update \
  && apt-get install -y -qq glmark2 \
  && glmark2

主机中的命令以公开 xhost

jakub@...:~$ export DISPLAY
jakub@...:~$ xhost +local:root
non-network local connections being added to access control list

devcontainer.json(在 vs 代码中)

{
    "name": "Existing Dockerfile",

    // Sets the run context to one level up instead of the .devcontainer folder.
    "context": "..",

    // Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename.
    "dockerFile": "../Dockerfile",

"mounts": [
        "source=/tmp/.X11-unix,target=/tmp/.X11-unix,type=bind,consistency=cached"
      ],
    "runArgs": ["--rm","-it","--privileged","--gpus","all","-e QT_X11_NO_MITSHM=1","-e DISPLAY=unix$DISPLAY" ]
}

DockerFile

    FROM ubuntu:20.04

LABEL maintainer "NVIDIA CORPORATION <cudatools@nvidia.com>"

RUN apt-get update && apt-get install -y --no-install-recommends \
    gnupg2 curl ca-certificates && \
    curl -fsSL https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub | apt-key add - && \
    echo "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64 /" > /etc/apt/sources.list.d/cuda.list && \
    echo "deb https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu2004/x86_64 /" > /etc/apt/sources.list.d/nvidia-ml.list && \
    apt-get purge --autoremove -y curl \
    && rm -rf /var/lib/apt/lists/*

ENV CUDA_VERSION 11.3.1

# For libraries in the cuda-compat-* package: https://docs.nvidia.com/cuda/eula/index.html#attachment-a
RUN apt-get update && apt-get install -y --no-install-recommends \
    cuda-cudart-11-3=11.3.109-1 \
    cuda-compat-11-3 \
    && ln -s cuda-11.3 /usr/local/cuda && \
    rm -rf /var/lib/apt/lists/*


# Dependencies for glvnd and X11.
RUN apt-get update \
  && apt-get install -y -qq --no-install-recommends \
    libglvnd0 \
    libgl1 \
    libglx0 \
    libegl1 \
    libxext6 \
    libx11-6 \
  && rm -rf /var/lib/apt/lists/*

# Required for nvidia-docker v1
RUN echo "/usr/local/nvidia/lib" >> /etc/ld.so.conf.d/nvidia.conf \
    && echo "/usr/local/nvidia/lib64" >> /etc/ld.so.conf.d/nvidia.conf

ENV PATH /usr/local/nvidia/bin:/usr/local/cuda/bin:${PATH}
ENV LD_LIBRARY_PATH /usr/local/nvidia/lib:/usr/local/nvidia/lib64


# nvidia-container-runtime
ENV NVIDIA_VISIBLE_DEVICES all
ENV NVIDIA_DRIVER_CAPABILITIES compute,utility
ENV NVIDIA_REQUIRE_CUDA "cuda>=11.3 brand=tesla,driver>=418,driver<419 brand=tesla,driver>=440,driver<441 driver>=450"

或者,我在 devcontainer.json 中添加了“runArgs”

"-v /tmp/.X11-unix:/tmp/.X11-unix" 

在这种情况下,我得到错误

Error response from daemon: create  /tmp/.X11-unix: " /tmp/.X11-unix" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path

坦率地说,如果 / 不允许如何表示路径?

标签: openglvisual-studio-codedockerfile

解决方案


推荐阅读