一、项目介绍

1.项目简述
Code-Server是作为一款开源代码编辑器,把Visual Studio Code的功能在浏览器端展现出来。

2.项目功能
Code-Server 是由 Coder 开发的,把 VSCode搬到了浏览器上。我们可以在任何地方的任何机器上运行Code-Server然后通过浏览器访问它。

它不仅具备基本的代码编辑、语法高亮、智能感知和自动补全等特性,更支持多人协作和远程开发,为开发者提供了前所未有的便捷体验。

无论是本地计算机还是云服务器,只需部署CodeServer,用户即可通过浏览器随时随地访问并使用其中的编辑器功能,无需额外安装本地编辑器。

此外,它还支持丰富的自定义插件和扩展,满足了不同用户的个性化需求,使得开发过程更加高效、灵活。

3.项目开源地址
https://github.com/coder/code-server

二、项目搭建环境

1. 项目测试环境

A.项目映射在华为云centos8.2,外网地址为124.71.67.131,实际搭建在我的minipc,通过内网穿透映射到华为云进行访问
Linux hcss-ecs-031b 4.18.0-348.7.1.el8_5.x86_64 #1 SMP Wed Dec 22 13:25:12 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

B.docker版本为26.1.3,docker-compose版本为26.1.3
注意:本次实践部署环境为个人的测试环境,若是生产环境请谨慎部署;对应开启了容器的端口,在linux下和防火墙下需开放对应端口。

2. 本次项目实施过程

使用docker下载镜像,创建好项目需要挂载的路径,通过docker compose启动容器,启动容器后查看容器启动状态,查看容器的运行日志是否正常,以上全部正常执行后体验项目功能。

3.注意:docker下载镜像有可能遇到比较慢的情况,参考以下解决措施:

A.docker配置换源,进入/etc/docker的路径,如果没有就创建这个目录
cd /etc/docker/
mkdir -p /etc/docker

B.编辑配置文件
vim daemon.json   ##可以清空里面的内容:%d 然后复制下面的源进去wq保存

{
    "registry-mirrors":[
        "https://286u3d9d.mirror.aliyuncs.com"
    ]
}

C.registry-mirrors:指定了一个镜像仓库的 URL https://286u3d9d.mirror.aliyuncs.com。 这个配置项用于设置 Docker镜像的镜像仓库地址,使得在拉取和推送 Docker 镜像时能够通过该镜像仓库进行加速。这边提供的是广东广州服务器的镜源,建议个人自己去阿里云建一个个人账号,根据实际所在区获取镜源。

D.重新加载源,重启docker服务
sudo systemctl daemon-reload 
sudo systemctl restart docker

三、项目搭建前巡检

1. 检查docker是否正常运行
systemctl status docker
or
service docker status
注:我个人测试环境是使用systemctl进行管理,若有使用service管理请使用第二条的命令进行查看。   

[root@VM-8-12-centos ~]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2024-04-22 23:13:57 CST; 4 days ago
     Docs: https://docs.docker.com
 Main PID: 17092 (dockerd)
    Tasks: 158
   Memory: 142.3M
   CGroup: /system.slice/docker.service

若显示docker的Active是active (running),即表明docker是正常运行的。

2.一般我会使用docker-compose去管理,所以预先需要创建好yaml文件,vim docker-compose.yml,格式如下例子:

services:
    nginx:
        image: nginx
        logging:
            options:
                max-size: 1g
        restart: always
        volumes:
            - '/var/run/docker.sock:/tmp/docker.sock:ro'
        ports:
            - '80:80'

四、项目实施过程

1.根据开源项目,找到对应的镜像进行pull,若遇到很慢的情况,先检查是否网络问题以及是否已经换源。
docker pull codercom/code-server:latest

[root@VM-8-12-centos ~]# docker pull codercom/code-server:latest
Using default tag: latest
latest: Pulling from codercom/code-server
0e29546d541c Pull complete                                                                                              
c3eda2a3e95a Pull complete                                                                                             
2cc1256921e5 Pull complete                                                                                             
213268839765 Pull complete                                                                                            
29ef16b63f4b Pull complete                                                                                          
aa7799335937 Pull complete                                                                                            
bdb878c420c0 Pull complete                                                                                         
c077ba74ac32 Pull complete 
Digest: sha256:39a32a34a4e4ecd6e289d08d649f530441d5fe3c6cdb60bdc0f1fbc9d1f2bd26
Status: Downloaded newer image for codercom/code-server:latest
docker.io/codercom/code-server:latest

