Docker基础命令与镜像

Docker

参考资料

官方文档:https://docs.docker.com/docker-for-windows/

【官方文档超级详细】

仓库地址:https://hub.docker.com/

【发布到仓库,git pull push】

b站教程:https://www.bilibili.com/video/BV1og4y1q7M4?

【这个教程非常简洁!且深入!基于企业应用场景!推荐!以下笔记都基于该课程】

前期基础

linux基本命令,类似cd,mkdir等

Docker概述

Docker为什么会出现

一款产品,开发和上线两套环境,应用环境配置费时费力,而且容易出问题

尤其对于机器学习和深度学习的库更是如此,很可能存在版本问题、底层依赖冲突问题

所以发布项目时,不只是一套代码过去,而是代码+环境整体打包过去

所谓开发即运维,保证系统稳定性,提高部署效率

使用Docker后的流程:

开发:建立模型–环境–打包带上环境,即镜像–放到Docker仓库

部署:下载Docker中的镜像,直接运行即可

Docker的思想来自于集装箱,集装箱,对环境进行隔离

Docker通过隔离机制,可以将服务器利用到极致。

Docker的历史

2010年,几个搞IT的人,在美国成立一家公司dotCloud

做一些pass的云计算服务

他们将自己的容器化技术命名为Docker

Docker基于Go语言开发

Docker刚刚诞生的时候,没有引起行业的注意,dotCloud活不下去

然后他们决定开源

2013年,创始人将Docker开源,不开则以,一开惊人,刚开源的时候,每个月都会更新一个版本

2014年4月9日,Docker 1.0发布

容器vs虚拟机

在容器技术出来之前,用的是虚拟机技术

虚拟机原理示意图

缺点:

  1. 资源占用多
  2. 冗余步骤多
  3. 启动很慢

容器化技术示意图

不是模拟的完整的操作系统

二者对比

比较虚拟机和Docker的不同

传统虚拟机 Docker
虚拟内容 硬件+完整的操作系统+软件 APP+LIB
大小 笨重,通常几个G 轻便几个M或KB
启动速度 慢,分钟级 快,秒级

Docker安装

Docker的基本组成

明确几个概念:

镜像(image):docker镜像好比一个模板,可以通过这个模板来创建容器(container),一个镜像可以创建多个容器,类似Python中的Class

容器(container):类似Python中通过Class创建的实例,Object;容器可以理解为一个简易的系统

仓库(repository):存放镜像的地方,

分为共有仓库和私有仓库

  • Docker Hub:国外的

  • 阿里云:配置镜像加速

环境准备

我们要有一台服务器,并且可以操作它

  1. Linux命令基础,购买linux阿里云的服务器
  2. CentOS 7
  3. 使用Xshell链接远程服务器

安装xshell

下载CentOS7 https://www.jianshu.com/p/a63f47e096e8

下载VMware 360软件管家下载

VMware配置虚拟机 https://blog.csdn.net/babyxue/article/details/80970526

xshell链接服务器 https://blog.csdn.net/zzy1078689276/article/details/77280814

1
2
3
4
5
6
7
[root@192 ~]# cd /
[root@192 /]# pwd
/
[root@192 /]# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[root@192 /]# uname -r
3.10.0-1127.el7.x86_64

用户名

密码

查看ip:vmware里面输入,ip addIPr

视频书签,p602:20

https://www.bilibili.com/video/BV1og4y1q7M4?p=6

Centos安装

https://docs.docker.com/engine/install/centos/

卸载旧的版本

1
2
3
4
5
6
7
8
9
10
# 卸载旧的版本
$ sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine

安装基本环境

1
2
# 安装基本的安装包
$ sudo yum install -y yum-utils

设置镜像的仓库

注意!!下载默认用国外的,太慢不要用!

用国内镜像,百度搜索,docker的阿里云镜像地址

1
2
3
4
5
6
7
8
9
10
# 不要用官网默认这个!
$ sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo # 默认是国外的

