常用命令拓展
后台启动容器 -d
# 如果这个容器里面没有前台进程、直接通过-d启动,就会退出。
[root@kuangshenlinux ~]# docker run -d centos
[root@kuangshenlinux ~]# docker run -d -it centos /bin/bash
[root@kuangshenlinux ~]# docker run -d -it centos /bin/bash
0e4a34338c611fd748ea6f7610b83e2fcfd68506d0c3b2b684f61746544bd7e6
[root@kuangshenlinux ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e4a34338c61 centos "/bin/bash" 4 seconds ago Up 3 seconds serene_margulis
查看日志 docker logs
# -c 可以输入一些 shell脚本来执行
[root@kuangshenlinux ~]# docker run -d centos /bin/sh -c "while true;do echo kuangshen;sleep 1;done"
bb07cc10ea857fea8c1d7e1ee6f4bbfb3d028e97553a9db184d5e6698cdd192b
[root@kuangshenlinux ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bb07cc10ea85 centos "/bin/sh -c 'while t…" 2 seconds ago Up 2 seconds epic_shaw
0e4a34338c61 centos "/bin/bash" 3 minutes ago Up 3 minutes serene_margulis
#docker logs
# -t 打印 时间戳 -f 打印最新的日志
[root@kuangshenlinux ~]# docker logs -tf --tail 10 bb07cc10ea85
2023-12-09T12:14:03.034524461Z kuangshen
2023-12-09T12:14:04.043121537Z kuangshen
2023-12-09T12:14:05.048666087Z kuangshen
查看容器相关的进程 docker top
[root@kuangshenlinux ~]# docker top bb07
UID PID PPID C STIME TTY TIME CMD
root 13438 13417 0 20:13 ? 00:00:00 /bin/sh -c while true;do echo kuangshen;sleep 1;done
root 13617 13438 0 20:15 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
查看容器的元数据(inspect)
[root@kuangshenlinux ~]# docker inspect bb07
[
{
"Id": "bb07cc10ea857fea8c1d7e1ee6f4bbfb3d028e97553a9db184d5e6698cdd192b",
"Created": "2023-12-09T12:13:40.596173947Z",
"Path": "/bin/sh",
...........
}
进入一个正在执行的容器
docker exec -it 容器id /bin/bash
# 控制登录的shell,如果不控制的,会进入正在执行的容器终端中 attach
[root@kuangshenlinux ~]# docker exec -it bb07 /bin/bash
[root@bb07cc10ea85 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@bb07cc10ea85 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 12:13 ? 00:00:00 /bin/sh -c while true;do echo kuangshen;sleep 1;done
root 386 0 0 12:20 pts/0 00:00:00 /bin/bash
root 406 1 0 12:20 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
root 407 386 0 12:20 pts/0 00:00:00 ps -ef
<