docker之容器管理常用命令篇

常用命令如下表:

选项 描述

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

1、ls - 列出容器

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

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

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

在容器里执行命令:

[root@localhost wwwroot]# docker container run -d --name nginx04   -h nginx04 nginx
a44b757591db1ebd9146ec9245b742f755439d83637012f4a27de83e56b52153

进入容器终端:

[root@localhost wwwroot]# docker container exec -it nginx04 bash
root@nginx04:/# hostname
nginx04

查看主机名:

[root@localhost wwwroot]# docker container exec nginx04 hostname
nginx04

查看时间:

[root@localhost wwwroot]# docker container exec nginx04 date
Sun Aug  5 19:10:11 UTC 2018
[root@localhost wwwroot]#

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

拷贝文件到容器:

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

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

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

[root@localhost ~]# docker container inspect -f '{{.NetworkSettings.IPAddress}}' nginx02
172.17.0.3
[root@localhost ~]# docker container inspect -f '{{.NetworkSettings.IPAddress}}' nginx01
172.17.0.2
[root@localhost ~]# docker container inspect -f '{{.NetworkSettings.IPAddress}}' nginx04
172.17.0.6

查看容器nginx02的日志:

[root@localhost ~]#  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
[root@localhost ~]# docker container inspect -f '{{.NetworkSettings.IPAddress}}' nginx02
172.17.0.3
[root@localhost ~]# 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

[root@localhost ~]# 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

[root@localhost ~]# 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 - 列出或指定容器端口映射

查看容器端口映射:

[root@localhost ~]#  docker container port nginx02
80/tcp -> 0.0.0.0:88

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

查看容器运行进程:

[root@localhost ~]# 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 - 显示容器资源使用统计

查看容器资源利用率:

[root@localhost ~]#  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

[root@localhost ~]#  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

[root@localhost ~]# docker export nginx03 >nginx03.tar
[root@localhost ~]# docker export nginx03  -o test01
[root@localhost ~]# du -sh test01 nginx03.tar 
106M    test01
106M    nginx03.tar

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

restart/stop/start/rm -
[root@localhost ~]# docker container stop nginx02
nginx02
[root@localhost ~]# docker container rm nginx02  
nginx02
相关文章
相关标签/搜索