# 换成下面的

$ sudo yum-config-manager \
--add-repo \
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # 阿里云镜像

直接复制粘贴就OK了

更像软件包索引

1
yum makecache fast

没有问题的话就是可以用的

安装docker引擎

1
yum install docker-ce docker-ce-cli containerd.io # docker-ce 社区版 ee 企业版

注意这里会有几个个y/n的判断

要看到Complet再收手!

启动Docker

1
systemctl start docker # 代表启动成功
1
docker version
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
Client: Docker Engine - Community
Version: 19.03.11
API version: 1.40
Go version: go1.13.10
Git commit: 42e35e61f3
Built: Mon Jun 1 09:13:48 2020
OS/Arch: linux/amd64
Experimental: false

Server: Docker Engine - Community
Engine:
Version: 19.03.11
API version: 1.40 (minimum version 1.12)
Go version: go1.13.10
Git commit: 42e35e61f3
Built: Mon Jun 1 09:12:26 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.2.13
GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683

1
docker run hello-world

中间一堆是签名信息

run的运行流程图

查看下载的镜像

1
docker images

卸载Docker

1
2
3
4
5
6
# 卸载依赖
yum remove docker-ce docker-ce-cli containerd.io

# 删除资源
rm -rf /var/lib/docker # docker 的默认工作路径

阿里云镜像加速

支付宝扫码登录,短信验证,确认授权

我有两个问题

  1. 阿里云镜像加速必须配置嘛?

    哎呦我去: 加速快

    哎呦我去: 要是翻墙就无所谓了

  2. 这个阿里云必须要买嘛,买哪个,我看阿里云好多产品

    哎呦我去:不需要买

    哎呦我去:免费的

    哎呦我去:阿里云搜索容器服务

    哎呦我去:有一个镜像加速

  1. 这个地址是哪来的呀

操作

控制台搜索 容器镜像服务

找到加速地址

1
2
3
4
5
6
7
8
9
sudo mkdir -p /etc/docker # 创建一个陌路
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://uyfgafsw.mirror.aliyuncs.com"]
}
EOF # 编写配置文件

sudo systemctl daemon-reload # 重启服务
sudo systemctl restart docker # 重启docker

在线画图软件ProcessOn

服务器及购买相关介绍

底层原理

Docker是真么工作的?

Docker是一个Client-Server结构的系统,Docker的守护进程在主机上。通过Socket从客户端访问!

DockerServer接受到Docker-Client的指令,

Docker为什么比VM快?

  1. Docker有着比虚拟机更少的抽象层

  2. docker主要用的是宿主机的内核,vm需要Guest OS

所以说新建一个容器的时候,docker不需要像虚拟机一样重新加载一个操作系统内核,避免引导

Docker命令

帮助命令

1
2
3
docker version # 显示docker的基本信息
docker info # 系统信息,镜像和容器的数量
docker 命令 --help # 全部信息

官网文档

镜像命令

docker images

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

1
2
[root@192 ~]# docker images

1
2
3
4
5
6
# 解释
REPOSITORY # 镜像仓库源
TAG # 镜像的标签
IMAGE ID # 镜像的ID
CREATED # 镜像的创建时间
SIZE # 镜像的大小
1
2
3
4
5
6
--all , -a		Show all images (default hides intermediate images) # 显示所有
--digests Show digests
--filter , -f Filter output based on conditions provided
--format Pretty-print images using a Go template
--no-trunc Don’t truncate output
--quiet , -q Only show numeric IDs # 只显示id

搜索仓库中的镜像,相当于网页搜索

网页搜索

mysql

1
docker search mysql
1
docker search --help
1
2
3
4
5
6
7
# 解释
Options:
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print search using a Go template
--limit int Max number of search results (default 25)
--no-trunc Don't truncate output

1
docker search mysql --filter=STARS=3000 # 搜索出Stars大于3000的

docker pull

下载镜像

