0%

Docker

Docker学习笔记

Docker基本概念

Docker基本组成:

image-20220119165007270

镜像(image)

Docker镜像好比一个模板,可以通过这个模板创建容器服务,tomcat镜像==>run ==>tomcat 01容器(提供服务),通过镜像可以创建多个容器。

容器(Container)

Docker利用容器技术,独立运行一个或一组应用程序,通过镜像来创建。

可以将容器理解为一个简易的linux系统。

仓库(Repository)

仓库就是存放镜像的地方。

仓库分为公有仓库和私有仓库。

Docker与虚拟机的区别:

image-20220119163814035

虚拟机是一个缩小版的操作系统,不同的应用共用一个运行环境。

image-20220119163535959

docker将不同的应用与其运行环境封装成一个个容器,并放置于OS内核上运行。不同容器之间相互隔离,每个容器有自己独立的文件系统。

DevOps(开发、运维)

应用更快速的交付和部署

Docker:打包镜像发布测试,一键运行。

更便捷的升级和扩缩容

使用Docker之后,我们部署应用如同搭积木。

更简单的系统运维

在容器化之后,开发和测试环境高度一致。

更高效的计算资源利用

Docker是内核级别的虚拟化,可以在一个物理机上运行多个容器实例。

Docker为什么比VM快?

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

  2. Docker利用宿主机的内核,VM需要搭建额外的环境。

    image-20220119184810155

    新建一个容器的时候,Docker不需要像虚拟机一样额外加载一个操作系统内核。

Docker run运行机制

image-20220119183156875

Docker底层实现

image-20220119184310423

Docker常用命令

镜像命令

查看所有镜像

1
2
3
root@node10-desktop:/home/ubuntu# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 3 months ago 13.3kB

可选选项

1
2
3
4
5
6
7
8
9
10
11
Usage:  docker images [OPTIONS] [REPOSITORY[:TAG]]

List images

Options:
-a, --all #展示所有镜像
--digests Show digests
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print images using a Go template
--no-trunc Don't truncate output
-q, --quiet Only show image IDs

搜索镜像

1
2
3
4
root@node10-desktop:/home/ubuntu# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11992 [OK]
mariadb MariaDB Server is a high performing open sou… 4591 [OK]

可选选项

1
2
3
4
ubuntu@node10-desktop:~$ docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11992 [OK]
mariadb MariaDB Server is a high performing open sou… 4591 [OK]

搜索stars大于3000的。

下载镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 下载镜像 docker pull 镜像名[:tag]
ubuntu@node10-desktop:~$ docker pull mysql
Using default tag: latest #如果不写 tag ,默认是latest
latest: Pulling from library/mysql
72a69066d2fe: Pull complete # 分层下载, docker iamges的核心 联合文件系统
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709 #签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真实地址

下载mysql 5.7版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ubuntu@node10-desktop:~$ docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Already exists
93619dbc5b36: Already exists
99da31dd6142: Already exists
626033c43d70: Already exists
37d5d7efb64e: Already exists
ac563158d721: Already exists
d2ba16033dad: Already exists #以上层级内容共用之前版本
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

删除镜像

1
2
3
4
5
6
7
8
9
10
11
# docker rmi -f 容器id
# docker rmi -f 容器id 容器id 容器id 容器id #删除多个容器
# docker rmi -f $(docker images -aq) #删除全部容器
ubuntu@node10-desktop:~$ docker rmi c20987f18b13 #按照镜像id删除
Untagged: mysql:5.7
Untagged: mysql@sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Deleted: sha256:c20987f18b130f9d144c9828df630417e2a9523148930dc3963e9d0dab302a76
Deleted: sha256:6567396b065ee734fb2dbb80c8923324a778426dfd01969f091f1ab2d52c7989
Deleted: sha256:0910f12649d514b471f1583a16f672ab67e3d29d9833a15dc2df50dd5536e40f
Deleted: sha256:6682af2fb40555c448b84711c7302d0f86fc716bbe9c7dc7dbd739ef9d757150
Deleted: sha256:5c062c3ac20f576d24454e74781511a5f96739f289edaadf2de934d06e910b92

容器命令

说明:有了镜像才可以创建容器

1
docker pull centos

新建容器并启动

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

#参数说明
--name="Name" 容器名字 tomcat01 tomcat02 用于区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器内部查看内容
-p 指定容器使用的端口 -p 8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
容器端口
-P 随机指定端口


#测试,启动并进入容器
ubuntu@node10-desktop:~$ docker run -it centos /bin/bash
[root@ad604a65efa9 /]# ls #查看内部的centos 基础版本 很多命令不完善
bin etc lib lost+found mnt proc run srv tmp var dev home lib64 media opt root sbin sys usr
[root@ad604a65efa9 /]# exit #退出

