解析 /var/run/docker.sock

image.png

docker.sock 是docker client 和docker daemon 在localhost进行通信的socket文件。
今天我们直接call这个socket文件来创建容器,启动容器。(其实就是直接call docker daemon API而不是通过docker client的方式去操控docker daemon)。

什么是unix socket?

unix socket可以让一个程序通过类似处理一个文件的方式和另一个程序通信,这是一种进程间通信的方式(IPC)。
当你在host上安装并且启动好docker,docker daemon 会自动创建一个socket文件并且保存在/var/run/docker.sock目录下。docker daemon监听着socket中即将到来的链接请求(可以通过-H unix:///var/run/docker.sock设定docker daemon监听的socket文件,-H参数还可以设定监听tcp:port或者其它的unix socket),当一个链接请求到来时,它会使用标准IO来读写数据。

创建容器

# curl -XPOST --unix-socket /var/run/docker.sock -d '{"Image":"nginx"}' -H 'Content-Type: application/json' http://localhost/containers/create
{"Id":"a4bc1943984d7fccd902ac9a5f5966b72b438a2f4f5764f319e181b601dbc886","Warnings":null}
# curl -XPOST --unix-socket /var/run/docker.sock http://localhost/containers/a4bc1943984d7fccd902ac9a5f5966b72b438a2f4f5764f319e181b601dbc886/start
# docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
a4bc1943984d        nginx               "nginx -g 'daemon of…"   2 minutes ago       Up 22 seconds       80/tcp              zealous_buck

docker API 还暴露出event接口来记录操作API的事件

curl --unix-socket /var/run/docker.sock http://localhost/events

当用docker client 进行操作时,会看到打印出来的event

docker run -it --rm nginx sh
# curl --unix-socket /var/run/docker.sock http://localhost/events
{"status":"create","id":"94e4de2eed35056c5434c84e544bc429b84cfbf4f99b458464eb87f3394eaead","from":"nginx","Type":"container","Action":"create","Actor":{"ID":"94e4de2eed35056c5434c84e544bc429b84cfbf4f99b458464eb87f3394eaead","Attributes":{"image":"nginx","maintainer":"NGINX Docker Maintainers \u003cdocker-maint@nginx.com\u003e","name":"ecstatic_mendeleev"}},"scope":"local","time":1560835913,"timeNano":1560835913703025294}
{"status":"attach","id":"94e4de2eed35056c5434c84e544bc429b84cfbf4f99b458464eb87f3394eaead","from":"nginx","Type":"container","Action":"attach","Actor":{"ID":"94e4de2eed35056c5434c84e544bc429b84cfbf4f99b458464eb87f3394eaead","Attributes":{"image":"nginx","maintainer":"NGINX Docker Maintainers \u003cdocker-maint@nginx.com\u003e","name":"ecstatic_mendeleev"}},"scope":"local","time":1560835913,"timeNano":1560835913704314497}
{"Type":"network","Action":"connect","Actor":{"ID":"b049de6ba0d38d1af9d93487b60ed861a08e65ecf3a644b0e6745fa903eb3d06","Attributes":{"container":"94e4de2eed35056c5434c84e544bc429b84cfbf4f99b458464eb87f3394eaead","name":"bridge","type":"bridge"}},"scope":"local","time":1560835913,"timeNano":1560835913730994076}
{"status":"start","id":"94e4de2eed35056c5434c84e544bc429b84cfbf4f99b458464eb87f3394eaead","from":"nginx","Type":"container","Action":"start","Actor":{"ID":"94e4de2eed35056c5434c84e544bc429b84cfbf4f99b458464eb87f3394eaead","Attributes":{"image":"nginx","maintainer":"NGINX Docker Maintainers \u003cdocker-maint@nginx.com\u003e","name":"ecstatic_mendeleev"}},"scope":"local","time":1560835914,"timeNano":1560835914138532930}
{"status":"resize","id":"94e4de2eed35056c5434c84e544bc429b84cfbf4f99b458464eb87f3394eaead","from":"nginx","Type":"container","Action":"resize","Actor":{"ID":"94e4de2eed35056c5434c84e544bc429b84cfbf4f99b458464eb87f3394eaead","Attributes":{"height":"49","image":"nginx","maintainer":"NGINX Docker Maintainers \u003cdocker-maint@nginx.com\u003e","name":"ecstatic_mendeleev","width":"136"}},"scope":"local","time":1560835914,"timeNano":1560835914140380103}

还可以通过将bind mounting方式使用docker.socket

# docker run -it -v /var/run/docker.sock:/var/run/docker.sock busybox sh
apk update
apk add curl
/ # curl --unix-socket /var/run/docker.sock http://localhost/events
# docker run -it --rm nginx sh
/ # curl --unix-socket /var/run/docker.sock http://localhost/events
{"status":"create","id":"af36698121cb5b0734d3aa746c3f9519f54bb9bb63ae23a16bf3188a9fd7c724","from":"nginx","Type":"container","Action":"create","Actor":{"ID":"af36698121cb5b0734d3aa746c3f9519f54bb9bb63ae23a16bf3188a9fd7c724","Attributes":{"image":"nginx","maintainer":"NGINX Docker Maintainers \u003cdocker-maint@nginx.com\u003e","name":"gracious_hopper"}},"scope":"local","time":1560836531,"timeNano":1560836531969907371}
{"status":"attach","id":"af36698121cb5b0734d3aa746c3f9519f54bb9bb63ae23a16bf3188a9fd7c724","from":"nginx","Type":"container","Action":"attach","Actor":{"ID":"af36698121cb5b0734d3aa746c3f9519f54bb9bb63ae23a16bf3188a9fd7c724","Attributes":{"image":"nginx","maintainer":"NGINX Docker Maintainers \u003cdocker-maint@nginx.com\u003e","name":"gracious_hopper"}},"scope":"local","time":1560836531,"timeNano":1560836531971179743}
{"Type":"network","Action":"connect","Actor":{"ID":"b049de6ba0d38d1af9d93487b60ed861a08e65ecf3a644b0e6745fa903eb3d06","Attributes":{"container":"af36698121cb5b0734d3aa746c3f9519f54bb9bb63ae23a16bf3188a9fd7c724","name":"bridge","type":"bridge"}},"scope":"local","time":1560836532,"timeNano":1560836532003253979}
{"status":"start","id":"af36698121cb5b0734d3aa746c3f9519f54bb9bb63ae23a16bf3188a9fd7c724","from":"nginx","Type":"container","Action":"start","Actor":{"ID":"af36698121cb5b0734d3aa746c3f9519f54bb9bb63ae23a16bf3188a9fd7c724","Attributes":{"image":"nginx","maintainer":"NGINX Docker Maintainers \u003cdocker-maint@nginx.com\u003e","name":"gracious_hopper"}},"scope":"local","time":1560836532,"timeNano":1560836532459395027}
{"status":"resize","id":"af36698121cb5b0734d3aa746c3f9519f54bb9bb63ae23a16bf3188a9fd7c724","from":"nginx","Type":"container","Action":"resize","Actor":{"ID":"af36698121cb5b0734d3aa746c3f9519f54bb9bb63ae23a16bf3188a9fd7c724","Attributes":{"height":"47","image":"nginx","maintainer":"NGINX Docker Maintainers \u003cdocker-maint@nginx.com\u003e","name":"gracious_hopper","width":"88"}},"scope":"local","time":1560836532,"timeNano":1560836532461732028}

通过bind mounting的方式可以在容器中控制docker daemon.

分析docker

以docker 18.09.6 为例,分析一下docker,在docker 安装后,有docker``dockerd``docker-init``docker-proxy``runc这个几个二进制文件。
docker是docker client,dockerd是docker daemon,docker daemon调用runc最终创建container。
以docker 18.09.6为例,在x86_64 redhat 7.5环境中,通过rpm包安装docker,需要安装以下几个包
docker-ce-18.09.6-3.el7.x86_64.rpm``docker-ce-cli-18.09.6-3.el7.x86_64.rpm``containerd.io-1.2.5-3.1.el7.x86_64.rpm
我们来看看这些包中都包含了哪些内容
docker-ce 包含了主要的docker 包

# rpm -qpl docker-ce-18.09.6-3.el7.x86_64.rpm
warning: docker-ce-18.09.6-3.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
/usr/bin/docker-init
/usr/bin/docker-proxy
/usr/bin/dockerd-ce
/usr/lib/systemd/system/docker.service
/usr/lib/systemd/system/docker.socket
/var/lib/docker-engine/distribution_based_engine-ce.json

docker-ce-cli包含了docker client和一些帮助文档

# rpm -qpl docker-ce-cli-18.09.6-3.el7.x86_64.rpm
warning: docker-ce-cli-18.09.6-3.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
/usr/bin/docker
/usr/share/bash-completion/completions/docker
/usr/share/doc/docker-ce-cli-18.09.6
/usr/share/doc/docker-ce-cli-18.09.6/LICENSE
/usr/share/doc/docker-ce-cli-18.09.6/MAINTAINERS
/usr/share/doc/docker-ce-cli-18.09.6/NOTICE
/usr/share/doc/docker-ce-cli-18.09.6/README.md
/usr/share/fish/vendor_completions.d/docker.fish
/usr/share/man/man1/docker-attach.1.gz
......

containerd中包含了containerd containerd-shim ctr runc

# rpm -qpl containerd.io-1.2.5-3.1.el7.x86_64.rpm
warning: containerd.io-1.2.5-3.1.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
/etc/containerd
/etc/containerd/config.toml
/usr/bin/containerd
/usr/bin/containerd-shim
/usr/bin/ctr
/usr/lib/systemd/system/containerd.service
/usr/sbin/runc
/usr/share/doc/containerd.io-1.2.5
/usr/share/doc/containerd.io-1.2.5/README.md
/usr/share/licenses/containerd.io-1.2.5
/usr/share/licenses/containerd.io-1.2.5/LICENSE
/usr/share/man/man1/containerd-config.1
/usr/share/man/man1/containerd.1
/usr/share/man/man1/ctr.1
/usr/share/man/man5/containerd-config.toml.5
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 205,132评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,802评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,566评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,858评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,867评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,695评论 1 282
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,064评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,705评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,915评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,677评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,796评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,432评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,041评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,992评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,223评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,185评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,535评论 2 343