From 4b432176d9b213dd1a4720a8add361ca7577aea8 Mon Sep 17 00:00:00 2001 From: jzsz-wsl <840863191@qq.com> Date: Thu, 25 Apr 2024 05:53:23 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .devcontainer/Dockerfile | 26 ++++++++++++++-- .devcontainer/devcontainer.json | 2 -- .vscode/tasks.json | 54 +++++++++++---------------------- SuperIDE.ini | 2 +- 4 files changed, 42 insertions(+), 42 deletions(-) diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 18ad12c..81ef655 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -1,3 +1,23 @@ -FROM jzsz/c-test:0.1 -# Install the gdb -# RUN apk add gdb \ No newline at end of file +FROM jzsz/ubuntu20.04-base + +USER root + +# 环境变量设置 +# ENV DEBIAN_FRONTEND=noninteractive +# ENV TZ Asia/Shanghai +# ENV LESSCHARSET=utf-8 + +# 安装开发工具 +# RUN apt-get update -y && apt-get install -y \ +# build-essential \ +# gdb \ +# gcc \ +# libc6-dev \ +# clang + +# 复制示例项目并更改权限 +# COPY ExampleProject /workspaces/ExampleProject +# RUN chown -R superIDE /workspaces & + +# 切换到普通用户 +# USER superIDE diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 16126ed..b4bfd1c 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -5,6 +5,4 @@ "build": { "dockerfile": "Dockerfile" } - // "image": "jzsz/c-test:0.1", - // "workspaceFolder": "/app/project" } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 23a0ee3..6a72169 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -4,48 +4,30 @@ { "label": "Build", "type": "shell", - "command": "gcc", + "command": "make", //命令行命令 "args": [ - "-g", - "./src/source.c", - "-o", - "./target" + // "命令行命令参数" ], "group": { "kind": "build", "isDefault": true } }, - { - "label": "Check", - "type": "shell", - "command": "clang", - "args": [ - "--analyze", - "./target" - ], - "dependsOn": [ - "Build" - ], - "group": { - "kind": "test", - "isDefault": true - } - }, - { - "label": "Run", - "type": "shell", - "command": "./target", - "args": [ - - ], - "dependsOn": [ - "Build" - ], - "group": { - "kind": "test", - "isDefault": true - } - } + // { + // "label": "Check", + // "type": "shell", + // "command": "clang",//测试命令 + // "args": [ + // "--analyze", + // "./target" + // ], + // "dependsOn": [ + // "Build" + // ], + // "group": { + // "kind": "test",//操作分类 + // "isDefault": true + // } + // } ] } \ No newline at end of file diff --git a/SuperIDE.ini b/SuperIDE.ini index 1c55f7a..2aa5faf 100644 --- a/SuperIDE.ini +++ b/SuperIDE.ini @@ -8,4 +8,4 @@ ; Please visit documentation for the other options and examples ; https://docs.superide.org/page/projectconf.html [env:testdemo] -env_image = jzsz/c-test:0.1 \ No newline at end of file +env_image = jzsz/ubuntu20.04-c \ No newline at end of file -- Gitee From 433c2268c4b50b253178d7db5b8597890b24a29c Mon Sep 17 00:00:00 2001 From: jzsz-wsl <840863191@qq.com> Date: Fri, 26 Apr 2024 18:10:15 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=AE=8C=E5=96=84=E4=BA=86=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E7=A4=BA=E4=BE=8B=E9=A1=B9=E7=9B=AE=E6=9E=84=E5=BB=BA?= =?UTF-8?q?=E5=BC=80=E5=8F=91=E5=AE=B9=E5=99=A8=E9=95=9C=E5=83=8F=E7=9A=84?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .devcontainer/Dockerfile | 51 +++++--- ...66\344\275\234\346\214\207\345\215\227.md" | 113 ++++++++++++++++++ .vscode/tasks.json | 48 +++++--- README.md | 48 +------- 4 files changed, 180 insertions(+), 80 deletions(-) create mode 100644 ".devcontainer/\345\274\200\345\217\221\347\216\257\345\242\203\345\256\271\345\231\250\351\225\234\345\203\217\345\210\266\344\275\234\346\214\207\345\215\227.md" diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 81ef655..7fe8a91 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -1,23 +1,40 @@ -FROM jzsz/ubuntu20.04-base - -USER root +FROM ubuntu:20.04 +### 以下为基础镜像的构建命令,请不要改动 +# 设置工作目录 +WORKDIR /workspaces/project # 环境变量设置 -# ENV DEBIAN_FRONTEND=noninteractive -# ENV TZ Asia/Shanghai -# ENV LESSCHARSET=utf-8 +ENV DEBIAN_FRONTEND=noninteractive +ENV TZ Asia/Shanghai +ENV LESSCHARSET=utf-8 +# 安装基本工具 +RUN apt-get update && apt-get install -y \ + build-essential git net-tools wget sudo && \ + apt-get autoremove -y && \ + rm -rf /tmp/* /var/lib/apt/lists/* /var/tmp/* +# 创建用户,容器启动挂载文件时id为1000 +ENV USERNAME=superIDE +ARG USER_UID=1000 +ARG USER_GID=1000 +RUN groupadd -g $USER_GID $USERNAME \ + && useradd -rm -d /home/$USERNAME -s /bin/bash -u $USER_UID -g $USER_GID $USERNAME \ + && chmod g+rw /home + + +### 以下为制作容器镜像时需要修改的区域 +# # 环境变量设置 +# ENV VARIABLE=new_value + +# # 安装基本工具 +# RUN apt-get update && apt-get install -y \ +# pacages \ -# 安装开发工具 -# RUN apt-get update -y && apt-get install -y \ -# build-essential \ -# gdb \ -# gcc \ -# libc6-dev \ -# clang -# 复制示例项目并更改权限 -# COPY ExampleProject /workspaces/ExampleProject -# RUN chown -R superIDE /workspaces & +### 以下内容在制作容器镜像时取消注释 +# # 复制示例项目并更改权限 +# COPY ./. /workspaces/ExampleProject +# RUN rm -rf /workspaces/ExampleProject/.devcontainer /workspaces/ExampleProject/.git +# RUN chown -R superIDE /workspaces -# 切换到普通用户 +# # 切换到普通用户 # USER superIDE diff --git "a/.devcontainer/\345\274\200\345\217\221\347\216\257\345\242\203\345\256\271\345\231\250\351\225\234\345\203\217\345\210\266\344\275\234\346\214\207\345\215\227.md" "b/.devcontainer/\345\274\200\345\217\221\347\216\257\345\242\203\345\256\271\345\231\250\351\225\234\345\203\217\345\210\266\344\275\234\346\214\207\345\215\227.md" new file mode 100644 index 0000000..c1bc2d6 --- /dev/null +++ "b/.devcontainer/\345\274\200\345\217\221\347\216\257\345\242\203\345\256\271\345\231\250\351\225\234\345\203\217\345\210\266\344\275\234\346\214\207\345\215\227.md" @@ -0,0 +1,113 @@ +# 开发环境容器镜像制作指南 + +本教程用于制作符合 SuperIDE 使用标准的开发环境容器镜像。 + +## 选择基础镜像 + +SuperIDE 提供了定制开发环境的基础镜像和使用 Vs Code 中 Dev Container 插件制作镜像的示例项目。建议开发者以此项目为模板进行后续制作。 + +```shell +https://gitee.com/SuperIDE/DevContainerExample.git +``` + +## 配置开发环境 + +开发环境的最终配置需要写在`Dockerfile`中,可以先在 Dev Container 的命令行中进行测试,使用 Dev Container 插件的教程在项目的`README.md`文档中。 + +开发环境配置完成后,将最终需要保留的命令写在`.devcontainer/Dockerfile`中,例如需要设置环境变量并下载安装某个包: + +```shell +export VARIABLE=new_value +apt-get install pacages1 +``` + +在`Dockerfile`中为 + +```dockerfile +ENV VARIABLE=new_value +RUN apt-get install pacages +``` + +Tips:如果执行多条命令行语句使用&&将命令连接起来而不是使用多条`RUN`命令,因为每条 RUN 命令会在容器中多生成一层,具体参考[Dockerfile 指令](https://yeasy.gitbook.io/docker_practice/image/build) + +## 测试示例项目 + +最终的容器开发环境容器镜像需要一个示例项目,可以参考的示例项目构成是 Dev Container 打开的工作目录中除了`.devcontainer`文件夹的其余文件。可以在此基础上修改,也可以删除其余文件新建。 + +一个示例项目必须有的成员是`SuperIDE.ini`和`.vscode/tasks.json`。 + +其中`SuperIDE.ini`必须包含以下内容,分别是开发环境名称(可以自定义)和镜像名称(就是本镜像最终构建时的名称) + +```ini +[env:envname] +env_image = image_name +``` + +`.vscode/tasks.json`文件是[vscode 的任务设置文件](https://code.visualstudio.com/Docs/editor/tasks),SuperIDE 读取其完成构建,假如项目构建命令为`make`,命令行参数为`-j$(nproc)`则为一下形式,开发者可根据需要修改此文档。 + +```json +{ + "version": "2.0.0", + "tasks": [ + { + "label": "Build", //任务名称 + "type": "shell", + "command": "make", //命令行命令 + "args": [ + // "-j$(nproc)" //命令行命令参数 + ], + "group": { + "kind": "build", + "isDefault": true + } + } + ] +} +``` + +示例项目完成后可以使用 vscode 提供的`Task`功能测试:使用`Ctrl`+`Shift`+`P`打开命令行执行`Tasks: Run Build Task`命令,验证执行结果是否符合预期。 + +## 制作容器镜像 + +以上工作完成后,工作区文件夹包含一个示例项目,可以将目录下的 README.md 修改为镜像的说明文档,用来指导使用这个容器镜像的开发者。 +此时,制作容器镜像的`.devcontainer/Dockerfile`中含有配置开发环境命令: + +```dockerfile +FROM ubuntu20.04-base + +### 以下为基础镜像的构建命令,请不要改动 + + + +### 以下为制作容器镜像时需要修改的区域 +# 环境变量设置 +ENV VARIABLE=new_value + +# 安装基本工具 +RUN apt-get update && apt-get install -y \ + pacages \ + +# 复制示例项目并更改权限 + COPY ExampleProject /workspaces/ExampleProject + RUN chown -R superIDE /workspaces & + +# 切换到普通用户 + USER superIDE +``` + +注意需要复制示例项目到容器并更改权限,最后切换到 superIDE 用户(这些命令已预先写好并注释,制作容器时取消注释即可),这是 superIDE 使用时要求的。 + +最后退出 Dev Container,在命令行中打开`DevContainerExample`文件夹,使用以下命令将其构建为容器镜像,这里的镜像名称为`基础系统名+环境名`如`ubuntu20.04-c` + +```shell +docker build -f -t ubuntu20.04-envname . +``` + +## 上传开发环境 + +将构建好的容器镜像上传到仓库中,供开发者使用。这里的例子仓库是开发者自己的 DokcerHub,`username`是 Docker 账号名称 + +```shell +docker tag ubuntu20.04-envname username/ubuntu20.04-envname +docker push username/ubuntu20.04-envname +``` diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 6a72169..e36b72c 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -4,30 +4,42 @@ { "label": "Build", "type": "shell", - "command": "make", //命令行命令 + "command": "make", "args": [ - // "命令行命令参数" + ], "group": { "kind": "build", "isDefault": true } }, - // { - // "label": "Check", - // "type": "shell", - // "command": "clang",//测试命令 - // "args": [ - // "--analyze", - // "./target" - // ], - // "dependsOn": [ - // "Build" - // ], - // "group": { - // "kind": "test",//操作分类 - // "isDefault": true - // } - // } + { + "label": "Check", + "type": "shell", + "command": "echo", + "args": [ + "check" + ], + "dependsOn": [ + "Build" + ], + "group": { + "kind": "test", + "isDefault": true + } + }, + { + "label": "Run", + "type": "shell", + "command": "./target", + "args": [], + "dependsOn": [ + "Build" + ], + "group": { + "kind": "test", + "isDefault": true + } + } ] } \ No newline at end of file diff --git a/README.md b/README.md index ec128cf..2862ac6 100644 --- a/README.md +++ b/README.md @@ -31,57 +31,15 @@ make clean - 快捷键`Ctrl + Shift + P`打开命令面板输入`task` - 在列出的命令中选择想要执行的命令 -## 容器相关设置 +## 开发环境容器镜像制作指南 -### 指定容器镜像 - -`.devcontainer/devcontainer.json` 中的`image`可以指定开发环境容器镜像 - -```json -{ - "name": "My Dev Container", - "image": "ContainerImageName", - // 其他设置 -} -``` - -### 自定义开发环境 - -定义`.devcontainer/devcontainer.json` 中`build`中的`dockerfile`可以指定构建容器镜像的Dockerfile - -```json -{ - "name": "My Dev Container", - "build":{ - "dockerfile": "Dockerfile" - } - //其他设置 -} -``` - -修改`.devcontainer/Dockerfile` 可以自定义开发环境 - -``` -FROM mcr.microsoft.com/devcontainers/base:alpine-3.19 -# Install the gdb -RUN apk add gdb -``` +如果你想制作符合SuperIDE使用标准的开发环境容器镜像,请参考本项目目录下的`.devcontainer/开发环境容器镜像制作指南.md`文档 ## SuperIDE容器镜像标准 ### 开发环境容器镜像目录 -- /app - - - config.json 配置说明文件,toolchain根据内容获取示例项目、工作挂载目录等信息 - - ```json - { - "work_directory":"/app/project", - "example_directory": "/app/ExampleProject", - "tasks_file": "/app/project/.vscode/tasks.json", - } - ``` +- /workspace - project 工作时项目目录的挂载文件夹,也是容器默认工作目录 -- Gitee