0%

Docker 初次使用

本部分内容基于CentOS 7 进行整理, 仅为记录;

运维视角的使用

安装docker

1
yum install docker

出现Complemte! 表示则说明已安装成功;

查看Docker 版本

1
docker version

发现docker daemon 并没有启动而docker client已启动, 那么需要启动docker daemon

1
2
3
4
5
Client:
Version: 1.13.1
API version: 1.26
Package version:
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

启动docker daemon 并置为系统启动

1
2
systemctl start docker.service
systemctl enable docker.service

再次查看Docker 版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@VM-0-13-centos ~]# docker version
Client:
Version: 1.13.1
API version: 1.26
Package version: docker-1.13.1-209.git7d71120.el7.centos.x86_64
Go version: go1.10.3
Git commit: 7d71120/1.13.1
Built: Wed Mar 2 15:25:43 2022
OS/Arch: linux/amd64

Server:
Version: 1.13.1
API version: 1.26 (minimum version 1.12)
Package version: docker-1.13.1-209.git7d71120.el7.centos.x86_64
Go version: go1.10.3
Git commit: 7d71120/1.13.1
Built: Wed Mar 2 15:25:43 2022
OS/Arch: linux/amd64
Experimental: false

发现docker daemon , docker client 均已启动;

拉取CentOS 镜像

1
2
3
docker pull centos
或者
docker image pull centos

查看镜像

