Skip to content

Pythonで作って学ぶミニミニDocker runtime

Notifications You must be signed in to change notification settings

todokr/mini-docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

81afa41 · Feb 12, 2023

History

13 Commits
Jun 27, 2021
Jun 23, 2021
Jun 26, 2021
Jun 27, 2021
Jun 24, 2021
Jun 24, 2021
Feb 12, 2023
Jun 27, 2021
Jun 27, 2021

Repository files navigation

Mini docker

学習用のなんちゃってDocker runtime

セットアップ

VMの作成と起動

$ vagrant up
$ vagrant ssh
$ sudo su -
# cat /etc/os-release
NAME="Ubuntu"
VERSION="20.10 (Groovy Gorilla)"
...

syscall用のPythonラッパーライブラリのインストール

# cd /vagrant/libs
# python3 ./setup.py install

// インストールできているかの確認。linuxが出てくればOK
# pip3 list | grep linux

Pythonライブラリのインストール

# cd /vagrant
# pipenv install --system

コマンド

Docker imageのpull

# ./bocker pull ubuntu

// or タグを指定
# ./bocker pull ubuntu:latest

コンテナの起動

# ./bocker run ubuntu /bin/bash

各種動作確認

hostとコンテナでPID名前空間が分離されている

コンテナから見ると、自身にPID = 1 が割り当てられている

host (VM)

# ps a
    PID TTY      STAT   TIME COMMAND
    698 tty1     Ss+    0:00 /sbin/agetty -o -p -- \u --noclear tty1 linux
  ...
  20414 pts/0    S      0:00 -bash
  20559 pts/0    S      0:00 /usr/bin/python3 ./bocker run --memory 100M ubuntu /bin/bash 
  ...
  20587 pts/1    R+     0:00 ps a

container

# ps a
    PID TTY      STAT   TIME COMMAND
      1 ?        S      0:00 /bin/bash
     24 ?        R+     0:00 ps a
hostとコンテナでUTS名前空間が分離されている

host (VM)

# hostname
vagrant
# hostname newhost
# hostname
newhost

container

# hostname
ubuntu_latest_e18ee394-791f-4df6-9833-2245f8fd5324
# hostname container
# hostname
container

host (VM)

# hostname
newhost
hostはコンテナのルートディレクトリ内を見られるが、コンテナは自身のルートディレクトリから外側を見ることができない

host (VM)

# touch /a.txt
# ls / | grep 'a.txt'
a.txt

container

# ls / | grep 'a.txt'
// nothing

# touch /b.txt
# ls / | grep 'b.txt'
b.txt

host (VM)

# ls /var/opt/app/container/ubuntu_latest_e18ee394-791f-4df6-9833-2245f8fd5324/cow_rw/ | grep 'b.txt'
b.txt
コンテナの最大CPU利用量を制限できる

host (VM)

// 1コアの25%までに制限
# ./bocker run --cpus 0.25 ubuntu /bin/bash

host (VM - another terminal)

// CPUの状況をグラフィカルに表示するかっこいいツールのインストール
# pip3 install s-tui
# s-tui

container

# yes > /dev/null
コンテナの最大メモリ利用量を制限できる

host (VM)

# ./bocker run --memory 100M ubuntu /bin/bash

host (VM - another terminal)

$ sudo su -
# htop -d 0.3 -p {pid}

container

# /dev/null < $(yes)
yes: standard output: Broken pipe
Killed

参考

About

Pythonで作って学ぶミニミニDocker runtime

Resources

Stars

Watchers

Forks