盒子
盒子
Posts List
  1. docker的简单使用
    1. 学习地址:https://www.bilibili.com/video/BV1og4y1q7M4?
    2. 跟着视频学习的,一点点跟着视频打了一遍
  2. 帮助命令
  3. 镜像命令
    1. 查看所有本地主机上的镜像
    2. 搜索镜像
    3. 下载镜像
    4. 删除镜像
  4. 容器命令
    1. 启动镜像
    2. 列出所有的运行的容器
    3. 退出容器
    4. 删除容器
    5. 启动和停止容器的操作
    6. 常用其他命令
      1. 后台启动容器
      2. 查看日志
      3. 查看容器中进程信息
      4. 查看镜像的元数据
      5. 进入当前正在运行的容器
      6. 从容器内拷贝文件到主机上

docker的简单使用

docker的简单使用

学习地址:https://www.bilibili.com/video/BV1og4y1q7M4?

跟着视频学习的,一点点跟着视频打了一遍

帮助命令

1
2
3
docker version    # 显示docker的版本信息
docker info # 显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help # 帮助命令

帮助文档地址:https://docs.docker.com/engine/reference

镜像命令

查看所有本地主机上的镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
root@kongzhou-virtual-machine:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 11 months ago 13.3kB

# 解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的id
CREATED 镜像的创建时间
SIZE 镜像的大小

# 可选项
-a, --all # 列出所有镜像
-q, --quiet # 只显示镜像的id

搜索镜像

1
2
3
4
5
6
7
8
9
10
11
12
root@kongzhou-virtual-machine:~# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10269 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3794 [OK]

# 可选项,通过STAR过滤
docker search mysql --filter=STARs=3000

root@kongzhou-virtual-machine:~# docker search mysql --filter=STARs=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10269 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3794 [OK]

下载镜像

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
# 下载镜像 docker pull 镜像名[:tag] 指定版本下载,但下载版本必须docker中有

root@kongzhou-virtual-machine:~# docker pull mysql
Using default tag: latest # 默认下载最新版
latest: Pulling from library/mysql
6ec7b7d162b2: Pull complete # 分层下载,docker images的核心 联合文件系统
fedd960d3481: Pull complete
7ab947313861: Pull complete
64f92f19e638: Pull complete
3e80b17bff96: Pull complete
014e976799f9: Pull complete
59ae84fee1b3: Pull complete
ffe10de703ea: Pull complete
657af6d90c83: Pull complete
98bfb480322c: Pull complete
9f2c4202ac29: Pull complete
a369b92bfc99: Pull complete
Digest: sha256:365e891b22abd3336d65baefc475b4a9a1e29a01a7b6b5be04367fcc9f373bb7 # 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址

# 两者相同
docker pull mysql
docker.io/library/mysql:latest

# docker pull mysql:5.7 指定版本下载
root@kongzhou-virtual-machine:~# docker pull mysql:5.7
5.7: Pulling from library/mysql
6ec7b7d162b2: Already exists # 很明显显示已经存在,因为上面下载的mysql中已经下载过这些的文件,可以共用,非常方便
fedd960d3481: Already exists
7ab947313861: Already exists
64f92f19e638: Already exists
3e80b17bff96: Already exists
014e976799f9: Already exists
59ae84fee1b3: Already exists
7d1da2a18e2e: Pull complete
301a28b700b9: Pull complete
979b389fc71f: Pull complete
403f729b1bad: Pull complete
Digest: sha256:d4ca82cee68dce98aa72a1c48b5ef5ce9f1538265831132187871b78e768aed1
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

删除镜像

1
2
3
docker rmi -f 容器id                 # 删除指定容器
docker rmi -f 容器id 容器id 容器id # 删除多个容器
docker rmi -f $(docker images -aq) # 删除全部的容器

容器命令

说明:我们有了镜像才可以创建容器,linux,下载一个镜像测试学习

1
docker pull centos

新建容器并启动

启动镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
docker run [可选参数] image

# 参数说明
--name="Name" 容器名字 pwn1 pwn2 用来区分容器
-d 后台方式运行
-it 使用交互方式运行
-p 指定容器端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
容器端口
-P 随机指定端口

