Docker overview | Docker Documentation
帮助命令docker 命令 --help
镜像命令#显示镜像
[root@VM-12-9-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 7425d3a7c478 4 days ago 142MB
whyour/qinglong latest 2b6f05a5ef3d 6 months ago 419MB
centos latest 5d0da3dc9764 8 months ago 231MB
#可选项
Options:
-a, --all 显示所有镜像
--digests 更深层次的挖掘
-q, --quiet 只显示镜像ID
#搜索镜像
[root@VM-12-9-centos ~]# docker search tomcat
#搜索添加过滤
[root@VM-12-9-centos ~]# docker search tomcat --filter=stars=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
tomcat Apache Tomcat is an open source implementati… 3319 [OK]
#下载镜像 比如下载mysql镜像
[root@VM-12-9-centos ~]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
c32ce6654453: Pull complete #采用分层下载方式
415d08ee031a: Pull complete
7a38fec2542f: Pull complete
352881ee8fe9: Pull complete
b8e20da291b6: Pull complete
66c2a8cc1999: Pull complete
d3a3a8e49878: Pull complete
e33a48832bec: Pull complete
410b942b8b28: Pull complete
d5323c9dd265: Pull complete
3212737f31c0: Pull complete
d0032d4b0dc5: Pull complete
Digest: sha256:a0805d37d4d298bd61e0dfa61f0ddf6f4680b453fa25d7aad420485a62417eab
Status: Downloaded newer image for mysql:latest #默认下载
#指定版本下载
[root@VM-12-9-centos ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
c32ce6654453: Already exists #分层下载这些模块刚才已经下载不需要在下载
415d08ee031a: Already exists
7a38fec2542f: Already exists
352881ee8fe9: Already exists
b8e20da291b6: Already exists
66c2a8cc1999: Already exists
d3a3a8e49878: Already exists
172aabfba65c: Pull complete
fea17d0b1d1e: Pull complete
fff7f5411ca9: Pull complete
c33d43428e07: Pull complete
Digest: sha256:16e159331007eccc069822f7b731272043ed572a79a196a05ffa2ea127caaf67
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
#删除镜像 指定id删除
[root@VM-12-9-centos ~]# docker rmi -f 76152be68449
#删除镜像 全部删除
[root@VM-12-9-centos ~]# docker rmi -f $(docker images -aq)
容器命令
说明:有了镜像才可以创建容器
运行容器&列出容器docker run [可选参数] images
#参数说明
--name="名字" 容器名字用来区分容器
-d 后台方式运行
-it 交互方式运行,进入容器
-p 指定容器端口 -p 8080:8080
-p 8080:8080(主机端口:容器端口)最常用
-p 容器端口
容器端口
—P 随机端口
#测试 启动centos镜像并进入容器
[root@VM-12-9-centos ~]# docker run --name="centos-01" -it centos /bin/bash
[root@7595ff4c38a3 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
#退出
exit #这样的话容器也会关闭
cltrl +p +q #这样不会退出
#查看正在运行的容器
[root@VM-12-9-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e085742b3bd2 centos "/bin/bash" 20 minutes ago Up 20 minutes centos-02
df8d78f75f61 whyour/qinglong:latest "./docker/docker-ent…" 5 weeks ago Up 2 hours 0.0.0.0:5700->5700/tcp, :::5700->5700/tcp qinglong
#####
docker ps命令
#默认不写现实当前正在运行的容器
- a 显示历史运行过的容器
- q 只显示容器id
- n=2 只显示2个
# 然后这几个就可以随意搭配了 比如 -aq -aqn=2
删除容器
#删除没有运行的容器 docker rm 镜像id #查出镜像id并删除 docker rm $(docker ps -aq) #强制删除 docker rm -f 镜像id [root@VM-12-9-centos ~]# docker rm $(docker ps -aq) 7595ff4c38a3 63f0b2eeadcc 6c64b1d4ee06 5cf23795c31c 31ae2505118b Error response from daemon: You cannot remove a running container e085742b3bd2b5b5ac3dd3f35cf569406d98d41da54d530affb4b29ecbccafe0. Stop the container before attempting removal or force removeqi启动重启容器
docker start 容器id docker restart 容器id docker stop 容器id docker kill 容器id #强制关闭容器进入当前运行容器
#这种方式相当于打开一个新的终端 docker -exec -it 容器id 目录 #这种方式进入原始终端 docker attach 容器id其他常用命令
#后台启动
docerk run -d 镜像名
#问题 启动后容器停止运行了
#常见的坑:docker使用后台运行,就必须有一个进程,docker发现没有应用,就会自动停止
#查看日志
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:37Z) or relative (e.g. 42m for 42
minutes)
-n, --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.
ro docker logs -ft -tn=10 df8d78f75f61 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42
minutes)
#举例 现实最近的10条日志 如果么有运行的的日志为空
docker logs -ft -tn=10 容器id
#查看容器中进程
docker top 容器id
[root@VM-12-9-centos ~]# docker top e085742b3bd2
UID PID PPID C STIME TTY TIME CMD
root 15779 15759 0 16:25 pts/0 00:00:00 /bin/bash
#查看镜像元信息
docker inspect 镜像id
容器内拷贝文件到宿主机
#容器关闭也没影响数据都还在 docker cp e085742b3bd2:/home/1.java /home练习 docker部署Nginx
#首先使得docker pull命令拉取Nginx镜像不指定版本拉取最新的 #启动 后台启动 5701端口映射容器内80端口 [root@VM-12-9-centos ~]# docker run -d --name "nginx-01" -p 5701:80 nginx 21901a41b4d078f1af89b61bd9b4073e3ce43a69de086f50914a988c17be4ebf #进入nginx-01容器查看nginx位置 [root@VM-12-9-centos ~]# docker exec -it nginx-01 /bin/bash root@21901a41b4d0:/# ls bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var boot docker-entrypoint.d etc lib media opt root sbin sys usr root@21901a41b4d0:/# whereis nginx nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
运行截图
docker部署Tocat#首先拉取tomcat镜像 我指定的是9.0版本 docker pull tomcat:9.0 #在docker运行tomcat 这里既然下载了9.0版本就要指定版本运行,否则docker会拉取最新版本运行 docker run -d --name tomcat-01 -p 5700:8080 tomcat:9.0 #进入这个容器 [root@VM-12-9-centos ~]# docker exec -it tomcat-01 /bin/bash root@d17ce5534051:/usr/local/tomcat# ls -al total 172 drwxr-xr-x 1 root root 4096 May 11 23:58 . drwxr-xr-x 1 root root 4096 May 11 23:49 .. -rw-r--r-- 1 root root 18980 Mar 31 14:34 BUILDING.txt -rw-r--r-- 1 root root 6210 Mar 31 14:34 CONTRIBUTING.md -rw-r--r-- 1 root root 57092 Mar 31 14:34 LICENSE -rw-r--r-- 1 root root 2333 Mar 31 14:34 NOTICE -rw-r--r-- 1 root root 3378 Mar 31 14:34 README.md -rw-r--r-- 1 root root 6898 Mar 31 14:34 RELEASE-NOTES -rw-r--r-- 1 root root 16497 Mar 31 14:34 RUNNING.txt drwxr-xr-x 2 root root 4096 May 11 23:58 bin drwxr-xr-x 1 root root 4096 May 15 12:43 conf drwxr-xr-x 2 root root 4096 May 11 23:58 lib drwxrwxrwx 1 root root 4096 May 15 12:43 logs drwxr-xr-x 2 root root 4096 May 11 23:58 native-jni-lib drwxrwxrwx 2 root root 4096 May 11 23:58 temp drwxr-xr-x 1 root root 4096 May 15 12:48 webapps drwxr-xr-x 7 root root 4096 Mar 31 14:34 webapps.dist drwxrwxrwx 1 root root 4096 May 15 12:48 work #这个tomcat是精简版本的webapps文件夹里没有任何东西,可以把webapps.dist里的东西拷贝到webapps cp -r webapps.dist/* webapps
运行截图
镜像是一种轻量级,可独立执行的软件包,用来打包运行环境和基于运行环境开发的软件,它包含了某个软件运行需要的的内容,比如代码,运行环境,库,配置文件
docker镜像加载原理联合文件系统(UnionFS)是一种分层、轻量级并且高性能的文件系统,它支持对文件系统的修改作为一次提交来一层层的叠加,同时可以将不同目录挂载到同一个虚拟文件系统下。联合文件系统是
Docker 镜像的基础。镜像可以通过分层来进行继承,基于基础镜像(没有父镜像),可以制作各种具体的应用镜像。
特性:一次同时加载多个文件系统,但从外面看起来只能看到一个文件系统。联合加载会把各层文件系统叠加起来,这样最终的文件系统会包含所有底层的文件和目录。
加载原理
docker的镜像实际上由一层一层的文件系统组成,这种层级的文件系统UnionFS。
看上图内核(kernel)之上有bootfs和rootfs
- bootfs(boot file system)
主要包含bootloader和kernel, bootloader主要是引导加载kernel, Linux刚启动时会加载bootfs文件系统,在Docker镜像的最底层是bootfs。这一层与我们典型的Linux/Unix系统是一样的,包含boot加载器和内核。当boot加载完成之后整个内核就都在内存中了,此时内存的使用权已由bootfs转交给内核,此时系统也会卸载bootfs。
- rootfs (root file system)
在bootfs之上。包含的就是典型 Linux 系统中的 /dev, /proc, /bin, /etc 等标准目录和文件。rootfs就是各种不同的操作系统发行版,比如Ubuntu,Centos等等
- 为何docker秒级启动
对于一个精简的系统,rootfs可以很小只需要简单的命令和工具就可以了,因为底层直接调用Host的kernel,自己只需要提供rootfs就可以,对于不同的linux发现版bootfs基本一致,rootfs有差别,因此不同的发行版可以公有bootfs
分层理解所有的Docker镜像都起始于一个基础镜像层,当进行修改或增加新的内容时,就会在当前镜像层之上,创建新的镜像层。分层时有文件更新直接替换,基础镜像一样时直接拿过来复用。
如redis下载时,第一层相同,直接复用,其他几层分层下载。
afb6ec6fdc1c: Already exists 608641ee4c3f: Pull complete 668ab9e1f4bc: Pull complete 78a12698914e: Pull complete d056855f4300: Pull complete 618fdf7d0dec: Pull complete
举个简单的例子,假如基于 Ubuntu16.04创建一个新的镜像,这就是新镜像的第一层。如果在该镜像中添加Python包,就会在基础镜像层之上创建第二个镜像层。如果继续添加一个安全补丁,就会创健第三个镜像层该像当前已经包含3个镜像层,如下图所示(这只是一个用于演示的很简单的例子)。
在添加额外的镜像层的同时,镜像始终保持是当前所有镜像的组合。一个简单的例子,每个镜像层包含3个文件,而镜像包含了来自两个镜像层的6个文件
所有镜像层堆并合井,对外提供统一的视图
Docker镜像都是只读的,当容器启动时,一个新的可写层加载到镜像的顶部。这一层就是我们通常说的容器层,容器之下的都叫镜像层。
commit提交镜像docker commit 提交容器成为一个新的副本 docker commit -m="提交信息描述" -a="作者" 容器id 目标镜像名:[TAG]
测试 提交刚才修改的tomcat容器
#提交测试 [root@VM-12-9-centos ~]# docker commit -a="Xu" -m="add webapps to tomcat" d17ce5534051 mytomcat:1.0 sha256:2511e0fad92f7e2e215a6a23af34d6cfa0770d6c769bd4d5f80587a8aa0ed90f [root@VM-12-9-centos ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mytomcat 1.0 2511e0fad92f 13 seconds ago 685MB tomcat 9.0 5e9a4d049871 3 days ago 680MB nginx latest 7425d3a7c478 4 days ago 142MB mysql 5.7 a3d35804fa37 4 days ago 462MB whyour/qinglong latest 2b6f05a5ef3d 6 months ago 419MB centos latest 5d0da3dc9764 8 months ago 231MB #运行提交的mytomcat [root@VM-12-9-centos ~]# docker run -d --name="mytomcat-01" -p 5703:8080 mytomcat:1.0 79d69bc4a4b8e59c3e529cdc805e1e1ddf53651972ce915f811ccd8b4a9700db #进入mytomcat容器 [root@VM-12-9-centos ~]# docker exec -it 79d69bc4a4b8 /bin/bash root@79d69bc4a4b8:/usr/local/tomcat# ls BUILDING.txt LICENSE README.md RUNNING.txt conf logs temp webapps.dist CONTRIBUTING.md NOTICE RELEASE-NOTES bin lib native-jni-lib webapps work root@79d69bc4a4b8:/usr/local/tomcat# cd webapps root@79d69bc4a4b8:/usr/local/tomcat/webapps# ls ROOT docs examples host-manager manager
运行结果:webapps已经有东西了5703端口正常映射



