docker之容器管理常用命令篇
常用命令如下表:
选项 描述
1、ls - 列出容器
只列出正在运行的容器:
[root@localhost ~]# docker container ls
列出所有容器,包括已停止的:
[root@localhost ~]# docker container ls -a
inspect - 查看一个或多个容器详细信息:
[root@localhost ~]# docker container inspect nginx01
…
2、exec - 在运行容器里执行命令
在容器里执行命令:
进入容器终端:
查看主机名:
查看时间:
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 ~]#
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
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
- 1.
- 2.
- 3.
- 4.
- 5.
- 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" "-"
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
5、port - 列出或指定容器端口映射
查看容器端口映射:
6、top - 显示一个容器运行的进程
查看容器运行进程:
7、stats - 显示容器资源使用统计
查看容器资源利用率:
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
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.

浙公网安备 33010602011771号