Docker常用命令及概念理解

常用命令拓展

后台启动容器 -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


<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值