胸怀万里世界, 放眼无限未来。这篇文章主要讲述Docker容器管理命令相关的知识,希望能为你提供帮助。
批量删除docker 容器
[root@harbor < sub> ]# docker ps
CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES
8c4f2e386534redis"docker-entrypoint.s…"3 seconds agoUp 2 seconds6379/tcpredis
7247a5d0942bnginx"/docker-entrypoint.…"12 seconds agoUp 10 seconds80/tcpnginx
[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 IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES
[root@harbor ~]#
?
docker cp命令docker cp :用于容器与主机之间的数据拷贝。
语法
docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
OPTIONS说明:
- -L :保持源目标中的链接
文章图片
将主机的文件复制到容器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 IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES
6ab66f6aed9fredis"docker-entrypoint.s…"7 minutes agoUp 7 minutes6379/tcpredis
[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 IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES
2398c4881afdnginx"/docker-entrypoint.…"4 seconds agoUp 2 seconds0.0.0.0:49153-> 80/tcp, :::49153-> 80/tcpnginx
[root@harbor < sub> ]# docker run -d -P --name redis redis
b925781beee51d8390c130305ab56c3c7bc3f1979ac67ae8bdcd8688629af4ec
[root@harbor < /sub> ]# docker ps
CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES
b925781beee5redis"docker-entrypoint.s…"4 seconds agoUp 2 seconds0.0.0.0:49154-> 6379/tcp, :::49154-> 6379/tcpredis
2398c4881afdnginx"/docker-entrypoint.…"34 seconds agoUp 32 seconds0.0.0.0:49153-> 80/tcp, :::49153-> 80/tcpnginx
-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 IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES
61ead3c51c9dnginx"/docker-entrypoint.…"4 seconds agoUp 2 seconds0.0.0.0:8081-> 80/tcp, :::8081-> 80/tcpnginx1
037abee1f783nginx"/docker-entrypoint.…"10 seconds agoUp 9 seconds0.0.0.0:8080-> 80/tcp, :::8080-> 80/tcpnginx
?
Docker top 命令说明:
查看运行容器中的进程
用法
docker top CONTAINER [ps OPTIONS]
[root@harbor < sub> ]# docker ps
CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES
6f0ca9e76701redis"docker-entrypoint.s…"3 minutes agoUp 8 seconds6379/tcpredis
037abee1f783nginx"/docker-entrypoint.…"4 hours agoUp 11 seconds0.0.0.0:8080-> 80/tcp, :::8080-> 80/tcpnginx
[root@harbor < /sub> ]# docker top redis
UIDPIDPPIDCSTIMETTYTIMECMD
polkitd1469414674023:39?00:00:00redis-server *:6379
[root@harbor ~]# docker top nginx
UIDPIDPPIDCSTIMETTYTIMECMD
root1460614585023:39?00:00:00nginx: master process nginx -g daemon off;
1011465114606023:39?00:00:00nginx: worker process
1011465214606023:39?00:00:00nginx: 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 -tfredis --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 IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES
00bd9bf582ffredis:latest"docker-entrypoint.s…"57 minutes agoUp 1 second6379/tcpredis
[root@harbor tmp]# docker rename redis myredis
[root@harbor tmp]# docker ps
CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES
00bd9bf582ffredis:latest"docker-entrypoint.s…"58 minutes agoUp 24 seconds6379/tcpmyredis
【Docker容器管理命令】
推荐阅读
- ABeam Consulting也为这项活动提供了支持
- 利用nginx+fancyindex美化目录索引
- 基于VMware Workstation构建Vagrant base box
- java高级用法之:JNA中的回调
- 干货 | 图标(ICON) 的 9 种设计原则
- Ansible自动化工具的实践
- Docker镜像管理
- Win10基础 路径 存放系统壁纸的文件夹
- 使用Redis解决秒杀业务问题分析与解决方案