1
docker pull mysql # 下载mysql镜像,default tag,默认最新版latest
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
[root@192 ~]# sudo systemctl daemon-reload
[root@192 ~]# sudo systemctl restart docker
[root@192 ~]# docker pull mysql
Using default tag: latest # 不写tag默认最新版
latest: Pulling from library/mysql
8559a31e96f4: Pull complete # layer 分层下载,docker image的核心 联合文件系统
d51ce1c2e575: Pull complete
c2344adc4858: Pull complete
fcf3ceff18fc: Pull complete
16da0c38dc5b: Pull complete
b905d1797e97: Pull complete
4b50d1c6b05c: Pull complete
c75914a65ca2: Pull complete
1ae8042bdd09: Pull complete
453ac13c00a3: Pull complete
9e680cd72f08: Pull complete
a6b5dc864b6c: Pull complete
Digest: sha256:8b7b328a7ff6de46ef96bcf83af048cb00a1c86282bfca0cb119c84568b4caf6#签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址

# 即
docker pull mysql
# 等价于
docker pull docker.io/library/mysql:latest

1
2
# 指定版本下载 
docker pull mysql:5.7

版本来自于官网,版本库https://hub.docker.com/_/mysql

1
docker images

此时查看镜像,可以看到新下载的两个

docker rmi

remove images

1
2
3
4
5
6
# 删除一个 可以通过名称 也可以指定id -f表示删除所有
docker rmi -f 9cfcce23593a
# 删除多个 用空格分隔id
docker rmi -f id id id
# 删除所有
docker rmi -f $(docker images -aq) # images -aq就是查所有镜像id,从而递归删除

容器命令

说明:有了镜像才能创建容器,linux,下载一个centos镜像来测试学习

1
docker pull centos

新建容器并启动

1
2
3
4
5
6
7
8
9
10
11
12
docker run [可选参数] image

# 参数说明
--name=“Name” # 容器名字,用于区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 如-p 8080::8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口
-p 容器端口

-p 随机指定端口

进入退出容器

1
2
3
4
5
6
# 进入
docker run -it centos /bin/bash
# 查看目录
ls
# 退出
exit

注意这里面主机名,变成了centos的id

这里面就是一个容器,套娃啊

查看运行的容器

1
2
3
4
5
6
7
8
# 查看正在运行的容器
docker ps
# 查看曾经运行的容器
docker ps -a
# 显示最近创建的容器,设置显示个数
docker ps -a - n=?
# 只显示容器的编号
docker ps -aq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@192 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@192 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9939864fa2e6 centos "bin/bash" 4 minutes ago Exited (0) 4 minutes ago unruffled_knuth
5f42e9930435 centos "/bin/bash" 8 minutes ago Exited (0) 4 minutes ago lucid_cannon
a89ddb393d3d bf756fb1ae65 "/hello" 19 hours ago Exited (0) 19 hours ago gracious_bhabha
[root@192 ~]# docker ps -a -n=2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9939864fa2e6 centos "bin/bash" 6 minutes ago Exited (0) 6 minutes ago unruffled_knuth
5f42e9930435 centos "/bin/bash" 10 minutes ago Exited (0) 7 minutes ago
[root@192 ~]# docker ps -aq
9939864fa2e6
5f42e9930435
a89ddb393d3d

退出容器

1
2
3
4
# 容器停止退出
exit
# 容器不停止退出 注意必须在英文输入法下,中文输入法不行
Ctrl + P + Q
1
2
3
4
5
6
7
8
9
[root@192 ~]# docker run -it centos /bin/bash
[root@bfcea13c40cd /]# [root@192 ~]# docker ps ##注意这里会自动给个命令,删掉
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bfcea13c40cd centos "/bin/bash" About a minute ago Up About a minute stoic_wilson
edbd9366d959 centos "/bin/bash" 7 minutes ago Up 7 minutes affectionate_bartik
[root@192 ~]# docker exec -it edbd9366d959 /bin/bash ## 再次进入
[root@edbd9366d959 /]# exit ##停止并推出
exit

