2.Docker 的第一次親密接觸

概況

掌握 Docker 在Centos的安裝:

  • 操作系統(tǒng):Win 10 家庭中文版
  • 虛擬機(jī)軟件:Oracle VirtualBox 5.0.26 及以上
  • 虛擬機(jī)操作系統(tǒng): Centos 7
  • Docker 最新版本的安裝

安裝VirtualBox 和 Centos7

參考網(wǎng)頁(yè):Win10 安裝 VirtualBox
參考網(wǎng)頁(yè):VirtualBox 安裝 Centos7 虛擬機(jī)

安裝 Docker

參考頁(yè)面 - Centos 安裝 Docker:Installation on Centos
參考頁(yè)面 - 二進(jìn)制包安裝方式:Installation from binaries

前置條件:

  • 64位操作系統(tǒng)
  • Linux 內(nèi)核是3.10或者更高(安裝Centos7的原因
  • iptables version 1.4 or later
  • Git version 1.7 or later
  • procps (or similar provider of a “ps” executable)
  • XZ Utils 4.9 or later
  • a properly mounted cgroupfs hierarchy

上述內(nèi)容只是讓我們清楚Docker的相關(guān)依賴(lài)固灵,在具體安裝的時(shí)候永品,我們可以通過(guò) yum 安裝的方式,自動(dòng)解決依賴(lài)包的下載和安裝。

安裝步驟

  1. 更新當(dāng)前軟件包到最新
[root@localhost ~]# yum update
  1. 添加 Docker 的 yum 倉(cāng)庫(kù)
[root@localhost ~]# tee /etc/yum.repos.d/docker.repo <<-'EOF'
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/7/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
EOF
[root@localhost ~]# du -sh /etc/yum.repos.d/docker.repo
4.0K    /etc/yum.repos.d/docker.repo
  1. 安裝 Docker 軟件包
[root@localhost ~]# yum install -y docker-engine
  1. 安裝成功之后校驗(yàn)檢查
  • 查看 docker 軟件包是否已安裝包(yum list installed | grep docker
  • 查看 docker 軟件包有哪些執(zhí)行文件(ls /usr/bin/*docker*
  • 查看 docker 系統(tǒng)服務(wù)配置文件是否存在(ls /usr/lib/systemd/system/docker.service -l
  • 查看 docker 版本(docker version
  • 查看 docker 默認(rèn)配置文件目錄是否存在(ls /etc/docker/ -l
  • 查看 docker 默認(rèn)運(yùn)行時(shí)目錄是否存在(ls /var/lib/docker/ -l
  • 查看系統(tǒng)的邏輯卷組有哪些(vgs -a
  • 查看系統(tǒng)的 DeviceMapper 機(jī)制映射的邏輯設(shè)備有哪些(ls /dev/centos/ -l:docker默認(rèn)方式會(huì)創(chuàng)建自己的邏輯設(shè)備)
  • 查看網(wǎng)絡(luò)接口配置信息(ifconfig:docker用到網(wǎng)絡(luò)接口)
  • 查看 iptables 規(guī)則配置信息(iptables --list:docker用到iptables)

下面是執(zhí)行結(jié)果

[root@localhost ~]# yum list installed | grep docker
docker-engine.x86_64                 1.12.2-1.el7.centos             @dockerrepo
docker-engine-selinux.noarch         1.12.2-1.el7.centos             @dockerrepo
[root@localhost ~]# docker version
Client:
 Version:      1.12.2
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   bb80604
 Built:
 OS/Arch:      linux/amd64
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
[root@localhost ~]# ls /usr/lib/systemd/system/docker.service -l
-rw-r--r--. 1 root root 974 9月  28 01:22 /usr/lib/systemd/system/docker.service
[root@localhost ~]# ls /usr/bin/*docker*
/usr/bin/docker                  /usr/bin/dockerd
/usr/bin/docker-containerd       /usr/bin/docker-proxy
/usr/bin/docker-containerd-ctr   /usr/bin/docker-runc
/usr/bin/docker-containerd-shim
[root@localhost ~]# ls /etc/docker/ -l
ls: 無(wú)法訪問(wèn)/etc/docker/: 沒(méi)有那個(gè)文件或目錄
[root@localhost ~]# ls /var/lib/docker/ -l
ls: 無(wú)法訪問(wèn)/var/lib/docker/: 沒(méi)有那個(gè)文件或目錄
[root@localhost ~]# vgs -a
  VG     #PV #LV #SN Attr   VSize  VFree
  centos   1   2   0 wz--n- 19.51g 40.00m
[root@localhost ~]# ls /dev/centos/ -l
總用量 0
lrwxrwxrwx. 1 root root 7 10月 16 23:18 root -> ../dm-0
lrwxrwxrwx. 1 root root 7 10月 16 23:18 swap -> ../dm-1
[root@localhost ~]# ls /dev/mapper/ -l
總用量 0
lrwxrwxrwx. 1 root root       7 10月 16 23:18 centos-root -> ../dm-0
lrwxrwxrwx. 1 root root       7 10月 16 23:18 centos-swap -> ../dm-1
crw-------. 1 root root 10, 236 10月 16 23:18 control
[root@localhost ~]# ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.104  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::a00:27ff:fe2d:62c7  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:2d:62:c7  txqueuelen 1000  (Ethernet)
        RX packets 1084  bytes 119437 (116.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 778  bytes 113093 (110.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@localhost ~]# iptables --list
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
  1. 啟動(dòng)服務(wù)
  • 設(shè)置開(kāi)機(jī)自動(dòng)啟動(dòng) docker 服務(wù)(systemctl enable docker
  • 手工啟動(dòng) docker 服務(wù)(systemctl start docker
  • 查看 docker 版本(docker version
  • 顯示 docker 詳細(xì)信息(docker info
[root@localhost ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@localhost ~]# systemctl start docker
[root@localhost ~]# ps -ef| grep docker
root      2160     1  3 00:01 ?        00:00:00 /usr/bin/dockerd
root      2163  2160  0 00:01 ?        00:00:00 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --shim docker-containerd-shim --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --runtime docker-runc
root      2278  2003  0 00:01 pts/0    00:00:00 grep --color=auto docker
[root@localhost ~]# docker version
Client:
Version:      1.12.2
API version:  1.24
Go version:   go1.6.3
Git commit:   bb80604
Built:
OS/Arch:      linux/amd64
Server:
Version:      1.12.2
API version:  1.24
Go version:   go1.6.3
Git commit:   bb80604
Built:
OS/Arch:      linux/amd64
[root@localhost ~]# docker info
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 1.12.2
Storage Driver: devicemapper
Pool Name: docker-253:0-67182400-pool
Pool Blocksize: 65.54 kB
Base Device Size: 10.74 GB
Backing Filesystem: xfs
Data file: /dev/loop0
Metadata file: /dev/loop1
Data Space Used: 11.8 MB
Data Space Total: 107.4 GB
Data Space Available: 17.52 GB
Metadata Space Used: 581.6 kB
Metadata Space Total: 2.147 GB
Metadata Space Available: 2.147 GB
Thin Pool Minimum Free Space: 10.74 GB
Udev Sync Supported: true
Deferred Removal Enabled: false
Deferred Deletion Enabled: false
Deferred Deleted Device Count: 0
Data loop file: /var/lib/docker/devicemapper/devicemapper/data
WARNING: Usage of loopback devices is strongly discouraged for production use. Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.
Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
Library Version: 1.02.107-RHEL7 (2016-06-09)
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: null host bridge overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Security Options: seccomp
Kernel Version: 3.10.0-327.36.2.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 1.522 GiB
Name: localhost.localdomain
ID: VFSO:QWR6:ICKT:QGXY:UQXT:5VKN:L6E3:7JBE:OBVO:SQFK:PU2V:IBMS
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Insecure Registries:
127.0.0.0/8
  1. 看看Docker 啟動(dòng)之后有什么變化
  • 查看 docker 版本(docker version
  • 查看 docker 默認(rèn)配置文件目錄是否存在(ls /etc/docker/ -l
  • 查看 docker 默認(rèn)運(yùn)行時(shí)目錄是否存在(ls /var/lib/docker/ -l
  • 查看系統(tǒng)的 DeviceMapper 機(jī)制映射的邏輯設(shè)備有哪些(ls /dev/centos/ -l
  • 查看 docker 啟動(dòng)后主程序的進(jìn)程文件[.pid文件]和本地套接字[.sock文件](ls /run/docker.* -l
  • 查看 docker 啟動(dòng)之后子進(jìn)程文件和對(duì)應(yīng)的本地套接字(ls /run/docker/ -l
  • 查看網(wǎng)絡(luò)接口配置信息(ifconfig
  • 查看 iptables 規(guī)則配置信息(iptables --list

命令運(yùn)行結(jié)果如下:

[root@localhost ~]# ls /etc/docker/ -l
總用量 4
-rw-------. 1 root root 244 10月 17 00:01 key.json
[root@localhost ~]# ls /var/lib/docker/ -l
總用量 0
drwx------. 2 root root  6 10月 17 00:01 containers
drwx------. 4 root root 40 10月 17 00:01 devicemapper
drwx------. 3 root root 25 10月 17 00:01 image
drwxr-x---. 3 root root 18 10月 17 00:01 network
drwx------. 2 root root  6 10月 17 00:01 swarm
drwx------. 2 root root  6 10月 17 00:01 tmp
drwx------. 2 root root  6 10月 17 00:01 trust
drwx------. 2 root root 24 10月 17 00:01 volumes
[root@localhost ~]# ls /dev/mapper/ -l
總用量 0
lrwxrwxrwx. 1 root root       7 10月 17 11:32 centos-root -> ../dm-0
lrwxrwxrwx. 1 root root       7 10月 17 11:32 centos-swap -> ../dm-1
crw-------. 1 root root 10, 236 10月 17 11:32 control
lrwxrwxrwx. 1 root root       7 10月 17 11:43 docker-253:0-67182400-pool -> ../dm-2
[root@localhost ~]# ls /run/docker.* -l
-rw-r--r--. 1 root root   4 10月 17 11:43 /run/docker.pid
srw-rw----. 1 root docker 0 10月 17 11:43 /run/docker.sock
[root@localhost ~]# ls /run/docker/ -l
總用量 0
drwx------. 3 root root 100 10月 17 11:43 libcontainerd
drw-------. 2 root root  60 10月 17 11:43 libnetwork
[root@localhost ~]# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 0.0.0.0
        ether 02:42:e4:89:85:b8  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.161  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::a00:27ff:fe2d:62c7  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:2d:62:c7  txqueuelen 1000  (Ethernet)
        RX packets 1621  bytes 111194 (108.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 118  bytes 17109 (16.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@localhost ~]# iptables --list
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
DOCKER-ISOLATION  all  --  anywhere             anywhere
DOCKER     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
Chain DOCKER (1 references)
target     prot opt source               destination
Chain DOCKER-ISOLATION (1 references)
target     prot opt source               destination
RETURN     all  --  anywhere             anywhere
  1. 運(yùn)行某個(gè)鏡像
[root@localhost ~]# docker run --rm hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c04b14da8d14: Pull complete
Digest: sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
    $ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker Hub account:
 https://hub.docker.com
For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市赡茸,隨后出現(xiàn)的幾起案子缎脾,更是在濱河造成了極大的恐慌,老刑警劉巖占卧,帶你破解...
    沈念sama閱讀 210,835評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件遗菠,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡华蜒,警方通過(guò)查閱死者的電腦和手機(jī)辙纬,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 89,900評(píng)論 2 383
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)叭喜,“玉大人贺拣,你說(shuō)我怎么就攤上這事∥嬖蹋” “怎么了譬涡?”我有些...
    開(kāi)封第一講書(shū)人閱讀 156,481評(píng)論 0 345
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)啥辨。 經(jīng)常有香客問(wèn)我涡匀,道長(zhǎng),這世上最難降的妖魔是什么溉知? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,303評(píng)論 1 282
  • 正文 為了忘掉前任陨瘩,我火速辦了婚禮腕够,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘舌劳。我一直安慰自己帚湘,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,375評(píng)論 5 384
  • 文/花漫 我一把揭開(kāi)白布蒿囤。 她就那樣靜靜地躺著客们,像睡著了一般。 火紅的嫁衣襯著肌膚如雪材诽。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 49,729評(píng)論 1 289
  • 那天恒傻,我揣著相機(jī)與錄音脸侥,去河邊找鬼。 笑死盈厘,一個(gè)胖子當(dāng)著我的面吹牛睁枕,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播沸手,決...
    沈念sama閱讀 38,877評(píng)論 3 404
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼外遇,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了契吉?” 一聲冷哼從身側(cè)響起跳仿,我...
    開(kāi)封第一講書(shū)人閱讀 37,633評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎捐晶,沒(méi)想到半個(gè)月后菲语,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,088評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡惑灵,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,443評(píng)論 2 326
  • 正文 我和宋清朗相戀三年山上,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片英支。...
    茶點(diǎn)故事閱讀 38,563評(píng)論 1 339
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡佩憾,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出干花,到底是詐尸還是另有隱情妄帘,我是刑警寧澤,帶...
    沈念sama閱讀 34,251評(píng)論 4 328
  • 正文 年R本政府宣布把敢,位于F島的核電站寄摆,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏修赞。R本人自食惡果不足惜婶恼,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,827評(píng)論 3 312
  • 文/蒙蒙 一桑阶、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧勾邦,春花似錦蚣录、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,712評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至蕉饼,卻和暖如春虐杯,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背昧港。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,943評(píng)論 1 264
  • 我被黑心中介騙來(lái)泰國(guó)打工擎椰, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人创肥。 一個(gè)月前我還...
    沈念sama閱讀 46,240評(píng)論 2 360
  • 正文 我出身青樓达舒,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親叹侄。 傳聞我的和親對(duì)象是個(gè)殘疾皇子巩搏,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,435評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容