運行交互式容器時以Ctrl+P Ctrl+Q 來退出容器,此時容器還在後臺繼續運行,咱們能夠經過docker attach [name] 來從新進入容器docker
[root@aliyun ~]# docker start -i cjj01 root@4f38551cf3cf:/# // Ctrl+P Ctrl+Q 退出
[root@aliyun ~]# [root@aliyun ~]# docker ps // 能夠查看正在運行的容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4f38551cf3cf ubuntu "/bin/bash" 31 minutes ago Up 57 seconds cjj01 [root@aliyun ~]# docker attach cjj01 root@4f38551cf3cf:/#
-t:在新容器內指定一個僞終端或終端。ubuntu
-i:容許你對容器內的標準輸入 (STDIN) 進行交互。bash
[root@aliyun ~]# docker run --name cjj02 -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done" 74fb89116dac4410bd268c92a3c2a9507430c49f3e61795142cad9ffed565837 [root@aliyun ~]# docker logs cjj02 // 查看容器內的標準輸出 hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world [root@aliyun ~]#
在輸出中,咱們沒有看到指望的"hello world",而是一串長字符spa
74fb89116dac4410bd268c92a3c2a9507430c49f3e61795142cad9ffed565837code
這個長字符串叫作容器ID,對每一個容器來講都是惟一的,咱們能夠經過容器ID來查看對應的容器發生了什麼。blog
首先,咱們須要確認容器有在運行,能夠經過 docker ps 來查看進程
[root@aliyun ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 74fb89116dac ubuntu "/bin/sh -c 'while..." 2 minutes ago Up 2 minutes cjj02
CONTAINER ID:容器ID字符串
NAMES:自動分配的容器名稱class
docker stop [容器名]容器
docker kill [容器名]
[root@aliyun ~]# docker stop cjj02
cjj02
[root@aliyun ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 74fb89116dac ubuntu "/bin/sh -c 'while..." 25 minutes ago Up 12 seconds cjj02 [root@aliyun ~]# docker top cjj02 UID PID PPID C STIME TTY TIME CMD root 9045 9028 0 17:38 ? 00:00:00 /bin/sh -c while true; do echo hello world; sleep 1; done root 9090 9045 0 17:39 ? 00:00:00 sleep 1 [root@aliyun ~]#
exec 命令
[root@aliyun ~]# docker top cjj02 UID PID PPID C STIME TTY TIME CMD root 9045 9028 0 17:38 ? 00:00:00 /bin/sh -c while true; do echo hello world; sleep 1; done root 9090 9045 0 17:39 ? 00:00:00 sleep 1 [root@aliyun ~]# docker exec -i -t cjj02 /bin/bash root@74fb89116dac:/# // Ctrl+P Ctrl+Q 退出
[root@aliyun ~]# docker top cjj02
UID PID PPID C STIME TTY TIME CMD
root 9045 9028 0 17:38 ? 00:00:00 /bin/sh -c while true; do echo hello world; sleep 1; done
root 13786 13768 0 18:56 pts/1 00:00:00 /bin/bashroot 13867 9045 0 18:57 ? 00:00:00 sleep 1