尤川豪   ·  2年前
445 貼文  ·  275 留言

Docker 學習筆記

  • linux 本身有一種容器技術叫做 LXC. 有提供一套 manager 叫做 LXD.

  • docker 一開始就是一套幫助操作 LXC 的工具

https://unix.stackexchange.com/questions/254956/what-is-the-difference-between-docker-lxd-and-lxc


  • 2014年以前是用 LXC driver. 2014 年之後自行用 Go 開發了 libcontainer

  • 所以現在 docker 有兩種 driver.

https://www.docker.com/blog/docker-0-9-introducing-execution-drivers-and-libcontainer/

https://github.com/opencontainers/runc/tree/master/libcontainer



把玩一下 linux 原生支援的容器技術 lxc

  • sudo apt-get install lxc
  • sudo apt-get install lxc-templates
  • to see the cool things: ls /usr/share/lxc/templates/
  • to see what's going on: cat /usr/share/lxc/templates/lxc-ubuntu
  • sudo lxc-create lxc-test -t ubuntu
Current default time zone: 'Etc/UTC'
Local time is now:      Thu May 13 02:49:25 UTC 2021.
Universal Time is now:  Thu May 13 02:49:25 UTC 2021.

##
# The default user is 'ubuntu' with password 'ubuntu'!
# Use the 'sudo' command to run tasks as root in the container.
##
  • sudo lxc-start lxc-test
  • sudo lxc-info lxc-test
Name:           lxc-test
State:          RUNNING
PID:            14100
IP:             10.0.3.220
CPU use:        0.61 seconds
BlkIO use:      4.67 MiB
Memory use:     29.45 MiB
KMem use:       10.23 MiB
Link:           vethX6K21K
 TX bytes:      1.89 KiB
 RX bytes:      10.33 KiB
 Total bytes:   12.22 KiB
  • sudo lxc-console lxc-test
Connected to tty 1
Type <Ctrl+a q> to exit the console, <Ctrl+a Ctrl+a> to enter Ctrl+a itself

Ubuntu 18.04.5 LTS lxc-test pts/0

lxc-test login: ubuntu
Password: 
Last login: Thu May 13 03:11:58 UTC 2021 on pts/0
Welcome to Ubuntu 18.04.5 LTS (GNU/Linux 4.19.10-041910-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

  • to see all the commands: sudo lxc- + press tab
lxc-attach         lxc-copy           lxc-ls             lxc-unshare
lxc-autostart      lxc-create         lxc-monitor        lxc-update-config
lxc-cgroup         lxc-destroy        lxc-snapshot       lxc-usernsexec
lxc-checkconfig    lxc-device         lxc-start          lxc-wait
lxc-checkpoint     lxc-execute        lxc-stop           
lxc-config         lxc-freeze         lxc-top            
lxc-console        lxc-info           lxc-unfreeze   

可以發現 lxc 指令命名設計哲學就是 linux 指令 or 功能動詞,前面 prefix lxc-

  • 再深入 lxc 就不想學了. 反正就是 host OS 能夠跟 container 互動. 懶得去學 iptables 那些指令

https://www.digitalocean.com/community/tutorials/getting-started-with-lxc-on-an-ubuntu-13-04-vps

  • 如果要方便的 manager app 那就是去學 lxd.

  • https://github.com/lxc/lxc

  • https://github.com/lxc/lxd

  • 然後這應該就是 docker 一開始版本的實作內容. docker 只是一層方便的 wrapper.

  • 所以是有一群工程師覺得 lxd 太難用,所以自己寫了一個 docker 來操作 lxc?

  • 過幾年,這群人覺得連 lxc 都太難用,所以自己寫了一個 libcontainer?

  • 題外話,lxc 本身的指令是 lxc- prefix 開頭系列,lxd 本身的指令則是 lxc

  • 這個命名設計真糟糕!


目前的LXC使用下列核心功能來控制行程:

核心命名空間(行程間通訊、uts、mount、pid、network和user)
AppArmor和SELinux組態
Seccomp策略
chroot(使用pivot_root)
Kernel Capibilities
控制組(cgroups)
因此,LXC通常被認為介於「加強版」的chroot和完全成熟的虛擬機器之間的技術。LXC的目標是建立一個儘可能與標準安裝的Linux相同但又不需要分離核心的環境。
  • 我發現,lxc 根本就是一個指令包而已,實際上只是去組合使用 linux 的 chroot, cgroups, mount... 等等指令,來創造一個「接近獨立」的環境效果而已

我花了相當多的時間談論Linux容器,但是Docker呢?它是生產中部署最多的容器解決方案。自首次推出以來,Docker已經風靡Linux計算世界。 Docker是一種Apache許可的開源容器化技術,旨在自動化在容器內建立和部署微服務這類重複性任務。 Docker將容器視為非常輕量級和模組化的虛擬機器。最初,Docker是在LXC之上構建的,但它已經遠離了這種依賴,從而帶來了更好的開發人員和使用者體驗。與LXC非常相似,Docker繼續使用核心 cgroup 子系統。該技術不僅僅是執行容器,還簡化了建立容器,構建映像,共享構建的映像以及對其進行版本控制的過程。


LXC關鍵技術點:

chroot,根切換,從容器內的角度來看,彷彿真的有自己的根樹

namespaces:名稱空間,負責將資源隔離,比如pid,網路,mnt,user,uts等

CGroups:控制組,負責控制資源的分配

  • 所以 docker 一開始是想做個 developer experience (DX) 更好的 lxd. 後來索性連 lxc 都改寫.
  • 反正底下都是操作 chroot, cgroups, mount... 等等指令,來模擬出環境效果,稱之為容器而已!

那麼,如何 ssh 到容器裡面呢?原理是?

  • 參考腳本,我發現會有
# download a mini ubuntu into a cache
  • 核心原理就是用了 debootstrap

debootstrap is a tool which will install a Debian base system into a subdirectory of another, already installed system. It doesn't require an installation CD, just access to a Debian repository.

這些 images 的本質是什麼?https://us.images.linuxcontainers.org/

Ubuntu, Mac, Windows 運行 docker 的差別


把玩一下 debootstrap 吧

  • sudo debootstrap xenial xenial

xenial 就是其中一個 ubuntu 版本的代號而已,基本上這些代號都可以用 http://archive.ubuntu.com/ubuntu/dists/

howtomakeaturn@howtomakeaturn-UX533FD:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.3 LTS
Release:    18.04
Codename:   bionic
root@howtomakeaturn-UX533FD:/home# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04 LTS
Release:    16.04
Codename:   xenial

2006年,Linux核心中開發出cgroups。2007年,被加到Linux 2.6.24版核心中。2008年,基於cgroups,開發出LXC,以及Docker。2013年被加入Linux 3.8版中。

  • 這就是現代 DevOps 的由來與原理吧!

  • 都能夠以 root 身份在裡面操作了,所謂「ssh 到容器裡面」,就是以此為基礎,加上一些網路設定的原理,所做出的效果吧!

  分享   共 2,309 次點閱
按了喜歡:
共有 0 則留言
還沒有人留言。歡迎分享您的觀點、或是疑問。
您的留言
尤川豪
445 貼文  ·  275 留言

Devs.tw 是讓工程師寫筆記、網誌的平台。隨手紀錄、寫作,方便日後搜尋!

歡迎您一起加入寫作與分享的行列!

查看所有文章