Docker容器管理命令
Posted 青衫解衣
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker容器管理命令相关的知识,希望对你有一定的参考价值。
批量删除docker 容器
[root@harbor <sub>]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8c4f2e386534 redis "docker-entrypoint.s…" 3 seconds ago Up 2 seconds 6379/tcp redis
7247a5d0942b nginx "/docker-entrypoint.…" 12 seconds ago Up 10 seconds 80/tcp nginx
[root@harbor </sub>]# docker ps -q
8c4f2e386534
7247a5d0942b
[root@harbor <sub>]# docker rm -f $(docker ps -q)
8c4f2e386534
7247a5d0942b
[root@harbor </sub>]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@harbor ~]#
docker cp命令
docker cp :用于容器与主机之间的数据拷贝。
语法
docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
OPTIONS说明:
- -L :保持源目标中的链接
将容器中的文件cp到当前主机
[root@harbor opt]# docker cp nginx:/etc/passwd .
将主机的文件复制到容器nginx的/tmp目录下
[root@harbor opt]# docker cp passwd nginx:/tmp/
[root@harbor opt]# docker exec -it nginx ls -l /tmp/
total 4
-rw-r--r-- 1 root root 976 Dec 29 19:28 passwd
docker commit命令
说明:从容器生成新镜像。
语法
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
OPTIONS说明:
- -a :提交的镜像作者;
- -c :使用Dockerfile指令来创建镜像;
- -m :提交时的说明文字;
- -p :在commit时,将容器暂停。
[root@harbor <sub>]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6ab66f6aed9f redis "docker-entrypoint.s…" 7 minutes ago Up 7 minutes 6379/tcp redis
[root@harbor </sub>]# docker exec -it redis /bin/bash
root@6ab66f6aed9f:/data# ls
1.txt
root@6ab66f6aed9f:/data# cd /home/
root@6ab66f6aed9f:/home# ls
root@6ab66f6aed9f:/home# touch 1.txt
root@6ab66f6aed9f:/home# exit
exit
[root@harbor <sub>]# docker commit -m "touch file" redis myredis:v1
sha256:dfeb33a0971400845a90e0ec0df60e7ca9c4ac9e728235ebc997725e14d74a24
[root@harbor </sub>]# docker run -d --name "redis2" myredis:v1
09c41b30991e12ac470927b7778dddb8ea004aba66b7b57fcb486dda275d8a93
[root@harbor <sub>]# docker exec redis2 ls /home
1.txt
[root@harbor </sub>]# docker exec redis2 ls /data
注意:docker commit并不会提交数据,/data目录是挂载目录,里面创建的文件在commit之后,新镜像也是么有的。
docker容器端口映射
-P参数:随机端口映射
[root@harbor <sub>]# docker run -d -P --name nginx nginx
2398c4881afd36a410e5b685898ef5c530612e2a6dd3aa014c9a1bd16eeefee1
[root@harbor </sub>]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2398c4881afd nginx "/docker-entrypoint.…" 4 seconds ago Up 2 seconds 0.0.0.0:49153->80/tcp, :::49153->80/tcp nginx
[root@harbor <sub>]# docker run -d -P --name redis redis
b925781beee51d8390c130305ab56c3c7bc3f1979ac67ae8bdcd8688629af4ec
[root@harbor </sub>]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b925781beee5 redis "docker-entrypoint.s…" 4 seconds ago Up 2 seconds 0.0.0.0:49154->6379/tcp, :::49154->6379/tcp redis
2398c4881afd nginx "/docker-entrypoint.…" 34 seconds ago Up 32 seconds 0.0.0.0:49153->80/tcp, :::49153->80/tcp nginx
-p参数:指定端口映射
[root@harbor <sub>]# docker run -d -p 8080:80 --name nginx nginx
037abee1f783b01258cc28c7f3cb9967ce12df02ba607fdb56c7f075cbfab920
[root@harbor </sub>]# docker run -d -p 8081:80 --name nginx1 nginx
61ead3c51c9dcc4d7d60743c36bc425cbc1ef14a9a038c7875dd93fb724cee65
[root@harbor ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
61ead3c51c9d nginx "/docker-entrypoint.…" 4 seconds ago Up 2 seconds 0.0.0.0:8081->80/tcp, :::8081->80/tcp nginx1
037abee1f783 nginx "/docker-entrypoint.…" 10 seconds ago Up 9 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp nginx
Docker top 命令
说明:
查看运行容器中的进程
用法
docker top CONTAINER [ps OPTIONS]
[root@harbor <sub>]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6f0ca9e76701 redis "docker-entrypoint.s…" 3 minutes ago Up 8 seconds 6379/tcp redis
037abee1f783 nginx "/docker-entrypoint.…" 4 hours ago Up 11 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp nginx
[root@harbor </sub>]# docker top redis
UID PID PPID C STIME TTY TIME CMD
polkitd 14694 14674 0 23:39 ? 00:00:00 redis-server *:6379
[root@harbor ~]# docker top nginx
UID PID PPID C STIME TTY TIME CMD
root 14606 14585 0 23:39 ? 00:00:00 nginx: master process nginx -g daemon off;
101 14651 14606 0 23:39 ? 00:00:00 nginx: worker process
101 14652 14606 0 23:39 ? 00:00:00 nginx: worker process
Docker logs 命令
说明:获取容器的日志
语法
docker logs [OPTIONS] CONTAINER
OPTIONS说明:
- -f :跟踪日志输出
- --since :显示某个开始时间的所有日志
- -t :显示时间戳
- --tail :仅列出最新N条容器日志
[root@harbor ~]# docker logs -tf redis
2022-03-31T15:36:21.702734999Z 1:C 31 Mar 2022 15:36:21.702 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2022-03-31T15:36:21.702786593Z 1:C 31 Mar 2022 15:36:21.702 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=1, just started
2022-03-31T15:36:21.702793366Z 1:C 31 Mar 2022 15:36:21.702 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
2022-03-31T15:36:21.703813687Z 1:M 31 Mar 2022 15:36:21.703 * monotonic clock: POSIX clock_gettime
2022-03-31T15:36:21.704600751Z 1:M 31 Mar 2022 15:36:21.704 * Running mode=standalone, port=6379.
--tail参数使用
[root@harbor ~]# docker logs -tf --tail 2 redis
2022-03-31T15:39:18.098456426Z 1:M 31 Mar 2022 15:39:18.098 * DB loaded from disk: 0.000 seconds
2022-03-31T15:39:18.098459753Z 1:M 31 Mar 2022 15:39:18.098 * Ready to accept connections
--tail 参数在前在后都可以。
[root@harbor ~]# docker logs -tf redis --tail 2
2022-03-31T15:39:18.098456426Z 1:M 31 Mar 2022 15:39:18.098 * DB loaded from disk: 0.000 seconds
2022-03-31T15:39:18.098459753Z 1:M 31 Mar 2022 15:39:18.098 * Ready to accept connections
Docker rename命令
说明:容器重命名
语法:docker rename 旧容器name 新容器name
[root@harbor tmp]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
00bd9bf582ff redis:latest "docker-entrypoint.s…" 57 minutes ago Up 1 second 6379/tcp redis
[root@harbor tmp]# docker rename redis myredis
[root@harbor tmp]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
00bd9bf582ff redis:latest "docker-entrypoint.s…" 58 minutes ago Up 24 seconds 6379/tcp myredis
以上是关于Docker容器管理命令的主要内容,如果未能解决你的问题,请参考以下文章