通常我会用这个技巧让X在docker:中工作
docker run --rm -it -v /tmp/.X11-unix:/tmp/.X11-unix -v $HOME/Videos:/videos -e DISPLAY=unix$DISPLAY --name knl kdenlive
我试着在一个devcontainer上做同样的事情:
{
"name": "example_dockerized_environment",
"dockerFile": "Dockerfile",
"extensions": [
"ms-vscode.cpptools",
"twxs.cmake",
"eamodio.gitlens"
],
"mounts": [ "source=../,target=/home/project", "source=/tmp/.X11-unix, target=/tmp/.X11-unix"],
"containerEnv": {
"DISPLAY": "unix:0"
},
"runArgs": ["--privileged"]
}
正如你所看到的,我通过了unix$DISPLAY
,还安装了X11-unix
但我有
root@5e6a10efbea6:/workspaces/leuze_lidar_volume/sdk/quanergy_client-master# ./visualizer --host localhost
ERROR: In /build/vtk6-YpT4yb/vtk6-6.2.0+dfsg1/Rendering/OpenGL/vtkXOpenGLRenderWindow.cxx, line 1466
vtkXOpenGLRenderWindow (0x2293740): bad X server connection. DISPLAY=Aborted (core dumped)
当我在docker内部执行echo $DISPLAY
时,我什么也看不到。我试着做
导出DISPLAY=unix:0
,然后我得到
root@5e6a10efbea6:/workspaces/leuze_lidar_volume/sdk/quanergy_client-master# ./visualizer --host localhost
ERROR: In /build/vtk6-YpT4yb/vtk6-6.2.0+dfsg1/Rendering/OpenGL/vtkXOpenGLRenderWindow.cxx, line 1466
vtkXOpenGLRenderWindow (0x281dcf0): bad X server connection. DISPLAY=unix:0. Aborting.
Aborted (core dumped)
我还看到集装箱内没有/tmp.x11-unix
:
root@5e6a10efbea6:/tmp# ls -la /tmp
total 16
drwxrwxrwt 1 root root 4096 Mar 18 03:47 .
drwxr-xr-x 1 root root 4096 Mar 18 03:45 ..
srwxr-xr-x 1 root root 0 Mar 18 03:42 vscode-git-askpass-c2ca47727522d7940b4cce1d99fcc88d32ccfefc.sock
srwxr-xr-x 1 root root 0 Mar 18 03:47 vscode-git-ipc-f52b0dbfd870db22481ea656170b7615ea1e6497.sock
srwxr-xr-x 1 root root 0 Mar 18 03:45 vscode-ipc-032f3099-16ea-4f5d-8561-586571a4aea9.sock
srwxr-xr-x 1 root root 0 Mar 18 03:32 vscode-ipc-425af2fc-ddb1-4554-b93b-3a5bede4c52d.sock
srwxr-xr-x 1 root root 0 Mar 18 03:47 vscode-ipc-58739ccc-fb7d-4289-808e-21d31c703d1a.sock
srwxr-xr-x 1 root root 0 Mar 18 03:42 vscode-ipc-aa7aed50-92e4-4b2b-b17e-d70c1bba595e.sock
-rw-r--r-- 1 root root 2342 Mar 18 03:46 vscode-remote-containers-6a199ce05d20a43a350860289798f388414d648c.js
srwxr-xr-x 1 root root 0 Mar 18 03:46 vscode-remote-containers-ipc-6a199ce05d20a43a350860289798f388414d648c.sock
srwxr-xr-x 1 root root 0 Mar 18 03:46 vscode-ssh-auth-6a199ce05d20a43a350860289798f388414d648c.sock
drwxr-xr-x 2 root root 4096 Mar 18 03:46 vscode-typescript0
root@5e6a10efbea6:/tmp#
这是有效的:
{
"name": "my_docker_environment",
"dockerFile": "Dockerfile",
"extensions": [
"ms-vscode.cpptools",
"twxs.cmake",
"eamodio.gitlens",
"ms-vscode.cmake-tools"
],
"containerEnv": {
"DISPLAY": "unix:0"
},
"mounts": [
"source=/tmp/.X11-unix,target=/tmp/.X11-unix,type=bind,consistency=cached"
],
"runArgs": ["--privileged"]
}
我不记得是否需要"runArgs": ["--privileged"]
,但我想没有
您可能需要执行
xhost local:root
在主机上的终端上,然后启动您的应用
在macOS上测试,devcontainer.json
中唯一使用的额外设置:
"containerEnv": {
"DISPLAY": "docker.for.mac.host.internal:0"
},
在主机xhost +localhost
上运行devcontainer 之前
关于如何在互联网上使用vscode进行x11转发,有许多不同的解决方案。Docker 23启用了Docker buildkit作为标准,一些GUI应用程序,包括openCV,不再使用这里提到的设置。
相反,在ubuntu 22.04、vscode 1.76.0和Docker 23.0.1上,以下最小的.devcontainer.json
文件为我启用了包括opencv在内的GUI:
{
"containerEnv": {
"DISPLAY": "${localEnv:DISPLAY}",
},
"remoteEnv": {
"DOCKER_BUILDKIT": "0",
},
"image": "YOUR_DOCKER_IMAGE",
"runArgs": [
"--volume=/tmp/.X11-unix:/tmp/.X11-unix",
]
}
有趣的阅读:
- https://github.com/microsoft/vscode-remote-release/issues/1409
- https://github.com/microsoft/vscode-remote-release/issues/550#issuecomment-501293753
为了避免xhost +
,可以使用.devcontainer.json
转发.Xauthority,如:
{
"image": "..."
"runArgs": [
"--net", "host",
"-e", "DISPLAY=:0",
"-e", "XAUTHORITY=/tmp/.Xauthority",
"-v", "${localEnv:HOME}/.Xauthority:/tmp/.Xauthority"
]
}