简容器
下面用 C 语言演示如何不依赖 Docker,手动组合所有命名空间来创建一个容器:
#define _GNU_SOURCE
#include
#include
#include
#include
#include
#define STACK_SIZE (1024 * 1024)
static char child_stack[STACK_SIZE];
int child_func(void *arg) {
// 1. 设置独立主机名
sethostname("mycontainer", 11);
// 2. 挂载新的 /proc
mount("proc", "/proc", "proc", 0, NULL);
mount("sysfs", "/sys", "sysfs", 0, NULL);
// 3. 切换根文件系统
// pivot_root(new_root, put_old) - 需要预先准备好根目录
// 4. 打印容器内部视角
printf("Container PID: %d\n", getpid());
printf("Container hostname: ");
fflush(stdout);
system("hostname");
// 5. 执行 shell
char *args[] = {"/bi

发表评论 取消回复