docekr 常用命令 1:ls 列出容器 $ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE jdk 8 fec5236a803b 48 seconds ago 643MB nginx latest 881bd08c0b08 6 days ago 109MB centos 7 1e1148e4cc2c 3 months ago 202MB java 8 d23bdf5b1b1b 2 years ago 643MB root@bogon: ~ 19:41:10 $ 2:inspect 查看一个或多个容器详细信息 root@bogon: ~ 19:42:19 $ docker inspect 42d6729d3d02 root@bogon: ~ 20:08:12 #获取正在运行的容器IP $ docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 025f1f92a6eb 172.17.0.2 3:exec 进入容器可写层(在容器中执行命令) root@bogon: ~ 19:43:09 $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 42d6729d3d02 nginx:latest "nginx -g 'daemon of…" 2 hours ago Up 2 hours 80/tcp nginx01 e125d0e27a24 java:8 "/bin/bash" 2 hours ago Up 2 hours 0.0.0.0:8080->8080/tcp test11 e2b78fc1b028 java:8 "/bin/bash" 2 hours ago Up 2 hours test10 025f1f92a6eb nginx:latest "nginx -g 'daemon of…" 3 hours ago Up 3 hours 0.0.0.0:80->80/tcp nginx1.0 root@bogon: ~ 19:43:14 $ docker exec -it 42d6729d3d02 bash 4:commit 在运行的容器制作成镜像显示在docker image ls中 $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 42d6729d3d02 nginx:latest "nginx -g 'daemon of…" 2 hours ago Up 2 hours 80/tcp nginx01 e125d0e27a24 java:8 "/bin/bash" 2 hours ago Up 2 hours 0.0.0.0:8080->8080/tcp test11 e2b78fc1b028 java:8 "/bin/bash" 2 hours ago Up 2 hours test10 025f1f92a6eb nginx:latest "nginx -g 'daemon of…" 3 hours ago Up 3 hours 0.0.0.0:80->80/tcp nginx1.0 root@bogon: ~ 19:44:20 $ docker commit -a "hctest" -m "this is test" e125d0e27a24 jdk:8 5:cp 拷贝文件/文件夹到一个容器 root@bogon: ~ 19:46:01 $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 42d6729d3d02 nginx:latest "nginx -g 'daemon of…" 2 hours ago Up 2 hours 80/tcp nginx01 e125d0e27a24 java:8 "/bin/bash" 2 hours ago Up 2 hours 0.0.0.0:8080->8080/tcp test11 e2b78fc1b028 java:8 "/bin/bash" 2 hours ago Up 2 hours test10 025f1f92a6eb nginx:latest "nginx -g 'daemon of…" 3 hours ago Up 3 hours 0.0.0.0:80->80/tcp nginx1.0 root@bogon: ~ 19:46:04 $ ls anaconda-ks.cfg ini.sh nginx.tar root@bogon: ~ 19:46:08 $ docker cp /root/nginx.tar 42d6729d3d02:/root root@bogon: ~ 19:46:35 $ docker exec -it 42d6729d3d02 bash root@42d6729d3d02:/# cd /root root@42d6729d3d02:~# ls nginx.tar root@42d6729d3d02:~# cd /tmp root@42d6729d3d02:/tmp# ls root@42d6729d3d02:/tmp# mkdir cptest root@42d6729d3d02:/tmp# exit exit root@bogon: ~ 19:47:15 $ docker cp 42d6729d3d02:/tmp/cptest /root root@bogon: ~ 19:47:29 $ ls anaconda-ks.cfg cptest ini.sh nginx.tar root@bogon: ~ 19:47:30 $ 6:logs 获取容器日志 root@bogon: ~ 19:49:07 $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 42d6729d3d02 nginx:latest "nginx -g 'daemon of…" 2 hours ago Up 2 hours 80/tcp nginx01 e125d0e27a24 java:8 "/bin/bash" 2 hours ago Up 2 hours 0.0.0.0:8080->8080/tcp test11 e2b78fc1b028 java:8 "/bin/bash" 2 hours ago Up 2 hours test10 025f1f92a6eb nginx:latest "nginx -g 'daemon of…" 3 hours ago Up 3 hours 0.0.0.0:80->80/tcp nginx1.0 root@bogon: ~ 19:49:10 $ docker logs -f 025f1f92a6eb 192.168.1.7 - - [11/Mar/2019:11:49:30 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-" 2019/03/11 11:49:31 [error] 6#6: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.1.7, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.1.199", referrer: "http://192.168.1.199/" 192.168.1.7 - - [11/Mar/2019:11:49:31 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.1.199/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-" ^C root@bogon: ~ 19:50:09 $ OPTIONS说明: -f : 跟踪日志输出 docker logs -f 025f1f92a6eb --since :显示某个开始时间的所有日志 docker logs --since="2019-03-12" --tail=10 025f1f92a6eb -t : 显示时间戳 docker logs -t 025f1f92a6eb --tail :仅列出最新N条容器日志 root@bogon: ~ 19:52:13 $ docker logs --tail 1 025f1f92a6eb 192.168.1.7 - - [11/Mar/2019:11:49:31 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.1.199/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-" root@bogon: ~ 19:52:19 $ 7 port 列出或者指定容器端口映射 root@bogon: ~ 19:57:59 $ docker port 025f1f92a6eb 80/tcp -> 0.0.0.0:80 root@bogon: ~ 19:58:04 $ 8: top 显示一个容器运行的进程 root@bogon: ~ 19:58:46 $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8f58621ab588 nginx:latest "nginx -g 'daemon of…" 4 minutes ago Up 4 minutes 0.0.0.0:8888->80/tcp test99 42d6729d3d02 nginx:latest "nginx -g 'daemon of…" 2 hours ago Up 2 hours 80/tcp nginx01 e125d0e27a24 java:8 "/bin/bash" 2 hours ago Up 2 hours 0.0.0.0:8080->8080/tcp test11 e2b78fc1b028 java:8 "/bin/bash" 3 hours ago Up 3 hours test10 025f1f92a6eb nginx:latest "nginx -g 'daemon of…" 3 hours ago Up 3 hours 0.0.0.0:80->80/tcp nginx1.0 root@bogon: ~ 19:58:48 $ docker top 025f1f92a6eb UID PID PPID C STIME TTY TIME CMD root 30271 30255 0 16:57 pts/0 00:00:00 nginx: master process nginx -g daemon off; 101 30300 30271 0 16:57 pts/0 00:00:00 nginx: worker process root@bogon: ~ 19:58:55 $ 8:stats 显示容器资源使用统计 $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8f58621ab588 nginx:latest "nginx -g 'daemon of…" 5 minutes ago Up 5 minutes 0.0.0.0:8888->80/tcp test99 42d6729d3d02 nginx:latest "nginx -g 'daemon of…" 2 hours ago Up 2 hours 80/tcp nginx01 e125d0e27a24 java:8 "/bin/bash" 2 hours ago Up 2 hours 0.0.0.0:8080->8080/tcp test11 e2b78fc1b028 java:8 "/bin/bash" 3 hours ago Up 3 hours test10 025f1f92a6eb nginx:latest "nginx -g 'daemon of…" 3 hours ago Up 3 hours 0.0.0.0:80->80/tcp nginx1.0 root@bogon: ~ 19:59:32 $ docker stats 025f1f92a6eb CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 025f1f92a6eb nginx1.0 0.00% 1.379MiB / 3.701GiB 0.04% 2.5kB / 2.4kB 0B / 0B 2 CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 025f1f92a6eb nginx1.0 0.00% 1.379MiB / 3.701GiB 0.04% 2.5kB / 2.4kB 0B / 0B 2 CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 025f1f92a6eb nginx1.0 0.00% 1.379MiB / 3.701GiB 0.04% 2.5kB / 2.4kB 0B / 0B 2 CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 025f1f92a6eb nginx1.0 0.00% 1.379MiB / 3.701GiB 0.04% 2.5kB / 2.4kB 0B / 0B 2 CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 025f1f92a6eb nginx1.0 0.00% 1.379MiB / 3.701GiB 0.04% 2.5kB / 2.4kB 0B / 0B 2 ^C root@bogon: ~ 19:59:41 $ 9:stop/start 停止启动一个容器或多个 root@bogon: ~ 20:00:19 $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8f58621ab588 nginx:latest "nginx -g 'daemon of…" 5 minutes ago Up 5 minutes 0.0.0.0:8888->80/tcp test99 42d6729d3d02 nginx:latest "nginx -g 'daemon of…" 2 hours ago Up 2 hours 80/tcp nginx01 e125d0e27a24 java:8 "/bin/bash" 2 hours ago Up 2 hours 0.0.0.0:8080->8080/tcp test11 e2b78fc1b028 java:8 "/bin/bash" 3 hours ago Up 3 hours test10 025f1f92a6eb nginx:latest "nginx -g 'daemon of…" 3 hours ago Up 3 hours 0.0.0.0:80->80/tcp nginx1.0 root@bogon: ~ 20:00:20 $ docker stop 42d6729d3d02 42d6729d3d02 root@bogon: ~ 20:00:37 $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8f58621ab588 nginx:latest "nginx -g 'daemon of…" 6 minutes ago Up 6 minutes 0.0.0.0:8888->80/tcp test99 42d6729d3d02 nginx:latest "nginx -g 'daemon of…" 2 hours ago Exited (0) 3 seconds ago nginx01 e125d0e27a24 java:8 "/bin/bash" 2 hours ago Up 2 hours 0.0.0.0:8080->8080/tcp test11 e2b78fc1b028 java:8 "/bin/bash" 3 hours ago Up 3 hours test10 025f1f92a6eb nginx:latest "nginx -g 'daemon of…" 3 hours ago Up 3 hours 0.0.0.0:80->80/tcp nginx1.0 root@bogon: ~ 20:00:41 $ docker start 42d6729d3d02 42d6729d3d02 root@bogon: ~ 20:00:50 $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8f58621ab588 nginx:latest "nginx -g 'daemon of…" 6 minutes ago Up 6 minutes 0.0.0.0:8888->80/tcp test99 42d6729d3d02 nginx:latest "nginx -g 'daemon of…" 2 hours ago Up 3 seconds 80/tcp nginx01 e125d0e27a24 java:8 "/bin/bash" 2 hours ago Up 2 hours 0.0.0.0:8080->8080/tcp test11 e2b78fc1b028 java:8 "/bin/bash" 3 hours ago Up 3 hours test10 025f1f92a6eb nginx:latest "nginx -g 'daemon of…" 3 hours ago Up 3 hours 0.0.0.0:80->80/tcp nginx1.0 root@bogon: ~ 20:00:53 $ 10:rm 删除一个或者多个容器(先停止在删除) root@bogon: ~ 20:01:17 $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8f58621ab588 nginx:latest "nginx -g 'daemon of…" 6 minutes ago Up 6 minutes 0.0.0.0:8888->80/tcp test99 42d6729d3d02 nginx:latest "nginx -g 'daemon of…" 2 hours ago Up 29 seconds 80/tcp nginx01 e125d0e27a24 java:8 "/bin/bash" 3 hours ago Up 3 hours 0.0.0.0:8080->8080/tcp test11 e2b78fc1b028 java:8 "/bin/bash" 3 hours ago Up 3 hours test10 025f1f92a6eb nginx:latest "nginx -g 'daemon of…" 3 hours ago Up 3 hours 0.0.0.0:80->80/tcp nginx1.0 root@bogon: ~ 20:01:19 $ docker rm 8f58621ab588 Error response from daemon: You cannot remove a running container 8f58621ab5880a360ea0714a73f0d700b5a9616b1308f0c681ac1d8af91c8437. Stop the container before attempting removal or force remove root@bogon: ~ 20:01:24 $ docker stop 8f58621ab588 8f58621ab588 root@bogon: ~ 20:01:28 $ docker rm 8f58621ab588 8f58621ab588 root@bogon: ~ 20:01:32 $ 11 ps 列出容器 root@bogon: ~ 20:03:37 $ docker ps -a #列出所有容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 42d6729d3d02 nginx:latest "nginx -g 'daemon of…" 2 hours ago Up 2 minutes 80/tcp nginx01 e125d0e27a24 java:8 "/bin/bash" 3 hours ago Up 3 hours 0.0.0.0:8080->8080/tcp test11 e2b78fc1b028 java:8 "/bin/bash" 3 hours ago Up 3 hours test10 025f1f92a6eb nginx:latest "nginx -g 'daemon of…" 3 hours ago Up 3 hours 0.0.0.0:80->80/tcp nginx1.0 root@bogon: ~ 20:03:47 $ docker ps -a -q #只列出容器ID 42d6729d3d02 e125d0e27a24 e2b78fc1b028 025f1f92a6eb root@bogon: ~ 20:03:53 $ docker ps -n 3 #列出最近创建的3个容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 42d6729d3d02 nginx:latest "nginx -g 'daemon of…" 2 hours ago Up 3 minutes 80/tcp nginx01 e125d0e27a24 java:8 "/bin/bash" 3 hours ago Up 3 hours 0.0.0.0:8080->8080/tcp test11 e2b78fc1b028 java:8 "/bin/bash" 3 hours ago Up 3 hours test10 root@bogon: ~ 20:04:01 $ docker ps -s #列出容器显示大小 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE 42d6729d3d02 nginx:latest "nginx -g 'daemon of…" 2 hours ago Up 3 minutes 80/tcp nginx01 113MB (virtual 222MB) e125d0e27a24 java:8 "/bin/bash" 3 hours ago Up 3 hours 0.0.0.0:8080->8080/tcp test11 11B (virtual 643MB) e2b78fc1b028 java:8 "/bin/bash" 3 hours ago Up 3 hours test10 79B (virtual 643MB) 025f1f92a6eb nginx:latest "nginx -g 'daemon of…" 3 hours ago Up 3 hours 0.0.0.0:80->80/tcp nginx1.0 19B (virtual 109MB) root@bogon: ~ 20:04:06 $ docker ps --no-trunc #持续不断输出容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 42d6729d3d023a649117fba59d2fbd1d9212c3d6d0ce5bbe16c90aa845239574 nginx:latest "nginx -g 'daemon off;'" 2 hours ago Up 3 minutes 80/tcp nginx01 e125d0e27a24040cc89f9e71d52ab6a3c1b22a5c35cf17a5e94de3ce25c84100 java:8 "/bin/bash" 3 hours ago Up 3 hours 0.0.0.0:8080->8080/tcp test11 e2b78fc1b02881626a0eb40f138eee28f5d0de3f2471b29c2ef8be7eff3d964c java:8 "/bin/bash" 3 hours ago Up 3 hours test10 025f1f92a6eb887f59d37813b0e251c524b47c645a33a13f72c31b6e082c467b nginx:latest "nginx -g 'daemon off;'" 3 hours ago Up 3 hours 0.0.0.0:80->80/tcp nginx1.0 root@bogon: ~ 20:04:25 $ 12:export 将文件系统作为一个tar文档文件导出到stdout root@bogon: ~ 20:19:45 $ docker export -o nginx-`date +%Y%m%d`.tar 025f1f92a6eb root@bogon: ~ 20:20:16 $ ls -lh 总用量 214M -rw-------. 1 root root 1.4K 3月 11 14:59 anaconda-ks.cfg drwxr-xr-x. 2 root root 6 3月 11 19:47 cptest -rwxr-xr-x. 1 root root 1.8K 3月 11 15:35 ini.sh -rw-------. 1 root root 107M 3月 11 20:20 nginx-20190311.tar -rw-r--r--. 1 root root 108M 3月 11 16:53 nginx.tar root@bogon: ~ 20:20:17 $ 13 images 列出本地镜像 选项说明: -a:列出本地所有的镜像(含中间映像层,默认情况下,过滤掉中间映像层); --digests:显示镜像的摘要信息; -f:显示满足条件的镜像; --format:指定返回值的模板文件; --no-trunc:显示完整的镜像信息; -q:只显示镜像ID。 root@bogon: ~ 20:22:33 #列出镜像摘要信息DIGEST $ docker images --digests REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE jdk 8 <none> fec5236a803b 42 minutes ago 643MB nginx latest <none> 881bd08c0b08 6 days ago 109MB centos 7 sha256:365fc7f33107869dfcf2b3ba220ce0aa42e16d3f8e8b3c21d72af1ee622f0cf0 1e1148e4cc2c 3 months ago 202MB java 8 sha256:34959eed5df6bfe74a17fbf459ed29505d51a48d5cb4a39f215b613c65b8023b d23bdf5b1b1b 2 years ago 643MB root@bogon: ~ 20:22:35 $ root@bogon: ~ 20:23:54 #只显示镜像ID $ docker images -q fec5236a803b 881bd08c0b08 1e1148e4cc2c d23bdf5b1b1b root@bogon: ~ 20:23:59 $ 14: rmi 删除镜像 root@bogon: ~ 20:24:51 $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE jdk 8 fec5236a803b 44 minutes ago 643MB nginx latest 881bd08c0b08 6 days ago 109MB centos 7 1e1148e4cc2c 3 months ago 202MB java 8 d23bdf5b1b1b 2 years ago 643MB root@bogon: ~ 20:24:52 $ docker rmi -f jdk:8 Untagged: jdk:8 Deleted: sha256:fec5236a803b351e50de689fb30ad95b76149fdf7e19b885f503ed4c0e46bcbf Deleted: sha256:2f4402558d7c5639be8e9a6b1ab96659801b23e96de81c251b744bcd1ceb770e root@bogon: ~ 20:25:06 $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 881bd08c0b08 6 days ago 109MB centos 7 1e1148e4cc2c 3 months ago 202MB java 8 d23bdf5b1b1b 2 years ago 643MB root@bogon: ~ 20:25:09 $ 15:tag 标记本地金香,将其归入仓库 $ docker tag nginx:latest hctest/nginx:latest root@bogon: ~ 20:27:02 $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE hctest/nginx latest 881bd08c0b08 6 days ago 109MB nginx latest 881bd08c0b08 6 days ago 109MB centos 7 1e1148e4cc2c 3 months ago 202MB java 8 d23bdf5b1b1b 2 years ago 643MB root@bogon: ~ 20:27:04 $ 16:save && load 将镜像打包和重新导入 $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE hctest/nginx latest 881bd08c0b08 6 days ago 109MB nginx latest 881bd08c0b08 6 days ago 109MB centos 7 1e1148e4cc2c 3 months ago 202MB java 8 d23bdf5b1b1b 2 years ago 643MB root@bogon: ~ 20:29:48 $ docker save nginx:latest >hc_nginx.tar root@bogon: ~ 20:30:20 $ docker rmi -f nginx:latest Untagged: nginx:latest root@bogon: ~ 20:30:26 $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE hctest/nginx latest 881bd08c0b08 6 days ago 109MB centos 7 1e1148e4cc2c 3 months ago 202MB java 8 d23bdf5b1b1b 2 years ago 643MB root@bogon: ~ 20:30:29 $ docker load <hc_nginx.tar Loaded image: nginx:latest root@bogon: ~ 20:30:37 $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE hctest/nginx latest 881bd08c0b08 6 days ago 109MB nginx latest 881bd08c0b08 6 days ago 109MB centos 7 1e1148e4cc2c 3 months ago 202MB java 8 d23bdf5b1b1b 2 years ago 643MB root@bogon: ~ 20:30:39 $