# 测试,启动并进入容器
root@kongzhou-virtual-machine:~# docker run -it centos /bin/bash
[root@0b0a0a20f059 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr

# 从容器这种返回主机
[root@0b0a0a20f059 /]# exit
exit
root@kongzhou-virtual-machine:~#

列出所有的运行的容器

1
2
3
4
docker ps 命令 
-a #列出当前正在运行的容器
-n=? #显示最近?个创建的容器
-q #只显示容器的编号

退出容器

1
2
exit          # 直接容器停止并退出
Ctrl + P + Q # 容器不停止退出

删除容器

1
2
3
docker rm 容器id                # 删除指定容器. 不能删除正在运行的容器,如果需要强制删除 rm -f
docker rm -f $(docker ps -aq) # 删除所有容器
docker ps -a q|xargs docker rm # 删除所有容器

启动和停止容器的操作

1
2
3
4
docker start 容器id    # 启动容器
docker restart 容器id # 重启容器
docker stop 容器id # 停止当前正在运行的容器
docker kill 容器id # 强制停止当前容器

常用其他命令

后台启动容器

image-20210118160535997

1
2
3
4
5
# 命令 docker run -d 镜像名

# 问题docker ps,发现centos停止了
# 常见的坑:docker使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
# nginx,容器启动后,发现自己没有提供服务,就是没有程序了

查看日志

1
2
3
4
5
6
7
8
9
10
11
12
13
docker logs -f -t --tail 容器id  # 没有日志
# 自己编写一段 shell脚本
root@kongzhou-virtual-machine:~# docker run -d centos /bin/sh -C "while true;do echo kongzhou;sleep 1;done"

# root@kongzhou-virtual-machine:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3cf6fc9bc61c centos "/bin/sh -c 'while t…" 15 seconds ago Up 13 seconds agitated_visvesvaraya

# 显示日志
-tf # 显示日志
--tail number # 要显示日志条数

root@kongzhou-virtual-machine:~# docker logs -f -t --tail 10 3cf6fc9bc61c

查看容器中进程信息

1
2
3
4
5
6
# docker top 容器id
root@kongzhou-virtual-machine:~# docker top 3cf6fc9bc61c
UID PID PPID C STIME TTY TIME CMD
root 53105 53082 0 16:14 ? 00:00:00 /bin/sh -c while true;do echo kongzhou;sleep 1;done
root 53844 53105 0 16:25 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1

查看镜像的元数据

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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# 命令 docker inspect 容器id
# 测试
root@kongzhou-virtual-machine:~# docker inspect 3cf6fc9bc61c
[
{
"Id": "3cf6fc9bc61c00647b95b88f6a8af9e35eabd06c7fdc46278ee38da237050410",
"Created": "2021-01-18T08:14:46.818963923Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo kongzhou;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 53105,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-01-18T08:14:47.428211885Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/3cf6fc9bc61c00647b95b88f6a8af9e35eabd06c7fdc46278ee38da237050410/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/3cf6fc9bc61c00647b95b88f6a8af9e35eabd06c7fdc46278ee38da237050410/hostname",
"HostsPath": "/var/lib/docker/containers/3cf6fc9bc61c00647b95b88f6a8af9e35eabd06c7fdc46278ee38da237050410/hosts",
"LogPath": "/var/lib/docker/containers/3cf6fc9bc61c00647b95b88f6a8af9e35eabd06c7fdc46278ee38da237050410/3cf6fc9bc61c00647b95b88f6a8af9e35eabd06c7fdc46278ee38da237050410-json.log",
"Name": "/agitated_visvesvaraya",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "docker-default",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "host",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/ace00a02f3d7912e0a1880237c628ebe03694655b08d2414359cffad7d093123-init/diff:/var/lib/docker/overlay2/6046f44170c775dd8216fb38c64d14f468d165d0271a044c16cd7a1e09e5046d/diff",
"MergedDir": "/var/lib/docker/overlay2/ace00a02f3d7912e0a1880237c628ebe03694655b08d2414359cffad7d093123/merged",
"UpperDir": "/var/lib/docker/overlay2/ace00a02f3d7912e0a1880237c628ebe03694655b08d2414359cffad7d093123/diff",
"WorkDir": "/var/lib/docker/overlay2/ace00a02f3d7912e0a1880237c628ebe03694655b08d2414359cffad7d093123/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "3cf6fc9bc61c",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"while true;do echo kongzhou;sleep 1;done"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20201204",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "ff8484bea0cc6734e492713115ca7c2c3e224d0d88dde3bd864e031c18bf38b6",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/ff8484bea0cc",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "0adfef4b6d95c9578bdd03c16773fe4071cc0ad124f701eac4c546433442fc83",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "f989d926b61313d32ba7442e1e69f4d7498f0c8e28818de42af301fc823b0386",
"EndpointID": "0adfef4b6d95c9578bdd03c16773fe4071cc0ad124f701eac4c546433442fc83",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]

进入当前正在运行的容器

1
2
3
4
5
6
# 我们通常容器都是使用后台方式运行的,需要进入容器,修改一些配置

# 命令
docker exec -it 容器id bashshell # 进入容器后开启一个新的终端,可以在里面操作(常用)

docker attach 容器id # 进入容器正在执行的终端,不会启动新的进程

从容器内拷贝文件到主机上

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
docker cp 容器id:容器内路径 目的的主机路径

# 查看容器id
root@kongzhou-virtual-machine:/home# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3e26eda4d0d9 centos "/bin/bash" 4 minutes ago Up 4 minutes pensive_bassi

# 进入docker容器内部
root@kongzhou-virtual-machine:/home# docker attach 3e26eda4d0d9
[root@3e26eda4d0d9 /]#

# 在容器内新建一个文件
[root@3e26eda4d0d9 /]# touch test.java
[root@3e26eda4d0d9 /]# ls
bin etc lib lost+found mnt proc run srv test.java usr
dev home lib64 media opt root sbin sys tmp var

# 从文件拷贝到主机
root@kongzhou-virtual-machine:/home/kongzhou# docker cp 3e26eda4d0d9:/test.java /home
root@kongzhou-virtual-machine:/home/kongzhou# cd /home
root@kongzhou-virtual-machine:/home# ls
kongzhou test.java
支持一下
扫一扫,支持forsigner
  • 微信扫一扫
  • 支付宝扫一扫