删除容器

1
2
3
4
5
6
# 删除指定容器 不能删除正在运行的容器,如果强制删除 rm -f
docker rm 容器id
# 删除所有容器
docker rm -f $(docker ps -aq)
# 删除所有容器
docker ps -a -q|xargs docker rm
1
2
3
4
5
6
7
8
9
10
11
12
[root@192 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bfcea13c40cd centos "/bin/bash" 29 minutes ago Up 29 minutes stoic_wilson
edbd9366d959 centos "/bin/bash" 35 minutes ago Up 35 minutes affectionate_bartik
9939864fa2e6 centos "bin/bash" 48 minutes ago Exited (0) 48 minutes ago unruffled_knuth
5f42e9930435 centos "/bin/bash" 52 minutes ago Exited (0) 49 minutes ago lucid_cannon
a89ddb393d3d bf756fb1ae65 "/hello" 20 hours ago Exited (0) 20 hours ago gracious_bhabha
[root@192 ~]# docker rm 5f42e9930435
5f42e9930435
[root@192 ~]# docker rm edbd9366d959 # 注意正在运行的容器不能删除
Error response from daemon: You cannot remove a running container edbd9366d9596c744dd449119269b04de2f2a494e7fc471f6396bcefd94c33fe. Stop the container before attempting removal or force remove

1
2
3
4
5
6
7
8
9
10
11
[root@192 ~]# docker ps -aq # 所有容器id
bfcea13c40cd
edbd9366d959
9939864fa2e6
a89ddb393d3d
[root@192 ~]# docker rm -f $(docker ps -aq) # 全部删除
bfcea13c40cd
edbd9366d959
9939864fa2e6
a89ddb393d3d

启动和停止容器的操作

1
2
3
4
docker start
docker restart
docker stop
docker kill
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@192 ~]# docker run -it centos /bin/bash
[root@7b1a7dd10ea4 /]# exit
exit
[root@192 ~]# docker ps #查看正在运行的
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@192 ~]# docker ps -a # 查看历史运行过的
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7b1a7dd10ea4 centos "/bin/bash" 54 seconds ago Exited (0) 42 seconds ago fervent_mirzakhani
[root@192 ~]# docker start 7b1a7dd10ea4 # 启动当前这个容器 container id 粘过 来
7b1a7dd10ea4
[root@192 ~]# docker ps # 查看当前运行容器 发现启动成功
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7b1a7dd10ea4 centos "/bin/bash" 2 minutes ago Up 28 seconds fervent_mirzakhani
[root@192 ~]# docker stop 7b1a7dd10ea4 # 停止运行
7b1a7dd10ea4
[root@192 ~]# docker ps # 再次查看 没有这个容器了
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES


后台启动docker

1
2
3
4
docker run -d 镜像名
# 用docker ps 查看的时候 发现停止了

# 后台运行,docker发现前台没有,容器启动后,发现自己没有提供服务,会立刻停止
1
2
3
4
5
6
7
8
9
Last login: Wed Jun 17 19:47:35 2020
[root@192 ~]# systemctl start docker # 关机后重启了,需要启动docker
[root@192 ~]# docker run -d centos # 运行
8ce188e5fee31c2fac93c0a405ee1a95c38dbc50cb47c35b19c0039c27558ded
[root@192 ~]# docker ps -a # 查看正在运行的
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8ce188e5fee3 centos "/bin/bash" 19 seconds ago Exited (0) 18 seconds ago tender_dirac
7b1a7dd10ea4 centos "/bin/bash" 8 hours ago Exited (0) 8 hours ago fervent_mirzakhani

查看日志

1
2
3
docker logs
docker logs -f -t --tail n 【id】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@192 ~]# docker logs --help

Usage: docker logs [OPTIONS] CONTAINER

Fetch the logs of a container