1
2
3
4
5
6
7
8
9
[root@VM-0-13-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/ubuntu latest 27941809078c 4 weeks ago 77.8 MB
docker.io/centos latest 5d0da3dc9764 9 months ago 231 MB

[root@VM-0-13-centos ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/ubuntu latest 27941809078c 4 weeks ago 77.8 MB
docker.io/centos latest 5d0da3dc9764 9 months ago 231 MB

两个命令均可以查看docker 镜像, 发现目前已经有两个镜像了, 均是刚拉取的镜像, 这里仅以centos作为示例;

启动镜像获得容器

1
2
3
4
[root@VM-0-13-centos ~]# docker run -it centos:latest /bin/bash
[root@a99489e5262c /]#
----------------
使用 docker container run -it centos:latest /bin/bash 是一样的;

发现已经进入了容器的控制台;

查看下容器内进程

1
2
3
4
[root@a99489e5262c /]# ps -elf
F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD
4 S root 1 0 0 80 0 - 3009 do_wai 09:58 ? 00:00:00 /bin/bash
4 R root 14 1 0 80 0 - 11163 - 10:02 ? 00:00:00 ps -elf

发现目前有两个进程在运行, 一个是启动容器/bin/bash的进程, 一个是用来查看进程的进程 ps -elf

回退到Host 主机

Ctrl+PQ, 这样返回并不会关闭进程;

1
[root@a99489e5262c /]# [root@VM-0-13-centos ~]#

查看容器列表

1
2
3
[root@VM-0-13-centos ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a99489e5262c centos:latest "/bin/bash" 7 minutes ago Up 7 minutes gracious_colden

发现刚才启动的容器,确实在列表中;

查看Host的进程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
l
[root@VM-0-13-centos ~]# ps -elf
F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD
4 S root 1 0 0 80 0 - 31438 ep_pol 17:16 ? 00:00:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
1 S root 2 0 0 80 0 - 0 kthrea 17:16 ? 00:00:00 [kthreadd]
1 S root 4 2 0 60 -20 - 0 worker 17:16 ? 00:00:00 [kworker/0:0H]
1 S root 6 2 0 80 0 - 0 smpboo 17:16 ? 00:00:00 [ksoftirqd/0]
1 S root 7 2 0 -40 - - 0 smpboo 17:16 ? 00:00:00 [migration/0]
1 S root 8 2 0 80 0 - 0 rcu_gp 17:16 ? 00:00:00 [rcu_bh]
1 S root 9 2 0 80 0 - 0 rcu_gp 17:16 ? 00:00:00 [rcu_sched]
1 S root 10 2 0 60 -20 - 0 rescue 17:16 ? 00:00:00 [lru-add-drain]
5 S root 11 2 0 -40 - - 0 smpboo 17:16 ? 00:00:00 [watchdog/0]
5 S root 12 2 0 -40 - - 0 smpboo 17:16 ? 00:00:00 [watchdog/1]
1 S root 13 2 0 -40 - - 0 smpboo 17:16 ? 00:00:00 [migration/1]
1 S root 14 2 0 80 0 - 0 smpboo 17:16 ? 00:00:00 [ksoftirqd/1]
1 S root 16 2 0 60 -20 - 0 worker 17:16 ? 00:00:00 [kworker/1:0H]
5 S root 18 2 0 80 0 - 0 devtmp 17:16 ? 00:00:00 [kdevtmpfs]
1 S root 19 2 0 60 -20 - 0 rescue 17:16 ? 00:00:00 [netns]
1 S root 20 2 0 80 0 - 0 watchd 17:16 ? 00:00:00 [khungtaskd]
1 S root 21 2 0 60 -20 - 0 rescue 17:16 ? 00:00:00 [writeback]
1 S root 22 2 0 60 -20 - 0 rescue 17:16 ? 00:00:00 [kintegrityd]
1 S root 23 2 0 60 -20 - 0 rescue 17:16 ? 00:00:00 [bioset]
1 S root 24 2 0 60 -20 - 0 rescue 17:16 ? 00:00:00 [bioset]
1 S root 25 2 0 60 -20 - 0 rescue 17:16 ? 00:00:00 [bioset]
1 S root 26 2 0 60 -20 - 0 rescue 17:16 ? 00:00:00 [kblockd]
1 S root 27 2 0 60 -20 - 0 rescue 17:16 ? 00:00:00 [md]
1 S root 28 2 0 60 -20 - 0 rescue 17:16 ? 00:00:00 [edac-poller]
1 S root 29 2 0 60 -20 - 0 rescue 17:16 ? 00:00:00 [watchdogd]
1 S root 36 2 0 80 0 - 0 kswapd 17:16 ? 00:00:00 [kswapd0]
1 S root 37 2 0 85 5 - 0 ksm_sc 17:16 ? 00:00:00 [ksmd]
1 S root 38 2 0 99 19 - 0 khugep 17:16 ? 00:00:00 [khugepaged]
1 S root 39 2 0 60 -20 - 0 rescue 17:16 ? 00:00:00 [crypto]
1 S root 47 2 0 60 -20 - 0 rescue 17:16 ? 00:00:00 [kthrotld]
1 S root 48 2 0 80 0 - 0 worker 17:16 ? 00:00:00 [kworker/u4:1]
1 S root 49 2 0 60 -20 - 0 rescue 17:16 ? 00:00:00 [kmpath_rdacd]
1 S root 50 2 0 60 -20 - 0 rescue 17:16 ? 00:00:00 [kaluad]
1 S root 51 2 0 60 -20 - 0 rescue 17:16 ? 00:00:00 [kpsmoused]
1 S root 52 2 0 60 -20 - 0 rescue 17:16 ? 00:00:00 [ipv6_addrconf]
1 S root 65 2 0 60 -20 - 0 rescue 17:16 ? 00:00:00 [deferwq]
1 S root 108 2 0 80 0 - 0 kaudit 17:16 ? 00:00:00 [kauditd]
1 S root 238 2 0 60 -20 - 0 rescue 17:16 ? 00:00:00 [ata_sff]
1 S root 241 2 0 80 0 - 0 scsi_e 17:16 ? 00:00:00 [scsi_eh_0]
1 S root 242 2 0 60 -20 - 0 rescue 17:16 ? 00:00:00 [scsi_tmf_0]
1 S root 243 2 0 80 0 - 0 scsi_e 17:16 ? 00:00:00 [scsi_eh_1]
1 S root 244 2 0 60 -20 - 0 rescue 17:16 ? 00:00:00 [scsi_tmf_1]
1 S root 248 2 0 60 -20 - 0 worker 17:16 ? 00:00:00 [kworker/0:1H]
1 S root 252 2 0 60 -20 - 0 rescue 17:16 ? 00:00:00 [ttm_swap]
1 S root 272 2 0 60 -20 - 0 worker 17:16 ? 00:00:00 [kworker/1:1H]
1 S root 273 2 0 80 0 - 0 kjourn 17:16 ? 00:00:00 [jbd2/vda1-8]
1 S root 274 2 0 60 -20 - 0 rescue 17:16 ? 00:00:00 [ext4-rsv-conver]
4 S root 363 1 0 80 0 - 26239 ep_pol 17:16 ? 00:00:00 /usr/lib/systemd/systemd-journald
4 S root 385 1 0 80 0 - 31210 poll_s 17:16 ? 00:00:00 /usr/sbin/lvmetad -f
4 S root 396 1 0 80 0 - 11424 ep_pol 17:16 ? 00:00:00 /usr/lib/systemd/systemd-udevd
1 S root 513 2 0 60 -20 - 0 rescue 17:16 ? 00:00:00 [nfit]
1 S root 633 2 0 60 -20 - 0 rescue 17:16 ? 00:00:00 [rpciod]
1 S root 634 2 0 60 -20 - 0 rescue 17:16 ? 00:00:00 [xprtiod]
5 S root 636 1 0 76 -4 - 13883 ep_pol 17:16 ? 00:00:00 /sbin/auditd
4 S root 638 636 0 72 -8 - 21139 futex_ 17:16 ? 00:00:00 /sbin/audispd
4 S root 640 638 0 76 -4 - 13911 unix_s 17:16 ? 00:00:00 /usr/sbin/sedispatch
4 S polkitd 658 1 0 80 0 - 153610 poll_s 17:16 ? 00:00:00 /usr/lib/polkit-1/polkitd --no-debug
4 S root 659 1 0 80 0 - 6654 ep_pol 17:16 ? 00:00:00 /usr/lib/systemd/systemd-logind
5 S rpc 668 1 0 80 0 - 17320 poll_s 17:16 ? 00:00:00 /sbin/rpcbind -w
4 S dbus 672 1 0 80 0 - 15151 ep_pol 17:16 ? 00:00:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --syst
1 S root 674 1 0 80 0 - 1097 poll_s 17:16 ? 00:00:00 /usr/sbin/acpid
5 S ntp 679 1 0 80 0 - 12345 poll_s 17:16 ? 00:00:00 /usr/sbin/ntpd -u ntp:ntp -g
4 S libstor+ 685 1 0 80 0 - 2145 poll_s 17:16 ? 00:00:00 /usr/bin/lsmd -d
4 S avahi 686 1 0 80 0 - 15573 poll_s 17:16 ? 00:00:00 avahi-daemon: running [VM-0-13-centos.local]
1 S avahi 690 686 0 80 0 - 15541 unix_s 17:16 ? 00:00:00 avahi-daemon: chroot helper
1 S root 696 1 0 80 0 - 28853 do_wai 17:16 ? 00:00:00 /bin/bash /usr/sbin/ksmtuned
1 S root 697 1 0 80 0 - 50358 ep_pol 17:16 ? 00:00:00 /usr/sbin/gssproxy -D
1 S root 982 1 0 80 0 - 25726 poll_s 17:16 ? 00:00:00 /sbin/dhclient -H VM-0-13-centos -q -lf /var/lib/dhclient/dhclient--eth0.lea
4 S root 1055 1 0 80 0 - 146513 poll_s 17:16 ? 00:00:00 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
4 S root 1076 1 0 80 0 - 185430 poll_s 17:16 ? 00:00:00 /usr/sbin/rsyslogd -n
4 S root 1090 1 0 80 0 - 251084 poll_s 17:16 ? 00:00:00 /usr/sbin/libvirtd
4 S root 1099 1 0 80 0 - 6477 hrtime 17:16 ? 00:00:00 /usr/sbin/atd -f
4 S root 1111 1 0 80 0 - 31598 hrtime 17:16 ? 00:00:00 /usr/sbin/crond -n
4 S root 1278 1 0 80 0 - 27552 n_tty_ 17:16 ttyS0 00:00:00 /sbin/agetty --keep-baud 115200,38400,9600 ttyS0 vt220
4 S root 1279 1 0 80 0 - 27552 n_tty_ 17:16 tty1 00:00:00 /sbin/agetty --noclear tty1 linux
1 S root 1314 1 0 80 0 - 7268 ep_pol 17:16 ? 00:00:00 /usr/local/qcloud/tat_agent/tat_agent
4 S root 1580 1 0 80 0 - 28231 poll_s 17:16 ? 00:00:00 /usr/sbin/sshd -D
5 S nobody 1592 1 0 80 0 - 13475 poll_s 17:16 ? 00:00:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasef
1 S root 1593 1592 0 80 0 - 13468 pipe_w 17:16 ? 00:00:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasef
4 S root 1598 1580 0 80 0 - 39852 poll_s 17:16 ? 00:00:00 sshd: root@pts/0
0 S root 1608 1 0 80 0 - 247139 do_wai 17:16 ? 00:00:00 /usr/local/qcloud/YunJing/YDLive/YDLive
1 S root 1628 1 0 80 0 - 24351 hrtime 17:17 ? 00:00:00 /usr/local/qcloud/stargate/bin/sgagent -d
1 S root 1644 1 0 80 0 - 38850 poll_s 17:17 ? 00:00:00 barad_agent
1 S root 1650 1644 0 80 0 - 41227 poll_s 17:17 ? 00:00:01 barad_agent
1 S root 1651 1644 0 80 0 - 134822 poll_s 17:17 ? 00:00:08 barad_agent
4 S root 1661 1608 0 80 0 - 251552 ep_pol 17:17 ? 00:00:12 /usr/local/qcloud/YunJing/YDEyes/YDService
0 S root 1801 1661 0 80 0 - 254192 futex_ 17:18 ? 00:00:00 /bin/sh -c sleep 100
4 S root 1846 1580 0 80 0 - 39775 poll_s 17:18 ? 00:00:00 sshd: root@notty
4 S root 1853 1846 0 80 0 - 18063 poll_s 17:18 ? 00:00:00 /usr/libexec/openssh/sftp-server
4 S root 1867 1598 0 80 0 - 29333 n_tty_ 17:18 pts/0 00:00:00 -bash
4 S root 1939 1 0 80 0 - 102654 poll_s 17:18 ? 00:00:00 /usr/libexec/packagekitd
4 S root 2242 1580 0 80 0 - 38684 poll_s 17:20 ? 00:00:00 sshd: root@notty
4 S root 2244 2242 0 80 0 - 28322 do_wai 17:20 ? 00:00:00 bash
0 S root 2300 2244 0 80 0 - 28321 do_wai 17:20 ? 00:00:00 sh /root/.vscode-server/bin/4af164ea3a06f701fe3e89a2bcbb421d2026b68f/bin/cod
0 S root 2312 2300 0 80 0 - 231839 ep_pol 17:20 ? 00:00:00 /root/.vscode-server/bin/4af164ea3a06f701fe3e89a2bcbb421d2026b68f/node /root
0 S root 2367 2312 0 80 0 - 157627 ep_pol 17:20 ? 00:00:00 /root/.vscode-server/bin/4af164ea3a06f701fe3e89a2bcbb421d2026b68f/node /root
4 S root 2885 1 0 80 0 - 204900 do_wai 17:22 ? 00:00:09 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docke
4 S root 2893 2885 0 80 0 - 80069 futex_ 17:22 ? 00:00:01 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/d
1 S root 5538 2 0 80 0 - 0 worker 17:37 ? 00:00:00 [kworker/1:0]
1 S root 5564 2 0 80 0 - 0 worker 17:37 ? 00:00:00 [kworker/u4:0]
4 S root 6971 1580 0 80 0 - 39850 poll_s 17:47 ? 00:00:00 sshd: root@pts/1
4 S root 6973 1580 0 80 0 - 39775 poll_s 17:47 ? 00:00:00 sshd: root@notty
4 S root 6975 6971 0 80 0 - 29151 n_tty_ 17:47 pts/1 00:00:00 -bash
4 S root 7015 6973 0 80 0 - 18063 poll_s 17:47 ? 00:00:00 /usr/libexec/openssh/sftp-server
1 S root 7421 2 0 80 0 - 0 worker 17:50 ? 00:00:00 [kworker/0:2]
4 R root 7859 1580 0 80 0 - 39850 - 17:53 ? 00:00:00 sshd: root@pts/2
4 S root 7861 1580 0 80 0 - 39775 poll_s 17:53 ? 00:00:00 sshd: root@notty
4 S root 7863 7859 0 80 0 - 29151 do_wai 17:53 pts/2 00:00:00 -bash
4 S root 7905 7861 0 80 0 - 18063 poll_s 17:53 ? 00:00:00 /usr/libexec/openssh/sftp-server
1 S root 8224 2 0 80 0 - 0 worker 17:55 ? 00:00:00 [kworker/0:0]
0 S root 8674 2893 0 80 0 - 68577 futex_ 17:58 ? 00:00:00 /usr/bin/docker-containerd-shim-current a99489e5262c774cc07629fdd3fffc2a9732
4 S root 8691 8674 0 80 0 - 3009 poll_s 17:58 pts/3 00:00:00 /bin/bash
1 S root 9005 2 0 80 0 - 0 worker 18:00 ? 00:00:00 [kworker/1:2]
1 S root 9742 2 0 80 0 - 0 worker 18:05 ? 00:00:00 [kworker/0:1]
0 S root 9785 2244 0 80 0 - 27014 hrtime 18:05 ? 00:00:00 sleep 180
0 S root 9951 696 0 80 0 - 27014 hrtime 18:06 ? 00:00:00 sleep 60
0 R root 10085 7863 0 80 0 - 38869 - 18:07 pts/2 00:00:00 ps -elf

发现Host 的进程比容器内多得多;

连接到运行中的容器中

1
2
[root@VM-0-13-centos ~]# docker container exec -it gracious_colden bash
[root@a99489e5262c /]#

回退到Host

这里依旧没有直接退出, 用的Ctrl+PQ

再次查看容器状态

1
2
3
4
[root@a99489e5262c /]# [root@VM-0-13-centos ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a99489e5262c centos:latest "/bin/bash" 14 minutes ago Up 14 minutes gracious_colden

关闭容器

1
2
3
4
5
6
[root@VM-0-13-centos ~]# docker container stop gracious_colden
gracious_colden
[root@VM-0-13-centos ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

[root@VM-0-13-centos ~]#

通过docker container stop docker container rm, 命令是一样效果; 再次查看容器状态,发现已经没有了;

开发视角的使用

本部分内容,会根据应用代码中的Docerfile将应用容器化,并使用容器的方式运行;代码来源于Docker Deep Dive 的配套资源;