2.若已经下载完成显示新的一行,可以输入命令查看是否上一条命令执行成功
echo$?
若返回0,则成功;返回其他则根据实际情况重新下载或者查找原因。

3.docker下载完后,可以查看对应的镜像是否下载成功
docker images |grep code-server
 
4.下载成功后,编辑docker-compose.yml文件

services:
    code-server:
        image: 'codercom/code-server:latest'
        volumes:
            - './vscode/data:/root/.vscode-server'
        environment:
            - PASSWORD=11111111
        ports:
            - '8188:8080'
        container_name: code-server

编辑后输入wq进行保存

5.启动docker-compose

docker compose up -d  

6.启动容器后,查看容器的状态是否正常  

docker ps |grep  code-server
    
root@HEJIAJIN-PC:/opt# docker ps |grep  code-server
6cade3583776   codercom/code-server:latest                                     "/usr/bin/entrypoint…"   10 minutes ago   Up 10 minutes   0.0.0.0:8188->8080/tcp, :::8188->8080/tcp                                              code-server

7.启动容器后,查看容器的日志是否正常

docker logs -f code-server

root@HEJIAJIN-PC:/opt# docker logs -f code-server
[2024-06-12T08:14:50.159Z] info  Wrote default config file to ~/.config/code-server/config.yaml
[2024-06-12T08:14:50.815Z] info  code-server 4.0.1 735c6da829535969ff7193c79379299e4a1cb9bc
[2024-06-12T08:14:50.817Z] info  Using user-data-dir ~/.local/share/code-server
[2024-06-12T08:14:50.838Z] info  Using config file ~/.config/code-server/config.yaml
[2024-06-12T08:14:50.838Z] info  HTTP server listening on http://0.0.0.0:8080/ 
[2024-06-12T08:14:50.838Z] info    - Authentication is enabled
[2024-06-12T08:14:50.838Z] info      - Using password from $PASSWORD
[2024-06-12T08:14:50.838Z] info    - Not serving HTTPS 
[08:18:04] Extension host agent started.
Failed login attempt {"remoteAddress":"172.19.0.1","userAgent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0","timestamp":1718180289}
[08:18:15] [172.19.0.1][c01e8834][ManagementConnection] New connection established.
[08:18:16] [172.19.0.1][fd780351][ExtensionHostConnection] New connection established.
[08:18:16] [172.19.0.1][fd780351][ExtensionHostConnection] <76> Launched Extension Host Process.
[08:19:33] Installing extension: ms-ceintl.vscode-language-pack-zh-hans
[08:19:37] Downloaded extension: ms-ceintl.vscode-language-pack-zh-hans /home/coder/.local/share/code-server/CachedExtensionVSIXs/ms-ceintl.vscode-language-pack-zh-hans-1.63.3-universal
[08:19:37] Extracted extension to /home/coder/.local/share/code-server/extensions/.c1a80fe6-0265-4252-bad0-b4095e0de5f5: ms-ceintl.vscode-language-pack-zh-hans
[08:19:37] Renamed to /home/coder/.local/share/code-server/extensions/ms-ceintl.vscode-language-pack-zh-hans-1.63.3
[08:19:37] Extracting completed. ms-ceintl.vscode-language-pack-zh-hans
[08:19:37] Adding language packs from the extension ms-ceintl.vscode-language-pack-zh-hans
[08:19:37] Extension installed successfully: ms-ceintl.vscode-language-pack-zh-hans
[08:19:40] [172.19.0.1][c01e8834][ManagementConnection] The client has disconnected gracefully, so the connection will be disposed.
[08:19:40] [172.19.0.1][fd780351][ExtensionHostConnection] <76> Extension Host Process exited with code: 0, signal: null.
[08:19:41] [172.19.0.1][d4d324e5][ManagementConnection] New connection established.
[08:19:42] [172.19.0.1][6fa7efdb][ExtensionHostConnection] New connection established.
[08:19:42] [172.19.0.1][6fa7efdb][ExtensionHostConnection] <117> Launched Extension Host Process.

五、项目体验

注:云服务器记得放开防火墙!
访问地址http://124.71.67.131:8188/,欢迎点击玩一下!
ps:内网穿透搭建好了,minipc可以撑一段时间,这些测试的服务应该会保留一段时间...如果有需要体验的但是服务已经被我down了的,可以在微信公众号《零氪的云原生》私我开启!