Coder Social home page Coder Social logo

container_from_scratch's Introduction

container_from_scratch

Tested container from Liz Rice on Ubuntu 20.04 LTS, release 20.04

Follow tutorial from:

https://www.youtube.com/watch?v=8fi7uSYlOdc
fork on github from: https://github.com/lizrice/containers-from-scratch
you can easily edit main.go code on Ubuntu Text Editor

Golang references for palackges and library function parameters:

https://pkg.go.dev/syscall
https://man7.org/linux/man-pages/man2/clone.2.html

Concepts:

kernel space
userspace
processes
system calls
operating system internals and resources
linux

Ran on ubuntu:

Distributor ID: Ubuntu
Description: Ubuntu 20.04 LTS
Release: 20.04
Codename: focal

Install these packages if not already present:

$sudo
$snap install docker
$apt install docker.io
$apt install go
$apt install golang-go

From container: successful run as root:

root@ubuntu:/home/samira/cont# go build # main.go
root@ubuntu:/home/samira/cont# ./cont run /bin/bash
RunningP1 [/bin/bash] as 18431
RunningP2 [/bin/bash] as 1
root@CONTAINER:/#

Test max pids set from host running a fork bomb:

root@CONTAINER:/# :(){ : | : & }; :

[1] 17
root@CONTAINER:/# bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: Resource temporarily unavailable
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: Resource temporarily unavailable
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: Resource temporarily unavailable
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: Resource temporarily unavailable
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: retry: No child processes
bash: fork: Resource temporarily unavailable
bash: fork: Resource temporarily unavailable
^C
[1]+ Done : | :
root@CONTAINER:/#

On host, $ps -fax will show no more than 20 defunct processes :)

samira@ubuntu:/sys/fs/cgroup/pids$ cat /sys/fs/cgroup/pids/samira/pids.max
20
samira@ubuntu:/sys/fs/cgroup/pids$ cat /sys/fs/cgroup/pids/samira/notify_on_release
1
samira@ubuntu:/sys/fs/cgroup/pids$ cat /sys/fs/cgroup/pids/samira/cgroup.procs
18734
18738
samira@ubuntu:/sys/fs/cgroup/pids$
samira@ubuntu:/sys/fs/cgroup/pids/samira$ cat pids.current
samira@ubuntu:/sys/fs/cgroup/pids/samira$ cat tasks
18734
18735
18736
18737
18738
samira@ubuntu:/sys/fs/cgroup/pids/samira$

samira@ubuntu:/sys/fs/cgroup/pids/samira$ ps -fax
10650 pts/0 Ss 0:00 | _ bash
16018 pts/0 S 0:00 | | _ su
16019 pts/0 S 0:00 | | _ bash
27424 pts/0 Sl 0:00 | | _ ./cont run /bin/bash
27428 pts/0 Sl 0:00 | | _ /proc/self/exe child /bin/bash
27433 pts/0 S+ 0:00 | | _ /bin/bash
27463 pts/0 Z 0:00 | | _ [bash]
27464 pts/0 Z 0:00 | | _ [bash]
27466 pts/0 Z 0:00 | | _ [bash]
27467 pts/0 Z 0:00 | | _ [bash]
27468 pts/0 S 0:00 | | _ /bin/bash
27469 pts/0 S 0:00 | | _ /bin/bash
27470 pts/0 S 0:00 | | _ /bin/bash
27471 pts/0 S 0:00 | | _ /bin/bash
27472 pts/0 S 0:00 | | _ /bin/bash
27473 pts/0 S 0:00 | | _ /bin/bash
27474 pts/0 S 0:00 | | _ /bin/bash
27475 pts/0 S 0:00 | | _ /bin/bash
27482 pts/0 S 0:00 | | | _ /bin/bash
27476 pts/0 S 0:00 | | _ /bin/bash
11046 pts/2 Ss 0:00 | _ bash
18819 pts/2 S 0:00 | | _ su
18821 pts/2 S+ 0:00 | | _ bash
11378 pts/1 Ss+ 0:00 | _ bash
24118 pts/3 Ss 0:00 | _ bash
27520 pts/3 R+ 0:00 | _ ps -fax

after we run again the container:

can still see info from container process using sleep:
root@CONTAINER:/# sleep 100

from host:

samira@ubuntu:/sys/fs/cgroup/pids/samira$ ps -C sleep
PID TTY TIME CMD
27758 pts/0 00:00:00 sleep

Next, you can play with docker and kubernestes:
On docker, you can follow this tutorial and exercise running priviledged, setting max pids, etc:
ex: docker container run --pids-limit 20
I recommend:

  1. go through entire tutorial of installing on desktop, then building image, running app, persistance of data across app updates etc.
    https://www.docker.com/101-tutorial
  2. https://kubernetes.io/docs/tutorials/kubernetes-basics/

container_from_scratch's People

Contributors

olivamadrigal avatar

Watchers

 avatar

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.