列出所有的运行的容器

1
2
3
4
5
6
7
8
9
10
11
docker ps     #列出所有正在运行的容器
-a #列出所有正在和曾经运行的容器
-n=? #列出最近创建的n个容器
-q #只显示容器的编号
ubuntu@node10-desktop:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ubuntu@node10-desktop:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ad604a65efa9 centos "/bin/bash" 5 minutes ago Exited (127) 3 minutes ago sharp_jang
a4688cab1ed2 feb5d9fea6a5 "/hello" 2 hours ago Exited (0) 2 hours ago loving_gould
aaf4c88c96a7 feb5d9fea6a5 "/hello" 2 hours ago Exited (0) 2 hours ago serene_lehmann

退出容器

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

删除容器

1
2
3
docker rm 容器id                #删除指定的容器
docker rm -f $(docker ps -aq) #删除所有的容器 -f强制删除(可以删除正在运行的容器)
docker ps -a -q | xargs docker rm #管道方式删除所有容器

启动和停止容器

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

常用其他命令

后台启动容器

1
2
3
4
5
6
7
8
9
10
#命令 Docker run -d 镜像名
ubuntu@node10-desktop:~$ docker run -d centos
3f38613fb472b3fbdd6f746192f3e8e11867fcc63d9cf8991ea8cfb07c96dbd3
ubuntu@node10-desktop:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

#问题 docker ps ,cent os 停止了

#常见的坑:docker,容器使用后台运行,必须有一个前台进程,docker发现没有应用,就会自动停止
#nginx,容器启动后,会发现自己没有提供服务,就会立刻停止。

查看日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
docker logs -tf --tail 容器 #没有日志

#自己编写一段shell脚本,并在容器中执行
ubuntu@node10-desktop:~$ docker run -d centos /bin/sh -c "while true;do echo fisher626;sleep 1;done"
3815fe287c92d7145e4da54769adb8552c7e95df51de0e0fe8876b71e81c3622
ubuntu@node10-desktop:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3815fe287c92 centos "/bin/sh -c 'while t…" 9 seconds ago Up 8 seconds distracted_pare

#查看日志
ubuntu@node10-desktop:~$ docker logs -tf --tail 10 3815fe287c92#显示最新的10条日志
2022-01-19T13:01:33.174886561Z fisher626
2022-01-19T13:01:34.178405517Z fisher626
2022-01-19T13:01:35.181766267Z fisher626
2022-01-19T13:01:36.185055836Z fisher626
2022-01-19T13:01:37.188711727Z fisher626
2022-01-19T13:01:38.192378519Z fisher626
2022-01-19T13:01:39.196042380Z fisher626
2022-01-19T13:01:40.199651762Z fisher626
2022-01-19T13:01:41.203364507Z fisher626
2022-01-19T13:01:42.206862755Z fisher626
2022-01-19T13:01:43.210545827Z fisher626
2022-01-19T13:01:44.214201098Z fisher626
2022-01-19T13:01:45.217870760Z fisher626

查看容器中的进程信息

1
2
3
4
5
6
7
8
#docker top 容器id
ubuntu@node10-desktop:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c3a047896877 centos "/bin/sh -c 'while t…" 14 seconds ago Up 13 seconds serene_thompson
ubuntu@node10-desktop:~$ docker top c3a047896877
UID PID PPID C STIME TTY TIME CMD
root 23055 23033 0 21:04 ? 00:00:00 /bin/sh -c while true;do echo fisher626;sleep 1;done
root 23167 23055 0 21:05 ? 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
#命令
docker inspect 容器id
ubuntu@node10-desktop:~$ docker inspect c3a047896877
[
{
"Id": "c3a0478968772430d034f0c533c3124327a51840d203394915b29113e4ba7007",
"Created": "2022-01-19T13:04:58.365195048Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo fisher626;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 23055,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-01-19T13:04:58.650652312Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/c3a0478968772430d034f0c533c3124327a51840d203394915b29113e4ba7007/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/c3a0478968772430d034f0c533c3124327a51840d203394915b29113e4ba7007/hostname",
"HostsPath": "/var/lib/docker/containers/c3a0478968772430d034f0c533c3124327a51840d203394915b29113e4ba7007/hosts",
"LogPath": "/var/lib/docker/containers/c3a0478968772430d034f0c533c3124327a51840d203394915b29113e4ba7007/c3a0478968772430d034f0c533c3124327a51840d203394915b29113e4ba7007-json.log",
"Name": "/serene_thompson",
}

进入当前正在运行的容器