Giter Club home page Giter Club logo

cpp-container's Introduction

About

This is a C++ rewrite of Lizzie Dixon's excellent C container here:
https://blog.lizzie.io/linux-containers-in-500-loc.html

This version simplifies and cleans up the code a bit. Still approximately 600 lines of code, but easier to read. Now with no gotos!
It would be interesting to extract the container code into a library, then you could create a container in something like 10 lines. But at that point you would basically just have a less featured, less secure version of libcontainer.

NOTE: This is just a proof of concept

  • It doesn't have basic, nice things like networking, package management, file system overlays, any sort of Dockerfile/makefile/recipe support, code reviews, testing, battle hardened development history, etc.
  • It's not Docker, it's a proof of concept of the basics of how containers work that doesn't hide everything away in Python/Go or libraries.
  • It runs as root
  • It modifies files as root, just one directory below other important system files, use at your own risk!

Building

Install dependencies:

sudo apt install libcap-dev libseccomp-dev

OR

sudo dnf install libcap-devel libseccomp-devel

Build cpp-container:

cmake .
make

Running

Extract busybox:

BUSYBOX_VERSION=1.33.0
(mkdir busybox-${BUSYBOX_VERSION} && cp busybox-${BUSYBOX_VERSION}.tar.xz ./busybox-${BUSYBOX_VERSION} && cd busybox-${BUSYBOX_VERSION} && tar -xf busybox-${BUSYBOX_VERSION}.tar.xz && rm busybox-${BUSYBOX_VERSION}.tar.xz)

Run /bin/sh in a container:

sudo ./cpp-container -h myhostname -m $(realpath ./busybox-${BUSYBOX_VERSION}/) -u 0 -c /bin/sh

Which user are we running as in the container?

/ # whoami
root

If we run pstree on the host we have this structure:
bash───sudo───cpp-container───sh

On the host:

$ cat
<expecting input>

The above cat process has a pid of 265314. Trying to kill it from the container fails:

/ # kill 265314
sh: can't kill pid 265314: No such process

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.