十一、日志、元数据、进程的查看
1、其他常用命令
[root@iZwz99sm8v95sckz8bd2c4Z ~]# 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: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. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
查询日志常用:
docker logs -tf 容器id
docker logs --tail number 容器id #num为要显示的日志条数
#docker容器后台运行,必须要有一个前台的进程,否则会自动停止
#编写shell脚本循环执行,使得centos容器保持运行状态
”[[e[31docker run -d centos /bin/sh -c "while true;do echo hi;sleep 5;done"
3f9b81982653be64fbad8995c981107ec5481102666cfcc992e73b03b80bdb9b
”[[e[31docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f9b81982653 centos "/bin/sh -c 'while t…" 35 seconds ago Up 32 seconds gifted_shaw
”[[e[31docker logs --tail 20 3f9b81982653
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
查看容器中进程信息
容器id
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker top c703b5b1911f
进程id
UID PID PPID C STIME TTY TIME CMD
root 11156 11135 0 11:31 ? 00:00:00 /bin/sh -c while true;do echo hi;sleep 5;done
root 11886 11156 0 11:43 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 5
查看容器的元数据
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker inspect 容器id
十二、进入容器的命令和拷贝命令
1、进入当前正在运行的容器
因为通常我们的容器都是使用后台方式来运行的,有时需要进入容器修改配置
方式一:
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker exec -it c703b5b1911f /bin/bash
[root@c703b5b1911f /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@c703b5b1911f /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 03:31 ? 00:00:00 /bin/sh -c while true;do echo hi;sleep 5;done
root 279 0 0 03:54 pts/0 00:00:00 /bin/bash
root 315 1 0 03:56 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 5
root 316 279 0 03:56 pts/0 00:00:00 ps -ef
方式二:
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker attach c703b5b1911f
docker exec 进入容器后开启一个新的终端,可以在里面操作
docker attach 进入容器正在执行的终端,不会启动新的进程
2、拷贝操作
#拷贝容器的文件到主机中
docker cp 容器id:容器内路径 目的主机路径
#拷贝宿主机的文件到容器中
docker cp 目的主机路径 容器id:容器内路径
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker exec -it c703b5b1911f /bin/bash
[root@c703b5b1911f /]# cd home
[root@c703b5b1911f home]# ls
#touch 新建文件
[root@c703b5b1911f home]# touch test.java
[root@c703b5b1911f home]# ls
test.java
[root@c703b5b1911f home]# exit
exit
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c703b5b1911f centos "/bin/sh -c 'while t…" 35 minutes ago Up 35 minutes pedantic_banach
将容器的文件拷到主机上
”[[e[31docker cp adf2eb122d14:/a.java /home/zl
”[[e[31ls
a.c a.java a.out Desktop Documents Downloads Music Pictures Public ssl Templates Videos
十三、常用命令小结
十四、部署nginx
Docker安装nginx
#1、搜索镜像 search 建议云docker搜索,可以看到帮助文档
#2、拉取镜像 pull
#3、运行
后台运行 给容器起名字 暴露端口: 8081(宿主机)外部 80内部 开启的镜像
docker run -d --name nginx01 -p 8081:80 nginx
#4、测试8081端口
”[[e[31curl localhost:8081
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
端口暴露的概念如图
进入nginx容器
进入nginx容器命令
docker exec -it nginx01 /bin/bash
root@28d548b4b86d:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@28d548b4b86d:/# cd /usr/share/
root@28d548b4b86d:/usr/share# ls
X11 base-passwd ca-certificates debianutils doc-base fonts info libc-bin man misc pam-configs polkit-1 tabset xml
adduser bash-completion common-licenses dict dpkg gcc java lintian maven-repo nginx perl5 readline terminfo zoneinfo
base-files bug debconf doc fontconfig gdb keyrings locale menu pam pixmaps sensible-utils ucf zsh
root@28d548b4b86d:/usr/share# cd nginx/
root@28d548b4b86d:/usr/share/nginx# ls
html
root@28d548b4b86d:/usr/share/nginx# cd /etc/nginx/
root@28d548b4b86d:/etc/nginx# ls
conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
十五、部署tomcat
#官方的使用:
docker run -it --rm tomcat:9.0
#我们之前的启动都是后台,停止了容器之后,容器还是可以查到;docker run -it --rm 一般用来测试,用完就删除
我们还是老老实实的先下载再使用:
先下载
docker pull tomcat //下载最新版本,无指定版本
启动运行
后台运行 给容器起名字 暴露端口: 8082(宿主机)外部 8080内部 开启的镜像
docker run -d --name tomcat01 -p 8082:8080 tomcat
测试访问没有问题
进入容器
docker exec -it tomcat01 /bin/bash
#发现问题,1、Linux命令少了 2、没有webapps 阿里云镜像的原因,默认是最小镜像,所有不必要都剔除了。
#保证最小可运行的环境
十六、部署ES+kibana
#es 1 暴露的端口多;2、十分耗内存;3、其数据一般要放置在安全目录,挂载!
#启动elasticsearch
--net somenetwork 网络配置
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.16.2
#启动了 elasticsearch Linux服务器非常卡 docker stats 容器id #查看cpu 的状态
网络如何连接ES和kibana
十七、图形化管理工具Portaniner安装
Portaniner是Docker的图形化管理工具,类似的工具还有Rancher(CI/CD再用)
下载运行Portaniner镜像并运行,设置本机映射端口为8088
[root@localhost conf]# docker run -d -p 8088:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer
Unable to find image 'portainer/portainer:latest' locally
latest: Pulling from portainer/portainer
94cfa856b2b1: Pull complete
49d59ee0881a: Pull complete
a2300fd28637: Pull complete
Digest: sha256:fb45b43738646048a0a0cc74fcee2865b69efde857e710126084ee5de9be0f3f
Status: Downloaded newer image for portainer/portainer:latest
8c525a0137be22965bd1e3944da622a2c4248f8ad20883f4b3ea4f8a6b11e163
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7789d4505a00 portainer/portainer "/portainer" 6 seconds ago Up 5 seconds 0.0.0.0:8088->9000/tcp quirky_sinoussi
第一次登录设置admin用户的密码
如果是阿里云服务器记得设置安全组,选择连接本地的Docker,整体界面预览如下图:
十八、Docker镜像详解
什么是镜像
镜像是一种轻量级、可执行的独立软件包,用来打包软件运行环境和基于运行环境开发的软件,它包含运行某个软件所需要的所有内容,包括代码,运行时(一个程序在运行或者在被执行的依赖)、库,环境变量和配置文件。
所有的应用,直接打包docker镜像,就可以直接跑起来!
如何得到镜像:
~从远程仓库下载
~朋友拷贝给你
~自己制作一个镜像 DockerFile
Docker镜像加载原理
Docker的镜像实际上由一层一层的文件系统组成,这种层级的文件系统是UnionFS联合文件系统。
十九、镜像原理之分层理解
”[[e[31docker image inspect tomcat
[
{
"Id": "sha256:24207ccc9cce2cb6473cec77e1ec441a7b4e6d0579f4e361899fecfc1019189f",
"RepoTags": [
"tomcat:latest"
],
"RepoDigests": [
"tomcat@sha256:429d5c140820d5eba38042823501d5f4e0f03b282133513e29c22d6358daa7c7"
],
"Parent": "",
"Comment": "",
"Created": "2021-12-08T20:54:01.992194711Z",
"Container": "cee929412d451a4b42b0588a7445254f0f7ac6ea31449b0b53d78d304aeaaaed",
"ContainerConfig": {
"Hostname": "cee929412d45",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"8080/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/tomcat/bin:/usr/local/openjdk-11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"JAVA_HOME=/usr/local/openjdk-11",
"LANG=C.UTF-8",
"JAVA_VERSION=11.0.13",
"CATALINA_HOME=/usr/local/tomcat",
"TOMCAT_NATIVE_LIBDIR=/usr/local/tomcat/native-jni-lib",
"LD_LIBRARY_PATH=/usr/local/tomcat/native-jni-lib",
"GPG_KEYS=A9C5DF4D22E99998D9875A5110C01C5A2F6059E7",
"TOMCAT_MAJOR=10",
"TOMCAT_VERSION=10.0.14",
"TOMCAT_SHA512=c2d2ad5ed17f7284e3aac5415774a8ef35434f14dbd9a87bc7230d8bfdbe9aa1258b97a59fa5c4030e4c973e4d93d29d20e40b6254347dbb66fae269ff4a61a5"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\"catalina.sh\" \"run\"]"
],
"Image": "sha256:3ec35f87b705ec6465c19cfdea07add49c534eb0223513bea902f73c87586411",
"Volumes": null,
"WorkingDir": "/usr/local/tomcat",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"DockerVersion": "20.10.7",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"8080/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/tomcat/bin:/usr/local/openjdk-11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"JAVA_HOME=/usr/local/openjdk-11",
"LANG=C.UTF-8",
"JAVA_VERSION=11.0.13",
"CATALINA_HOME=/usr/local/tomcat",
"TOMCAT_NATIVE_LIBDIR=/usr/local/tomcat/native-jni-lib",
"LD_LIBRARY_PATH=/usr/local/tomcat/native-jni-lib",
"GPG_KEYS=A9C5DF4D22E99998D9875A5110C01C5A2F6059E7",
"TOMCAT_MAJOR=10",
"TOMCAT_VERSION=10.0.14",
"TOMCAT_SHA512=c2d2ad5ed17f7284e3aac5415774a8ef35434f14dbd9a87bc7230d8bfdbe9aa1258b97a59fa5c4030e4c973e4d93d29d20e40b6254347dbb66fae269ff4a61a5"
],
"Cmd": [
"catalina.sh",
"run"
],
"Image": "sha256:3ec35f87b705ec6465c19cfdea07add49c534eb0223513bea902f73c87586411",
"Volumes": null,
"WorkingDir": "/usr/local/tomcat",
"Entrypoint": null,
"OnBuild": null,
"Labels": null
},
"Architecture": "amd64",
"Os": "linux",
"Size": 679640649,
"VirtualSize": 679640649,
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/7b1a9cc507941ca0eabc41349f6ab2bdbc70f48124f8dc9d8fc19bdb2a5daff8/diff:/var/lib/docker/overlay2/cb5fd283d2c0982267ca2b22a07381c79ddc3df979e11dd6608d553a4a0dc589/diff:/var/lib/docker/overlay2/f531c63a994e9aa21035b88a53d82477f5f04d58490ba28f709ad2030880cc7c/diff:/var/lib/docker/overlay2/bc1c4a0f9c7df9318af04d101dd8552e246ed56cec9f48dc3c4d2ca442e9a918/diff:/var/lib/docker/overlay2/11b91da5a2048ee8b25c6ab4c1093817014b2c64929b0cc247fdc25f3cf0f5ce/diff:/var/lib/docker/overlay2/ea6e65f1bf6fa9b43a560c749d754e0038ce579ccdbc448e8120ca4460e44638/diff:/var/lib/docker/overlay2/15940ff38605c64a0458ee60ae6f88b26962a28fe3992c04f89e5382facc2bfe/diff:/var/lib/docker/overlay2/f830168542a1d2de390c50e89b6c1e2650d5805f72c672361a8b9ee38682f4a2/diff:/var/lib/docker/overlay2/d6c5accedd27d8f5824f9e2e26b0dabb14f87f65ec7b9aa3ffff2c2aa15a44ea/diff",
"MergedDir": "/var/lib/docker/overlay2/deb975bf1dfc9af7ae1969cc17a70622c03627b5c9dca70b0f1eef151384931c/merged",
"UpperDir": "/var/lib/docker/overlay2/deb975bf1dfc9af7ae1969cc17a70622c03627b5c9dca70b0f1eef151384931c/diff",
"WorkDir": "/var/lib/docker/overlay2/deb975bf1dfc9af7ae1969cc17a70622c03627b5c9dca70b0f1eef151384931c/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:e2e8c39e0f77177381177ba8c4025421ec2d7e7d3c389a9b3d364f8de560024f",
"sha256:91f7336bbfffdab2b558c738b7eeddec9bd8ace8884b3e7b96f3f5bcb643cafa",
"sha256:d3710de04cb3fb2b3cb30765805f36935699523e75f199975f2b9e827f19e615",
"sha256:3b441d7cb46b419dbd85417c8710660d24df06309e0e29a22d5467514148a86b",
"sha256:a81f1846a0d20b440724b661832139e284b2a67a99a11eeaa07089fd910ebc5e",
"sha256:927f9fcef4cf31ed2cce157134cfc3a26dc64b1a9bab644265c0260d7f749153",
"sha256:5c81f9330d992740680756163ac07b737f2eaa7c9694cf0986452e3b31acc860",
"sha256:f79d29235239b70e8572cb6955f4794b603de8d310a86b57dd2f10c22587886d",
"sha256:516aad68a0ece7a38877acdcf480a44e786822a867675cc4c35529c1e0b32af2",
"sha256:0a62582891d7e1cde80de764437c05704228f2cbd63475d722e92053072db5e4"
]
},
"Metadata": {
"LastTagTime": "0001-01-01T00:00:00Z"
}
}
]
这里指示了分层信息:
"Layers": [
"sha256:e2e8c39e0f77177381177ba8c4025421ec2d7e7d3c389a9b3d364f8de560024f",
"sha256:91f7336bbfffdab2b558c738b7eeddec9bd8ace8884b3e7b96f3f5bcb643cafa",
"sha256:d3710de04cb3fb2b3cb30765805f36935699523e75f199975f2b9e827f19e615",
"sha256:3b441d7cb46b419dbd85417c8710660d24df06309e0e29a22d5467514148a86b",
"sha256:a81f1846a0d20b440724b661832139e284b2a67a99a11eeaa07089fd910ebc5e",
"sha256:927f9fcef4cf31ed2cce157134cfc3a26dc64b1a9bab644265c0260d7f749153",
"sha256:5c81f9330d992740680756163ac07b737f2eaa7c9694cf0986452e3b31acc860",
"sha256:f79d29235239b70e8572cb6955f4794b603de8d310a86b57dd2f10c22587886d",
"sha256:516aad68a0ece7a38877acdcf480a44e786822a867675cc4c35529c1e0b32af2",
"sha256:0a62582891d7e1cde80de764437c05704228f2cbd63475d722e92053072db5e4"
]
总结:下载的镜像层是只读,我们操作的是在容器层(即容器run起来的),基于容器和镜像层还能打包做成镜像。
二十、Commit镜像
如何提交自己的镜像
使用docker commit 命令提交容器成为一个新的版本
#命令和git原理类似
docker commit -m=“提交的描述信息” -a="作者" 容器id 目标镜像名:[TAG]
实战测试:
1、启动一个默认的tomcat
2、发现这个默认的tomcat是没有webapps应用的,镜像的原因,官方镜像默认webapps下面是没有文件的!
3、我自己拷贝进去基本的文件
4、我们操作过的容器通过commit提交为一个镜像,我们以后就使用修改过的镜像即可,这就是我们自己的一个修改过的镜像。
#1.复制文件夹
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker run -it tomcat /bin/bash
root@2a3bf3eaa2e4:/usr/local/tomcat# cd webapps
root@2a3bf3eaa2e4:/usr/local/tomcat/webapps# ls
root@2a3bf3eaa2e4:/usr/local/tomcat/webapps# cd ../
root@2a3bf3eaa2e4:/usr/local/tomcat# cp -r webapps.dist/* webapps
root@2a3bf3eaa2e4:/usr/local/tomcat# cd webapps
root@2a3bf3eaa2e4:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2a3bf3eaa2e4 tomcat "/bin/bash" 4 minutes ago Up 4 minutes 8080/tcp competent_torvalds
7789d4505a00 portainer/portainer "/portainer" 24 hours ago Up 24 hours 0.0.0.0:8088->9000/tcp quirky_sinoussi
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker exec -it 2a3bf3eaa2e4 /bin/bash
root@2a3bf3eaa2e4:/usr/local/tomcat# cd webapps
root@2a3bf3eaa2e4:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager
root@2a3bf3eaa2e4:/usr/local/tomcat/webapps# cd ../
root@2a3bf3eaa2e4:/usr/local/tomcat# read escape sequence
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2a3bf3eaa2e4 tomcat "/bin/bash" 8 minutes ago Up 8 minutes 8080/tcp competent_torvalds
7789d4505a00 portainer/portainer "/portainer" 24 hours ago Up 24 hours 0.0.0.0:8088->9000/tcp quirky_sinoussi
#2.提交镜像作为一个新的镜像
作者 消息 被打包的容器id 镜像打包的名称
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker commit -a="zl" -m="add webapps" dd0211b6f17d mytomcat1.0.0
sha256:7a00fbfe17f22a6b38878ab6ac4d295b713891dbe901b10de8d90d9d79e96fc7
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mytomcat 1.0 f189aac861de 7 seconds ago 653MB
mysql 5.7 f07dfa83b528 6 days ago 448MB
tomcat latest feba8d001e3f 10 days ago 649MB
nginx latest ae2feff98a0c 12 days ago 133MB
centos latest 300e315adb2f 2 weeks ago 209MB
portainer/portainer latest 62771b0b9b09 5 months ago 79.1MB
elasticsearch 7.6.2 f29a1ee41030 9 months ago 791MB
#3.运行容器
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker run -it mytomcat:1.0.0 /bin/bash
root@1645774d4605:/usr/local/tomcat# cd webapps
root@1645774d4605:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager
wz99sm8v95sckz8bd2c4Z ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mytomcat 1.0 f189aac861de 7 seconds ago 653MB
mysql 5.7 f07dfa83b528 6 days ago 448MB
tomcat latest feba8d001e3f 10 days ago 649MB
nginx latest ae2feff98a0c 12 days ago 133MB
centos latest 300e315adb2f 2 weeks ago 209MB
portainer/portainer latest 62771b0b9b09 5 months ago 79.1MB
elasticsearch 7.6.2 f29a1ee41030 9 months ago 791MB
小结:如果你想要保存当前容器的状态,就可以通过commit来提交,获得一个镜像,就好比我们VM的快照。