ITPub博客

首页 > 云计算 > Docker/K8 > 06 Docker容器内部管理的终极指南:掌握核心技巧与最佳实践

06 Docker容器内部管理的终极指南:掌握核心技巧与最佳实践

原创 Docker/K8 作者:shuchaoyang 时间:2024-11-03 17:26:01 0 删除 编辑

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

06 Docker容器内部管理的终极指南:掌握核心技巧与最佳实践

一 docker client 查看容器内部日志

$ docker logs [容器ID]

1.1 全部日志查看

[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 ~] $

1.2 实时进行日志查看

[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'

1.3 实时进行日志查看-显示详细时间戳

[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'

1.4 日志查看-后5行

[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 ~] $

1.5 日志查看-后5行

[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 client 查看容器内部运行的进程

在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 host进入到容器内部 - 分配shell

$ 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 host和容器之间进行数据复制

4.1 从宿主机拷贝文件到容器

$ 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 ~] $

4.2 从容器拷贝文件到宿主机

$ 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 host和容器之间进行数据卷文件共享

docker支持文件共享数据卷 在宿主机中创建⼀个共享目录,将其映射到容器路径中。当前目录在宿主机中可以和容器进行文件共享。 文件共享数据卷必须在创建容器时,指定共享路径。容器创建完毕后无法进行文件共享数据卷创建。

 -v host-dir:container-dir

必须/⼀定使用绝对路径。

5.1 创建共享数据卷

[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 ~] $

5.2 挂载共享数据卷到superman01

[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 ~] $

5.3 挂载共享数据卷到superman02

[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 ~] $

原文链接: https://mp.weixin.qq.com/s?__biz=MzkxNzI1OTE3Mw==&mid=2247492124&idx=1&sn=03d13676360562425515f69354089811&chksm=c141f4d6f6367dc036854a2fb9fd1681048e0a73734f30b47619ab8af836e731f87a384eb8f5#rd

👍 点赞,你的认可是我创作的动力!

⭐️ 收藏,你的青睐是我努力的方向!

✏️ 评论,你的意见是我进步的财富!

图片



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

请登录后发表评论 登录
全部评论
专注于IT系统集成、运维、架构二十年的IT职场老兵,拥有CCNA、OCA、OCP、PGCA、PGCE认证证书,公众号:超哥的IT私房菜,知识星球:超哥的IT私房菜,目前是多家IT厂商的咨询顾问。

注册时间:2021-08-27

  • 博文量
    552
  • 访问量
    303477