Skip to content

实现一个简易的容器引擎,中国科学院大学研究生课程《云计算技术》大作业

License

Notifications You must be signed in to change notification settings

MiaoHao-oops/mycontainer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mycontainer

A tiny toy container runtime written in 200 lines of C.

quick start

Prerequisites

mycontainer runs smoothly with the following prerequisites:

The following build instructions were tested under Ubuntu 22.04.

Step 1: make rootfs from docker image

First, we should make a rootfs for our container. There are many ways to build rootfs from scratch, namely, busybox, shell command lines, and etc. In this document, I will demostrate an unusual way to build a rootfs, which take advantages of docker images.

  1. export an archive from a docker image(centos for example)

    docker export $(docker create centos) --output="centos.tar"
  2. extract the archive to a directory

    mkdir rootfs
    tar -xf centos.tar -C rootfs
  3. check the rootfs

    $ ls rootfs
    bin  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
    dev  home  lib64  media       opt  root  sbin  sys  usr

Step 2: compile and run

  1. edit macro ROOT_PATH in main.c, change it to the absolute path of your own rootfs

    -#define ROOT_PATH "/home/haooops/Documents/CST/Projects/mycontainer/rootfs"
    +#define ROOT_PATH "/absolute/path/to/your/own/rootfs"
  2. compile and run

    make run

    The shell will ask for your root password. After entering the password, the container will start up.

  3. check if the container is in a new PID namespace

    ps -e

    the shell will show:

    [root@75a80a52 /]# ps -e
        PID TTY          TIME CMD
          1 ?        00:00:00 bash
         12 ?        00:00:00 ps
  4. check the network

    ip a

    the shell will show:

    [root@75a80a52 /]# ip a
    1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    2: eth0@if30: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
        link/ether f2:bc:91:44:c8:40 brd ff:ff:ff:ff:ff:ff link-netnsid 0
        inet 172.17.0.82/16 scope global eth0
        valid_lft forever preferred_lft forever
        inet6 fe80::f0bc:91ff:fe44:c840/64 scope link 
        valid_lft forever preferred_lft forever

    The container is at ip address 172.17.0.82/16.

About

实现一个简易的容器引擎,中国科学院大学研究生课程《云计算技术》大作业

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published