在linux上使用vscode,docker和Dev containers进行开发;Using VS Code and Dev containers to Develop on Linux
Posted: 2024-12-14 6:28
For Linux users.
发现dev container插件可以不必使用Docker Desktop,而是直接使用Docker CLI。这样就可以开启特权模式在linux上愉快的开发了。
The Dev Container plugin can use Docker CLI directly instead of Docker Desktop. This allows me to enable privileged mode and develop on Linux. It's like an IDE experience.
在SDK目录新建.devcontainer文件夹,在这个文件夹内创建Dockerfile和devcontainer.json,内容分别如下
Create a .devcontainer folder in the SDK directory, and within this folder, create a Dockerfile and devcontainer.json with the following codes:
Dockerfile:
devcontainer.json:
安装好dev container插件之后,vscode打开sdk文件夹,选择reopen in container即可。
After installing the Dev Container plugin, open the SDK folder in VS Code and select "Reopen in Container".
使用事项:1.默认打开SDK 文件夹,更新环境变量会弹出警告,但是不必担心,因为环境变量已写入。你可以选择手动添加自己需要的环境变量至"containerEnv"中以方便开发和测试。
Usage notes: Terminal navigates the SDK folder by default. Just ignore the warning of sed. The scripts env_install_toolchain.sh have finished installing the toolchain. You can also add the environment variables you want to the list of "containerEnv".
2. 新建终端,默认进入SDK 文件夹
Add a new terminal. The terminal enters the SDK folder.
3.已配置容器特权模式,设备映射,支持热拔插USB,可以使用ADB方便传文件,在自己项目中的build.sh 中就使用ADB进行上传和测试。该容器环境也支持直接运行rkflash.sh进行烧写.
The container runs in privileged mode. And we map the host /dev folder to the container /dev folder. So you can easily use ADB even within dynamically plugged USB devices. The script rkflash.sh can also work. 4. 可选按F1配置ADB文件管理,进入基于adb的图形化文件管理,选择AdbFS: Setup Android ... Workspace,忽略重新加载窗口。如果切换文件夹并且不保存工作区,ADB file会自动关闭,可以再次按F1启动.
You can press F1 to active AdbFS to preview the pictures or download the files. I recommend not saving the workspace and activating AdbFS again to prevent potential ADB problems when you are switching folders.
即可方便地预览照片
Preview the picture in an elegant way. 5. 容器已经安装部分插件,可以更方便进行项目配置和代码编写。可以修改devcontainer.json的"extensions":进行个性化,添加插件时使用插件ID 比如mutantdino.resourcemonitor。
Some add-ons or plugins are preinstalled. You can customise the " extensions " array in devcontainer.json by adding/removing plugin IDs like mutantdino.resourcemonitor.
6. 已配置SDK交叉编译和wiki RKMPI示例所需的环境变量,因此make相关插件可检测到工具链并正常工作。可以在SDK目录下新建项目并打开项目文件夹作为临时工作区。图片以wiki RKMPI例程为例。因为ADB file插件会修改工作区,因此切换文件夹时可能会询问是否保存,选否即可。
The environment variables required for SDK cross-compilation and the RKMPI example in Wiki have been configured. So, you can switch to your project folder(in the SDK folder) in the container with the installed toolchain. Because the ADB file plugin modifies the workspace, you may be prompted to save changes when switching folders. Simply select 'No'. Here are RKMPI examples in Wiki for reference.
可以看到make插件可以正确识别工具链,意味着工具链安装成功(因为部分环境变量,因此这不意味着make完全工作)。
You can see that the Cmake plugin recognizes the toolchain, which means the toolchain is successfully installed (though due to partial environment variables, this doesn't necessarily mean Cmake will work completely). 尝试编译
Compiling
成功
Successful!
实现以上功能,尤其是USB,buildroot相关)的前提:
To fully access the functions, especially related to USB and Buildroot, make sure:
More images: 有一说一五张照片有点紧
发现dev container插件可以不必使用Docker Desktop,而是直接使用Docker CLI。这样就可以开启特权模式在linux上愉快的开发了。
The Dev Container plugin can use Docker CLI directly instead of Docker Desktop. This allows me to enable privileged mode and develop on Linux. It's like an IDE experience.
在SDK目录新建.devcontainer文件夹,在这个文件夹内创建Dockerfile和devcontainer.json,内容分别如下
Create a .devcontainer folder in the SDK directory, and within this folder, create a Dockerfile and devcontainer.json with the following codes:
Dockerfile:
Code: Select all
FROM ubuntu:22.04
RUN ln -snf /usr/share/zoneinfo/$CONTAINER_TIMEZONE /etc/localtime && echo $CONTAINER_TIMEZONE > /etc/timezone
RUN apt-get update && apt-get install -y tzdata
RUN apt update && apt-get install -y git ssh make gcc gcc-multilib g++-multilib module-assistant expect g++ gawk texinfo libssl-dev bison flex fakeroot cmake unzip gperf autoconf device-tree-compiler libncurses5-dev pkg-config bc python-is-python3 passwd openssl openssh-server openssh-client vim file cpio rsync usbutils adb
RUN apt-get clean && apt-get autoremove && rm -rf /var/lib/apt/lists/*
Code: Select all
{
"name": "LuckfoxPico",
"build": {
"dockerfile": "Dockerfile"
},
"customizations": {
"vscode": {
"settings": {
"terminal.integrated.shell.linux": "/bin/bash"
},
"extensions": [ "Erol444.adb-files",
"ms-vscode.cpptools",
"ms-vscode.cpptools-extension-pack",
"twxs.cmake",
"ms-vscode.cmake-tools",
"ms-vscode.cpptools-themes"]
}
},
"runArgs": ["--privileged","-v","/dev:/dev"],
"containerEnv": {
"SDK_PATH": "/workspaces/luckfox-pico",
"LUCKFOX_SDK_PATH": "/workspaces/luckfox-pico"
},
"postCreateCommand": "bash -c 'cd $SDK_PATH/tools/linux/toolchain/arm-rockchip830-linux-uclibcgnueabihf/ && source env_install_toolchain.sh'"
}
After installing the Dev Container plugin, open the SDK folder in VS Code and select "Reopen in Container".
使用事项:1.默认打开SDK 文件夹,更新环境变量会弹出警告,但是不必担心,因为环境变量已写入。你可以选择手动添加自己需要的环境变量至"containerEnv"中以方便开发和测试。
Usage notes: Terminal navigates the SDK folder by default. Just ignore the warning of sed. The scripts env_install_toolchain.sh have finished installing the toolchain. You can also add the environment variables you want to the list of "containerEnv".
2. 新建终端,默认进入SDK 文件夹
Add a new terminal. The terminal enters the SDK folder.
3.已配置容器特权模式,设备映射,支持热拔插USB,可以使用ADB方便传文件,在自己项目中的build.sh 中就使用ADB进行上传和测试。该容器环境也支持直接运行rkflash.sh进行烧写.
The container runs in privileged mode. And we map the host /dev folder to the container /dev folder. So you can easily use ADB even within dynamically plugged USB devices. The script rkflash.sh can also work. 4. 可选按F1配置ADB文件管理,进入基于adb的图形化文件管理,选择AdbFS: Setup Android ... Workspace,忽略重新加载窗口。如果切换文件夹并且不保存工作区,ADB file会自动关闭,可以再次按F1启动.
You can press F1 to active AdbFS to preview the pictures or download the files. I recommend not saving the workspace and activating AdbFS again to prevent potential ADB problems when you are switching folders.
即可方便地预览照片
Preview the picture in an elegant way. 5. 容器已经安装部分插件,可以更方便进行项目配置和代码编写。可以修改devcontainer.json的"extensions":进行个性化,添加插件时使用插件ID 比如mutantdino.resourcemonitor。
Some add-ons or plugins are preinstalled. You can customise the " extensions " array in devcontainer.json by adding/removing plugin IDs like mutantdino.resourcemonitor.
6. 已配置SDK交叉编译和wiki RKMPI示例所需的环境变量,因此make相关插件可检测到工具链并正常工作。可以在SDK目录下新建项目并打开项目文件夹作为临时工作区。图片以wiki RKMPI例程为例。因为ADB file插件会修改工作区,因此切换文件夹时可能会询问是否保存,选否即可。
The environment variables required for SDK cross-compilation and the RKMPI example in Wiki have been configured. So, you can switch to your project folder(in the SDK folder) in the container with the installed toolchain. Because the ADB file plugin modifies the workspace, you may be prompted to save changes when switching folders. Simply select 'No'. Here are RKMPI examples in Wiki for reference.
可以看到make插件可以正确识别工具链,意味着工具链安装成功(因为部分环境变量,因此这不意味着make完全工作)。
You can see that the Cmake plugin recognizes the toolchain, which means the toolchain is successfully installed (though due to partial environment variables, this doesn't necessarily mean Cmake will work completely). 尝试编译
Compiling
成功
Successful!
实现以上功能,尤其是USB,buildroot相关)的前提:
- 已经安装Docker CLI和Docker Compose,保证Docker守护进程正常运行。
- 配置权限和用户组,以确保当前用户可以直接使用docker。
Code: Select all
sudo usermod -aG docker $USER
- VS Code使用unix:///var/run/docker.sock 而不是 Docker Desktop的sock比如unix:///home/USERNAME/.docker/desktop/docker.sock。
Code: Select all
export DOCKER_HOST=unix:///var/run/docker.sock #更新环境变量使其生效
- 检查var/run/docker.sock文件权限是不是可以被访问和执行
- 重启Docker服务前注意先更新配置
To fully access the functions, especially related to USB and Buildroot, make sure:
- Docker CLI and Docker Compose have been installed, and the Docker daemon is running properly.
- Configure permissions and user groups to ensure the current user can directly use Docker.
Code: Select all
sudo usermod -aG docker $USER
- VS Code uses unix:///var/run/docker.sock instead of Docker Desktop's socket, such as unix:///home/USERNAME/.docker/desktop/docker.sock.
Code: Select all
export DOCKER_HOST=unix:///var/run/docker.sock #Update DOCKER_HOST environment variable
- Check the permissions for the /var/run/docker.sock file.
- Update the configuration before you restart the Docker daemon.
More images: 有一说一五张照片有点紧