Options:
--details Show extra details provided to logs
-f, --follow Follow log output
--since string Show logs since timestamp (e.g.
2013-01-02T13:23:37) or relative
(e.g. 42m for 42 minutes)
--tail string Number of lines to show from the
end of the logs (default "all")
-t, --timestamps Show timestamps # 时间戳
--until string Show logs before a timestamp (e.g.
2013-01-02T13:23:37) or relative
(e.g. 42m for 42 minutes)

1
whiel true;do echo shenzai;sleep 
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
# 运行一个
[root@192 ~]# docker run -it centos /bin/bash

[root@c2887d35c71d /]# [root@192 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c2887d35c71d centos "/bin/bash" 57 seconds ago Up 56 seconds vigorous_kare

# 查看日志,由于没有运行脚本,所以啥也没显示
[root@192 ~]# docker logs -f -t --tail 10 c2887d35c71d
^C # ctrl+c退出

# 运行centos里面加个脚本
[root@192 ~]# docker run -d centos /bin/sh -c "while true;do echo XHD天下最帅;sleep 1;done"
cb6d7fbc3f27a064137d58282de97b97365dea2705211ebfbad642079cc1b388

[root@192 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cb6d7fbc3f27 centos "/bin/sh -c 'while t…" 7 seconds ago Up 6 seconds dreamy_almeida
c2887d35c71d centos "/bin/bash" 3 minutes ago Up 3 minutes vigorous_kare

# 查看日志 发现隔一秒打印一条
[root@192 ~]# docker logs -f -t --tail 10 cb6d7fbc3f27
2020-06-17T12:02:11.293765084Z shenzai
2020-06-17T12:02:12.297675608Z shenzai
2020-06-17T12:02:13.301845582Z shenzai
2020-06-17T12:02:14.304800996Z shenzai
2020-06-17T12:02:15.307130238Z shenzai
2020-06-17T12:02:16.310574235Z shenzai
2020-06-17T12:02:17.312946923Z shenzai
2020-06-17T12:02:18.314841295Z shenzai
2020-06-17T12:02:19.317021705Z shenzai
2020-06-17T12:02:20.319670013Z shenzai
2020-06-17T12:02:21.322651649Z shenzai
2020-06-17T12:02:22.325466918Z shenzai
2020-06-17T12:02:23.327984704Z shenzai
2020-06-17T12:02:24.329656919Z shenzai

查看正在运行的容器信息

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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
[root@192 ~]# docker inspect cb6d7fbc3f27
[
{
# 容器的完整id
"Id": "cb6d7fbc3f27a064137d58282de97b97365dea2705211ebfbad642079cc1b388",

# 创建时间
"Created": "2020-06-17T12:00:50.706906186Z",

# 脚本位置
"Path": "/bin/sh",

# 运行的脚本
"Args": [
"-c",
"while true;do echo shenzai;sleep 1;done"
],
"State": {
"Status": "running", # 状态,正在运行
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 1909, # 父进程id
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-06-17T12:00:51.093617477Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},

# 来源镜像
"Image": "sha256:831691599b88ad6cc2a4abbd0e89661a121aff14cfa289ad840fd3946f274f1f",
"ResolvConfPath": "/var/lib/docker/containers/cb6d7fbc3f27a064137d58282de97b97365dea2705211ebfbad642079cc1b388/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/cb6d7fbc3f27a064137d58282de97b97365dea2705211ebfbad642079cc1b388/hostname",
"HostsPath": "/var/lib/docker/containers/cb6d7fbc3f27a064137d58282de97b97365dea2705211ebfbad642079cc1b388/hosts",
"LogPath": "/var/lib/docker/containers/cb6d7fbc3f27a064137d58282de97b97365dea2705211ebfbad642079cc1b388/cb6d7fbc3f27a064137d58282de97b97365dea2705211ebfbad642079cc1b388-json.log",


"Name": "/dreamy_almeida",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"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,
"Capabilities": null,
"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/3675586ebbd79cd72d2562a90c9380627a331c563724c0dac091f92600af4907-init/diff:/var/lib/docker/overlay2/7f79322e0f58d651a84a555dadd83d92537788172525945d3f538dd95dce336c/diff",
"MergedDir": "/var/lib/docker/overlay2/3675586ebbd79cd72d2562a90c9380627a331c563724c0dac091f92600af4907/merged",
"UpperDir": "/var/lib/docker/overlay2/3675586ebbd79cd72d2562a90c9380627a331c563724c0dac091f92600af4907/diff",
"WorkDir": "/var/lib/docker/overlay2/3675586ebbd79cd72d2562a90c9380627a331c563724c0dac091f92600af4907/work"
},
"Name": "overlay2"
},

"Mounts": [], # 挂载

# 基本配置
"Config": {
"Hostname": "cb6d7fbc3f27",
"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"
], # 基本环境变量,这里没有Java

# 基本命令
"Cmd": [
"/bin/sh",
"-c",
"while true;do echo shenzai;sleep 1;done"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20200611",
"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": "4d701985d7e77aa153790b697b2f38a61e20555c224b7675e4bf650b82799882",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/4d701985d7e7",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "8a6c71e2bafb19ca7dfd85445ccc4bef6d17467360a243d624089e676a24a018",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "22b0fd2290ccbc4e066a75d3f01bd8bf32ee4352c5bbcfc9f911287219219571",
"EndpointID": "8a6c71e2bafb19ca7dfd85445ccc4bef6d17467360a243d624089e676a24a018",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
}
}
}
}
]

