视频链接:https://www.bilibili.com/video/BV1og4y1q7M4?p=1
docker logs
查看日志要查看日志的前提是 容器中有进程运行过
docker logs -f -t --tail n 容器ID #此处 n 表示首次打印要查看的日志数量 该命令在打印了 n 条日志后 只要还有日志产生 仍会 继续打印日志 -t # 显示时间戳 -f # 对显示的内容格式化(t、f 参数 可以一起写作 -tf 即可 先后次序不影响输出效果) --tail number #首次输出的 日志条数 docker logs -tf 容器ID # 输出当前容器所有日志 #示例 # 此处 后台启动 centOS 镜像 作为容器 并进入 /bin/sh 运行一段shell脚本 每隔一秒输出 一个 hello 字符串(确保有进程在运行,一边查看日志) docker run -d centos /bin/sh -c "while true;do echo hello;sleep 1;done" docker logs -f -t --tail 10 刚刚运行的容器ID # 这里1的输出效果是 先输出10条日志,然后每隔一秒再输出一条日志(由于是 while true循环,只能ctrl+C结束该命令) ———————————————————————————————————————————————————————————————————————————— #实际执行结果: C:\Users\z>docker run -d centos /bin/sh -c "i=0;while i<10;do echo hello;i=i+1;sleep 1;done" 1ba631a037e8087c91ca541e604c12cf5f17d4e5f102b431bd49f50da0beea07 C:\Users\z>docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES C:\Users\z>docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1ba631a037e8 centos "/bin/sh -c 'i=0;whi…" 14 seconds ago Exited (0) 12 seconds ago distracted_pare C:\Users\z>docker logs -ft 1ba631a037e8 2021-11-14T04:53:45.756136200Z /bin/sh: 10: No such file or directory # 由于写的shell脚本不正确,导致后台容器没有运行进程被杀死了,最后只输出了一个错误信息日志(按照设想如果shell脚本正确,会输出10次hello 然后容器停止运行) #还是执行一下while true 查看结果吧 C:\Users\z>docker run -d centos /bin/sh -c "while true;do echo hello;sleep 1;done" f538ba990f583e68a58a62453fe989f94045d95531881ee2859e51693aff98d4 C:\Users\z>docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f538ba990f58 centos "/bin/sh -c 'while t…" 7 seconds ago Up 6 seconds recursing_tu C:\Users\z>docker logs -ft --tail 10 f538ba990f58 2021-11-14T04:55:56.337365700Z hello 2021-11-14T04:55:57.339553800Z hello 2021-11-14T04:55:58.341160600Z hello 2021-11-14T04:55:59.343652000Z hello 2021-11-14T04:56:00.345573400Z hello 2021-11-14T04:56:01.347140900Z hello 2021-11-14T04:56:02.348679700Z hello 2021-11-14T04:56:03.350375700Z hello 2021-11-14T04:56:04.352198600Z hello 2021-11-14T04:56:05.353991500Z hello 2021-11-14T04:56:06.356389100Z hello 2021-11-14T04:56:07.358056900Z hello 2021-11-14T04:56:08.359753000Z hello 2021-11-14T04:56:09.361280300Z hello 2021-11-14T04:56:10.363219600Z hello 2021-11-14T04:56:11.364954700Z hello 2021-11-14T04:56:12.366588400Z hello ^C C:\Users\z>
docker inspect
查看元数据
运行示例:
C:\Users\z>docker inspect --help Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID...] Return low-level information on Docker objects Options: -f, --format string Format the output using the given Go template -s, --size Display total file sizes if the type is container --type string Return JSON for specified type C:\Users\z>docker inspect 57134526c81c [ { "Id": "57134526c81c45ba5b9203e1717728d2e36d611cb3d031371cc36f3d0c736c1d", "Created": "2021-11-13T16:11:31.5861229Z", "Path": "/bin/sh", "Args": [ "-c", "while true;do echo zhangzhangzhang;sleep 1;done" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 2534, "ExitCode": 0, "Error": "", "StartedAt": "2021-11-13T16:11:32.0996725Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "——————————————————————————此处省略 100 lines——————————————————————————————" "Cmd": [ "/bin/sh", "-c", "while true;do echo zhangzhangzhang;sleep 1;done" ] } } } } ] C:\Users\z>
进入正在运行的容器
docker exec 容器ID # 进入容器后重新开启一个终端,可以在里面进行一些配置和操作 docker attach 容器ID # 直接进入容器正在执行的终端,不会启动新的终端
运行示例:
C:\Users\z>docker exec --help Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...] Run a command in a running container Options: -d, --detach Detached mode: run command in the background --detach-keys string Override the key sequence for detaching a container -e, --env list Set environment variables --env-file list Read in a file of environment variables -i, --interactive Keep STDIN open even if not attached --privileged Give extended privileges to the command -t, --tty Allocate a pseudo-TTY -u, --user string Username or UID (format: <name|uid>[:<group|gid>]) -w, --workdir string Working directory inside the container C:\Users\z>docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 57134526c81c centos "/bin/sh -c 'while t…" 14 minutes ago Up 14 minutes centos03 1578c133ad4f centos "/bin/bash" 21 minutes ago Up 21 minutes centos01 C:\Users\z>docker exec -it 57134526c81c "docker exec" requires at least 2 arguments. See 'docker exec --help'. Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...] Run a command in a running container C:\Users\z>docker exec -it 57134526c81c /bin/bash # 以交互方式 进入 centos 容器的 bash终端 [root@57134526c81c /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@57134526c81c /]# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 16:11 ? 00:00:00 /bin/sh -c while true;do echo zhangzhangzhang;sleep 1;done root 921 0 0 16:26 pts/0 00:00:00 /bin/bash root 958 1 0 16:27 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1 root 959 921 0 16:27 pts/0 00:00:00 ps -ef [root@57134526c81c /]# C:\Users\z>docker attach 1578c133ad4f [root@1578c133ad4f /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@1578c133ad4f /]# docker bash: docker: command not found [root@1578c133ad4f /]# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 16:04 pts/0 00:00:00 /bin/bash root 18 1 0 16:33 pts/0 00:00:00 ps -ef [root@1578c133ad4f /]# C:\Users\z> # 如果以docker attach 的方式进入 上面正在运行 while true 进程的 容器 就会看到这样的输出,而且 由于是 while true 只能以关闭 终端的方式强制退出该容器 C:\Users\z>docker attach f538ba990f58 hello hello hello hello
docker cp
将容器中的问卷拷贝到宿主机
C:\Users\z>docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1578c133ad4f centos "/bin/bash" 31 minutes ago Up 30 minutes centos01 C:\Users\z>docker attach 1578c133ad4f [root@1578c133ad4f /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@1578c133ad4f /]# cd usr [root@1578c133ad4f usr]# touch helloDocker.txt [root@1578c133ad4f usr]# ls bin games helloDocker.txt include lib lib64 libexec local sbin share src tmp [root@1578c133ad4f usr]# exit exit C:\Users\z>docker cp 1578c133ad4f:usr/helloDocker.txt D:\Users\z\Downloads C:\Users\z>
拷贝是一个手动过程,后面的学习中使用 -v 卷的技术 可以实现将容器的文件夹同步到宿主机文件夹