docker之容器管理常用命令篇

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker之容器管理常用命令篇相关的知识,希望对你有一定的参考价值。

常用命令如下表:

选项 描述

ls  列出容器
inspect 查看一个或多个容器详细信息
exec    在运行容器中执行命令
commit  创建一个新镜像来自一个容器
cp  拷贝文件/文件夹到一个容器
logs    获取一个容器日志
port    列出或指定容器端口映射
top 显示一个容器运行的进程
stats   显示容器资源使用统计
stop/start  停止/启动一个或多个容器
rm  删除一个或多个容器

1、ls - 列出容器

只列出正在运行的容器:
[[email protected] ~]# docker container ls
列出所有容器,包括已停止的:
[[email protected] ~]# docker container ls -a

inspect - 查看一个或多个容器详细信息:
[[email protected] ~]# docker container inspect nginx01
......

2、exec - 在运行容器里执行命令

在容器里执行命令:

[[email protected] wwwroot]# docker container run -d --name nginx04   -h nginx04 nginx
a44b757591db1ebd9146ec9245b742f755439d83637012f4a27de83e56b52153

进入容器终端:

[[email protected] wwwroot]# docker container exec -it nginx04 bash
[email protected]:/# hostname
nginx04

查看主机名:

[[email protected] wwwroot]# docker container exec nginx04 hostname
nginx04

查看时间:

[[email protected] wwwroot]# docker container exec nginx04 date
Sun Aug  5 19:10:11 UTC 2018
[[email protected] wwwroot]# 

3、cp - 拷贝文件或目录到一个容器

拷贝文件到容器:

[[email protected] ~]# echo 123321 >> 123.txt
[[email protected] ~]# cat 123.txt 
123321
[[email protected] ~]# docker container cp 123.txt nginx04:/usr/share/nginx/html
[[email protected] ~]# docker container exec -it nginx04 cat /usr/share/nginx/html/123.txt
123321
[[email protected] ~]# docker container exec -it nginx04 ls /usr/share/nginx/html/123.txt
/usr/share/nginx/html/123.txt
[[email protected] ~]# cd /usr/share/nginx/html
-bash: cd: /usr/share/nginx/html: No such file or directory
[[email protected] ~]# 

4、logs - 获取一个容器日志

查看容器日志:
查看容器的IP地址

[[email protected] ~]# docker container inspect -f ‘{{.NetworkSettings.IPAddress}}‘ nginx02
172.17.0.3
[[email protected] ~]# docker container inspect -f ‘{{.NetworkSettings.IPAddress}}‘ nginx01
172.17.0.2
[[email protected] ~]# docker container inspect -f ‘{{.NetworkSettings.IPAddress}}‘ nginx04
172.17.0.6

查看容器nginx02的日志:


[[email protected] ~]#  docker container ls|grep nginx02
a5e10362acc3        nginx               "nginx -g ‘daemon of…"   25 hours ago        Up 2 hours          0.0.0.0:88->80/tcp   nginx02
[[email protected] ~]# docker container inspect -f ‘{{.NetworkSettings.IPAddress}}‘ nginx02
172.17.0.3
[[email protected] ~]# curl -I http://192.168.1.101:88
HTTP/1.1 200 OK
Server: nginx/1.15.2
Date: Sun, 05 Aug 2018 19:26:30 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 24 Jul 2018 13:02:29 GMT
Connection: keep-alive
ETag: "5b572365-264"
Accept-Ranges: bytes

[[email protected] ~]# curl -I 172.17.0.3
HTTP/1.1 200 OK
Server: nginx/1.15.2
Date: Sun, 05 Aug 2018 19:26:41 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 24 Jul 2018 13:02:29 GMT
Connection: keep-alive
ETag: "5b572365-264"
Accept-Ranges: bytes

[[email protected] ~]# docker container logs -f nginx02
192.168.1.1 - - [05/Aug/2018:19:25:38 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko" "-"
192.168.1.1 - - [05/Aug/2018:19:25:38 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko" "-"
192.168.1.101 - - [05/Aug/2018:19:26:30 +0000] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.29.0" "-"
172.17.0.1 - - [05/Aug/2018:19:26:41 +0000] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.29.0" "-"

5、port - 列出或指定容器端口映射

查看容器端口映射:

[[email protected] ~]#  docker container port nginx02
80/tcp -> 0.0.0.0:88

6、top - 显示一个容器运行的进程

查看容器运行进程:

[[email protected] ~]# docker container top nginx02
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                3351                3335                0                   00:56               ?                   00:00:00            nginx: master process nginx -g daemon off;
101                 3391                3351                0                   00:56               ?                   00:00:00            nginx: worker process

7、stats - 显示容器资源使用统计

查看容器资源利用率:

[[email protected] ~]#  docker container stats --no-stream nginx02
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
a5e10362acc3        nginx02             0.00%               1.383MiB / 1.566GiB   0.09%               26kB / 18.1kB       24.6kB / 4.1kB      0

8、export - 导出容器文件系统到tar归档文件

docker export name/id > name.tar

[[email protected] ~]#  docker export -h
Flag shorthand -h has been deprecated, please use --help

Usage:  docker export [OPTIONS] CONTAINER

Export a container‘s filesystem as a tar archive

Options:
  -o, --output string   Write to a file, instead of STDOUT

[[email protected] ~]# docker export nginx03 >nginx03.tar
[[email protected] ~]# docker export nginx03  -o test01
[[email protected] ~]# du -sh test01 nginx03.tar 
106M    test01
106M    nginx03.tar

9、停止/启动/删除一个容器

restart/stop/start/rm -
[[email protected] ~]# docker container stop nginx02
nginx02
[[email protected] ~]# docker container rm nginx02  
nginx02

以上是关于docker之容器管理常用命令篇的主要内容,如果未能解决你的问题,请参考以下文章

容器技术之Docker常用命令说明

容器技术之Docker常用命令说明

测试开发之系统篇-Docker常用操作

测试开发之系统篇-Docker常用操作

Docker之进入容器

docker入门之二:docker常用命令