使用Code Server容器部署VS Code Online

本文最后更新于:November 11, 2022 am

使用Code Server容器部署VS Code Online

前言

考虑到在服务器上直接部署Code Server将端口暴露,非常的不安全。这时候使用Docker将Code Server在容器中运行,相对来说会更加安全。但是在实际配置运行过程中会有各种的问题,如容器权限管理,SSL证书/HTTPS,Code Server更新等。根据实际过程的踩坑,将教程整理为本文。

开始之前

首先要准备一台服务器,Code Server文档的最低推荐配置是1GB of RAM,2 CPU Cores。
我选用的是腾讯云的轻量云服务器配置为2c4g8m,CentOS 7。

Docker Engine

要在服务器运行Docker需要安装Docker Engine,建议通过Docker Docs给出的说明进行安装,非常不建议直接apt/yum安装仓库中的包(特别是CentOS这种以老旧稳定著称的发行版)。在文档中选取对应系统,参照文档进行安装即可。
非root用户下运行docker请参考此文档

Code Server

有关Code Server的介绍可以参考其官网。从dockerhub中直接拉取最新的Code Server镜像

1
docker pull codercom/code-server:latest

Code Server镜像是基于Debian buster构建,非常干净,需要自己安装所需的软件包。由于我不止部署了一个Code Server(多人使用),考虑到不同的Code Server中版本升级和文件传输备份问题,所以我选择在服务器(宿主机)上每个Code Server容器创建对应的home文件夹,并挂载到容器/home/coder/(Code Server默认的home路径)。同时使用dockerfile来构建我所需的Code Server镜像(A在学习C,B只用Python)。

Dockerfile

我需要分别构建C和Python的两个镜像(当然也可以一个里全装,就是镜像体积会很大)。

Dockerfile for Python

多行写入其实可以用cat,但是在dockerfile中似乎语法不支持,只能用echo一行行写了。
当然apt在dockerfile的编译环境也是会报错的,要用apt-get。具体区别的话可以Google一下。

1
2
3
4
5
6
7
8
9
FROM codercom/code-server
RUN sudo mv /etc/apt/sources.list /etc/apt/sources.list.bak \
&& echo 'deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free' >> /home/coder/sources.list \
&& echo 'deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free' >> /home/coder/sources.list \
&& echo 'deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free' >> /home/coder/sources.list \
&& echo 'deb https://mirrors.tuna.tsinghua.edu.cn/debian-security buster/updates main contrib non-free' >> /home/coder/sources.list \
&& sudo mv /home/coder/sources.list /etc/apt/ \
&& sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get install -y fonts-powerline python3 python3-pip vim \
&& sudo apt-get clean

Docker for C/Cpp

1
2
3
4
5
6
7
8
9
FROM codercom/code-server
RUN sudo mv /etc/apt/sources.list /etc/apt/sources.list.bak \
&& echo 'deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free' >> /home/coder/sources.list \
&& echo 'deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free' >> /home/coder/sources.list \
&& echo 'deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free' >> /home/coder/sources.list \
&& echo 'deb https://mirrors.tuna.tsinghua.edu.cn/debian-security buster/updates main contrib non-free' >> /home/coder/sources.list \
&& sudo mv /home/coder/sources.list /etc/apt/ \
&& sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get install -y fonts-powerline build-essential gdb vim \
&& sudo apt-get clean

构建镜像

参考docker build命令

1
docker build -t 构建的镜像名字 -f dockerfile文件名 --no-cache . 

最后是有一点. 的指当前路径。

运行Code Server

Code Server是支持ssl证书的,参考其文档。因为我使用Code Server容器的话,所以直接在Docker运行配置中添加对应参数即可。运行命令如下:

1
2
3
4
5
6
7
docker run -dit --cap-add SYS_PTRACE --name 命名运行的容器 -p 0.0.0.0:8100:8080 \
-v /宿主机中的创建的home文件夹路径:/home/coder \
-v /宿主机中证书路径/cert.pem:/容器中证书路径/cert.pem \
-v /宿主机中公钥路径/key.pem:/容器中公钥路径/key.pem \
-u "$(id -u):$(id -g)" \
-e PASSWORD=设置的登录密码 \
构建的镜像名字 --cert /容器中证书路径/cert.pem --cert-key /容器中公钥路径/key.pem
  • –cap-add SYS_PTRACE 给容器添加权限(否则gdb会无法运行)
  • –name 给运行容器命名(需要符合规范:英文字符1-英文字符2)
  • -p 0.0.0.0:8100:8080 8100是服务器(宿主机)上的端口也是实际访问的端口,需要在防火墙中放开,自行修改;8080是Code Server默认的运行端口不需要修改。

升级Code Server

在容器中直接运行Code Server的升级命令应该是可以的,但是我有洁癖不是很喜欢。使用dockerfile构建镜像,home目录本地挂载使得可以直接删除镜像,重复build镜像和run容器(使用与之前完全相同的命令),就可实现更新Code Server,同时保留所有扩展设置和文件。

References


使用Code Server容器部署VS Code Online
https://yyhua.me/2021/082307.html
Author
Yangyang Hua
Posted on
August 23, 2021
Licensed under