1
2
3
# 停止正在疯狂输出的那个容器
[root@192 ~]# docker stop cb6d7fbc3f27
cb6d7fbc3f27

进入当前正在运行的容器

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
# 我们通常容器都是使用后台方式运行的e

docker exec -it 容器id bashSHELL

# 测试
[root@192 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c2887d35c71d centos "/bin/bash" 35 minutes ago Up 35 minutes vigorous_kare
[root@192 ~]# docker exec -it c2887d35c71d /bin/bash
[root@c2887d35c71d /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@c2887d35c71d /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 11:57 pts/0 00:00:00 /bin/bash
root 14 0 0 12:32 pts/1 00:00:00 /bin/bash
root 28 14 0 12:32 pts/1 00:00:00 ps -ef
[root@c2887d35c71d /]# c2887d35c71d
[root@c2887d35c71d /]# exit
exit

# 方式二
[root@192 ~]# docker attach c2887d35c71d
[root@c2887d35c71d /]#

# 区别
# docker exec # 进入容器后开启一个新的终端,可以在里面操作(常用)
# docker attach 进入容器正在执行的终端,不会启动新的进程

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

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
# 运行
[root@192 ~]# docker run -it centos
# ctrl P Q 不关闭退出,查看
[root@0569081aa89c /]# [root@192 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0569081aa89c centos "/bin/bash" 19 seconds ago Up 19 seconds
hopeful_chebyshev

# 查看主机home下无文件
[root@192 ~]# cd /home
[root@192 home]# ls

# 进入正在运行的容器
[root@192 home]# docker attach 0569081aa89c

# 进入容器home目录
[root@0569081aa89c /]# cd /home

# 在目录中创建java文件
[root@0569081aa89c home]# touch test.java

# 退出并停止容器
[root@0569081aa89c home]# exit
exit

# 查看现在运行的容器
[root@192 home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

# 容器虽然被停止,但是数据都会保留
[root@192 home]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0569081aa89c centos "/bin/bash" 3 minutes ago Exited (0) 8 seconds ago hopeful_chebyshev
f589e5684a01 centos "/bin/bash" 44 minutes ago Exited (0) 44 minutes ago cranky_easley
cb6d7fbc3f27 centos "/bin/sh -c 'while t…" 54 minutes ago Exited (137) 42 minutes ago dreamy_almeida
c2887d35c71d centos "/bin/bash" 58 minutes ago Exited (127) 16 minutes ago vigorous_kare
8ce188e5fee3 centos "/bin/bash" About an hour ago Exited (0) About an hour ago tender_dirac
7b1a7dd10ea4 centos "/bin/bash" 9 hours ago Exited (0) 9 hours ago fervent_mirzakhani

# 容器数据拷贝到主机
[root@192 home]# docker cp 0569081aa89c:/home/test.java /home
[root@192 home]# ls
test.java

# 拷贝是一个手动过程,未来我们使用 -v 卷的技术,可以实现自动同步 /home /home

查看内容占用

1
2
docker stats

小结

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
[root@192 home]# docker --help

Usage: docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
--config string Location of client config
files (default "/root/.docker")
-c, --context string Name of the context to use to
connect to the daemon
(overrides DOCKER_HOST env var
and default context set with
"docker context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level
("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by
this CA (default
"/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file
(default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default
"/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit

Management Commands:
builder Manage builds
config Manage Docker configs
container Manage containers
context Manage contexts
engine Manage the docker engine
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes

Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes

image-20200617210932306

image-20200617211021003

image-20200617211039508

Docker镜像

原理

UnionFS 联合文件系统

联合文件系统(UnionFS)是一种分层、轻量级并且高性能的文件系统,它支持对文件系统的修改作为一次提交来一层层的叠加,同时可以将不同目录挂载到同一个虚拟文件系统下(unite several directories into a single virtual filesystem)。

联合文件系统是 Docker 镜像的基础。镜像可以通过分层来进行继承,基于基础镜像(没有父镜像),可以制作各种具体的应用镜像。

另外,不同 Docker 容器就可以共享一些基础的文件系统层,同时再加上自己独有的改动层,大大提高了存储的效率。

Docker 中使用的 AUFS(Advanced Multi-Layered Unification Filesystem)就是一种联合文件系统。 AUFS 支持为每一个成员目录(类似 Git 的分支)设定只读(readonly)、读写(readwrite)和写出(whiteout-able)权限, 同时 AUFS 里有一个类似分层的概念, 对只读权限的分支可以逻辑上进行增量地修改(不影响只读部分的)。

Docker 目前支持的联合文件系统包括 OverlayFS, AUFS, Btrfs, VFS, ZFSDevice Mapper

各 Linux 发行版 Docker 推荐使用的存储驱动如下表。

Linux 发行版 Docker 推荐使用的存储驱动
Docker on Ubuntu overlay2 (16.04 +)
Docker on Debian overlay2 (Debian Stretch), aufs, devicemapper
Docker on CentOS overlay2
Docker on Fedora overlay2

在可能的情况下,推荐 使用 overlay2 存储驱动,overlay2 是目前 Docker 默认的存储驱动,以前则是 aufs。你可以通过配置来使用以上提到的其他类型的存储驱动。

bootfs:boot file system

rootfs:root file system

Docker镜像都是只读的,当容器启动时,一个新的可写层被加到镜像的顶部,这一层就是我们通常说的容器层,容器层之下的都叫镜像层

commit提交镜像

1
2
docker commit # 提交容器成为一个新的副本
docker commit -m="提交的描述信息" -a="作者" 容器id 目标镜像名:[TAG]
1
2
docker images
docker run -it -p 8080:8080 tomcat

这是一个前台程序

将webapps.dist里面所有的文件拷贝到webapps里面,其中-r必须有,表示目录递归拷贝

1
docker commit -a="paidaxing" -m="add webapps app" 当前容器的id tomcat02:1.0

发现新的版本,比之前的大了一些,因为里面记录了我们的改动

如果想保存当前容器的状态,可以通过commit提交,获得一个镜像

好比我们以前学习VM的时候的快照

到这里算是入门了

接下来三个部分是docker的精髓

谢谢你的支持哦,继续加油.