06 Docker容器内部管理的终极指南:掌握核心技巧与最佳实践 一 docker client 查看容器内部日志 1.1 全部日志查看 1.2 实时进行日志查看 1.3 实时进行日志查看-显示详细时间戳 1.4 日志查看-后5行 1.5 日志查看-后5行 二 docker client 查看容器内部运行的进程 三 从docker host进入到容器内部 - 分配shell 四 在docker host和容器之间进行数据复制 4.1 从宿主机拷贝文件到容器 4.2 从容器拷贝文件到宿主机 五 在docker host和容器之间进行数据卷文件共享 5.1 创建共享数据卷 5.2 挂载共享数据卷到superman01 5.3 挂载共享数据卷到superman02
$ docker logs [容器ID]
[superman@docker ~]$ docker logs 393e19a0d0a9
示例:
[superman@docker ~] $ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b4ccd86df7e3 httpd:2.4 "httpd-foreground" 39 seconds ago Up 38 seconds 0.0.0.0:32768->80/tcp, :::32768->80/tcp jolly_thompson
[superman@docker ~] $
[superman@docker ~] $ docker logs b4ccd86df7e3
AH00558: httpd: Could not reliably determine the server 's fully qualified domain name, using 172.17.0.2. Set the 'ServerName ' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server 's fully qualified domain name, using 172.17.0.2. Set the 'ServerName ' directive globally to suppress this message
[Sat Jun 08 13:27:42.207112 2024] [mpm_event:notice] [pid 1:tid 140576319216960] AH00489: Apache/2.4.52 (Unix) configured -- resuming normal operations
[Sat Jun 08 13:27:42.208052 2024] [core:notice] [pid 1:tid 140576319216960] AH00094: Command line: 'httpd -D FOREGROUND'
[superman@docker ~] $
[superman@docker ~]$ docker logs -f b4ccd86df7e3
示例:
[superman@docker ~] $ docker logs -f b4ccd86df7e3
AH00558: httpd: Could not reliably determine the server 's fully qualified domain name, using 172.17.0.2. Set the 'ServerName ' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server 's fully qualified domain name, using 172.17.0.2. Set the 'ServerName ' directive globally to suppress this message
[Sat Jun 08 13:27:42.207112 2024] [mpm_event:notice] [pid 1:tid 140576319216960] AH00489: Apache/2.4.52 (Unix) configured -- resuming normal operations
[Sat Jun 08 13:27:42.208052 2024] [core:notice] [pid 1:tid 140576319216960] AH00094: Command line: 'httpd -D FOREGROUND'
[superman@docker ~]$ docker logs -tf b4ccd86df7e3
示例:
[superman@docker ~] $ docker logs -tf b4ccd86df7e3
2024 -06-08T13:27:42.202842014Z AH00558: httpd: Could not reliably determine the server 's fully qualified domain name, using 172.17.0.2. Set the 'ServerName ' directive globally to suppress this message
2024 -06-08T13:27:42.213102371Z AH00558: httpd: Could not reliably determine the server 's fully qualified domain name, using 172.17.0.2. Set the 'ServerName ' directive globally to suppress this message
2024 -06-08T13:27:42.213122427Z [Sat Jun 08 13:27:42.207112 2024] [mpm_event:notice] [pid 1:tid 140576319216960] AH00489: Apache/2.4.52 (Unix) configured -- resuming normal operations
2024 -06-08T13:27:42.213125676Z [Sat Jun 08 13:27:42.208052 2024] [core:notice] [pid 1:tid 140576319216960] AH00094: Command line: 'httpd -D FOREGROUND'
[superman@docker ~]$ docker logs --tail 5 b4ccd86df7e3
示例:
[superman@docker ~] $ docker logs --tail 5 b4ccd86df7e3
AH00558: httpd: Could not reliably determine the server 's fully qualified domain name, using 172.17.0.2. Set the 'ServerName ' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server 's fully qualified domain name, using 172.17.0.2. Set the 'ServerName ' directive globally to suppress this message
[Sat Jun 08 13:27:42.207112 2024] [mpm_event:notice] [pid 1:tid 140576319216960] AH00489: Apache/2.4.52 (Unix) configured -- resuming normal operations
[Sat Jun 08 13:27:42.208052 2024] [core:notice] [pid 1:tid 140576319216960] AH00094: Command line: 'httpd -D FOREGROUND'
[superman@docker ~] $
[superman@docker ~]$ docker logs -n 5 b4ccd86df7e3
示例:
[superman@docker ~] $ docker logs -n 5 b4ccd86df7e3
AH00558: httpd: Could not reliably determine the server 's fully qualified domain name, using 172.17.0.2. Set the 'ServerName ' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server 's fully qualified domain name, using 172.17.0.2. Set the 'ServerName ' directive globally to suppress this message
[Sat Jun 08 13:27:42.207112 2024] [mpm_event:notice] [pid 1:tid 140576319216960] AH00489: Apache/2.4.52 (Unix) configured -- resuming normal operations
[Sat Jun 08 13:27:42.208052 2024] [core:notice] [pid 1:tid 140576319216960] AH00094: Command line: 'httpd -D FOREGROUND'
[superman@docker ~] $
在docker容器中,通常情况下⼀个容器只运行⼀个服务 ,⼀个服务可能能对1/多个进程。
在docker容器后台运行过程中,运行了哪些进程。
[superman@docker ~]$ docker top b4ccd86df7e3
示例:
[superman@docker ~] $ docker top b4ccd86df7e3
UID PID PPID C STIME TTY TIME CMD
root 23672 23651 0 21:27 ? 00:00:00 httpd -DFOREGROUND
33 23699 23672 0 21:27 ? 00:00:00 httpd -DFOREGROUND
33 23700 23672 0 21:27 ? 00:00:00 httpd -DFOREGROUND
33 23701 23672 0 21:27 ? 00:00:00 httpd -DFOREGROUND
[superman@docker ~] $
$ docker exec -it <容器ID> <shell>
[superman@docker ~]$ docker exec -it b4ccd86df7e3 /bin/bash
示例:
[superman@docker ~] $ docker exec -it b4ccd86df7e3 /bin/bash
root@b4ccd86df7e3:/usr/local/apache2 #
[superman@docker ~]$ docker exec -it b4ccd86df7e3 bash
示例:
[superman@docker ~] $ docker exec -it b4ccd86df7e3 bash
root@b4ccd86df7e3:/usr/local/apache2 #
root@b4ccd86df7e3:/usr/local/apache2 # exit
exit
[superman@docker ~] $
exit #退出容器,回到docker宿主机; docker容器内部无法执行完整所有linux命令,系统命令已经为精简系统。
$ docker cp [宿主机文件路径] [容器ID:容器路径]
[superman@docker ~]$ docker cp ./index.html b4ccd86df7e3:/usr/local/apache2/htdocs
示例:
[superman@docker ~] $ cat index.html
<html lang =en>
<head>
<meta charset =UTF-8>
<title>Welcome to Apache</title>
</head>
<body>
</body>
</html>
[superman@docker ~] $
[superman@docker ~] $ curl 192.168.0.121:32768
<html><body><h1>It works!</h1></body></html>
[superman@docker ~] $
[superman@docker ~] $ docker cp ./index.html b4ccd86df7e3:/usr/local/apache2/htdocs
Successfully copied 2.05kB to b4ccd86df7e3:/usr/local/apache2/htdocs
[superman@docker ~] $
[superman@docker ~] $ curl 192.168.0.121:32768
<html lang =en>
<head>
<meta charset =UTF-8>
<title>Welcome to Apache</title>
</head>
<body>
</body>
</html>
[superman@docker ~] $
$ docker cp [容器ID:容器路径] [宿主机文件路径]
[superman@docker ~]$ docker cp b4ccd86df7e3:/usr/local/apache2/htdocs ./
示例:
[superman@docker ~] $ ls
index.html index.html.1
[superman@docker ~] $
[superman@docker ~] $ rm -rf *
[superman@docker ~] $
[superman@docker ~] $ ls
[superman@docker ~] $
[superman@docker ~] $ docker cp b4ccd86df7e3:/usr/local/apache2/htdocs/index.html ./
Successfully copied 2.05kB to /home/superman/./
[superman@docker ~] $
[superman@docker ~] $ ls
index.html
[superman@docker ~] $
docker支持文件共享数据卷 在宿主机中创建⼀个共享目录,将其映射到容器路径中。当前目录在宿主机中可以和容器进行文件共享。 文件共享数据卷必须在创建容器时,指定共享路径。容器创建完毕后无法进行文件共享数据卷创建。
-v host-dir:container-dir
必须/⼀定使用绝对路径。
[superman@docker ~]$ mkdir superman_share
示例:
[superman@docker ~] $ mkdir superman_share
[superman@docker ~] $
[superman@docker ~] $ cp index.html ./superman_share/
[superman@docker ~] $
[superman@docker ~] $ ls ./superman_share/
index.html
[superman@docker ~] $
[superman@docker ~]$ docker run -d -p 10001:80 --name superman01 -v /home/superman/superman_share:/usr/local/apache2/htdocs httpd:2.4
示例:
[superman@docker ~] $ docker run -d -p 10001:80 --name superman01 -v /home/superman/superman_share:/usr/local/apache2/htdocs httpd:2.4
158b2ca4350bc43f2f892971f9b58fb36bc4d9ae5c804bd2af63cf7e96ca2733
[superman@docker ~] $
[superman@docker ~] $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
158b2ca4350b httpd:2.4 "httpd-foreground" 7 seconds ago Up 6 seconds 0.0.0.0:10001->80/tcp, :::10001->80/tcp superman01
b4ccd86df7e3 httpd:2.4 "httpd-foreground" 34 minutes ago Up 34 minutes 0.0.0.0:32768->80/tcp, :::32768->80/tcp jolly_thompson
[superman@docker ~] $
[superman@docker ~] $ curl 192.168.0.121:10001
<html lang =en>
<head>
<meta charset =UTF-8>
<title>Welcome to Apache</title>
</head>
<body>
</body>
</html>
[superman@docker ~] $
[superman@docker ~]$ docker run -d -p 10002:80 --name superman02 -v /home/superman/superman_share:/usr/local/apache2/htdocs httpd:2.4
示例:
[superman@docker ~] $ docker run -d -p 10002:80 --name superman02 -v /home/superman/superman_share:/usr/local/apache2/htdocs httpd:2.4
aa491a7ac7ab9feacae5ba4bf8275f6646ecc2d85d12c4009db10ecc8ca82af4
[superman@docker ~] $
[superman@docker ~] $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aa491a7ac7ab httpd:2.4 "httpd-foreground" 5 seconds ago Up 4 seconds 0.0.0.0:10002->80/tcp, :::10002->80/tcp superman02
158b2ca4350b httpd:2.4 "httpd-foreground" About a minute ago Up About a minute 0.0.0.0:10001->80/tcp, :::10001->80/tcp superman01
b4ccd86df7e3 httpd:2.4 "httpd-foreground" 36 minutes ago Up 35 minutes 0.0.0.0:32768->80/tcp, :::32768->80/tcp jolly_thompson
[superman@docker ~] $
[superman@docker ~] $ curl 192.168.0.121:10002
<html lang =en>
<head>
<meta charset =UTF-8>
<title>Welcome to Apache</title>
</head>
<body>
</body>
</html>
[superman@docker ~] $
👍 点赞,你的认可是我创作的动力!
⭐️ 收藏,你的青睐是我努力的方向!
✏️ 评论,你的意见是我进步的财富!

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/291614/viewspace-3045028/ ,如需转载,请注明出处,否则将追究法律责任。