1. 程式人生 > >docker之容器管理常用命令篇

docker之容器管理常用命令篇

多個 filesyste 啟動 acc code ppi -- ike ces

常用命令如下表:

選項 描述

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

docker之容器管理常用命令篇