# 查看所有镜像 ubuntu@VM-0-4-ubuntu:~$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 74435f89ab78 7 days ago 73.9MB ubuntu 18.04 8e4ce0a6ce69 7 days ago 64.2MB mysql latest be0dbf01a0f3 2 weeks ago 541MB hello-world latest bf756fb1ae65 5 months ago 13.3kB # 查看当前正在运行的容器 ubuntu@VM-0-4-ubuntu:~$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES # 后来运行ubuntu容器 ubuntu@VM-0-4-ubuntu:~$ sudo docker run -d ubuntu 6173c91e2589e3f43172805521a858e3ca242db72fcbf653849fac9216faf934 # 再次查看当前正在运行的容器,发现ubuntu并没有正在运行?! # 这是因为docker没有发现应用便会自动停止,所以若要使得docker容器可以在后台启动并运行,则前台必须要有一个进程 ubuntu@VM-0-4-ubuntu:~$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Options: --details Show extra details provided to logs -f, --follow Follow log output --since string Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes) --tail string Number of lines to show from the end of the logs (default "all") -t, --timestamps Show timestamps --until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes) # 查看当前正在运行的容器(无) ubuntu@VM-0-4-ubuntu:~$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES # 执行 docker run -d <容器名称> 后台启动 ubuntu 容器并在该容器中创建一个进程( 一段shell脚本 )以防止docker容器没有发现进程而自动停止 ubuntu@VM-0-4-ubuntu:~$ sudo docker run -d ubuntu /bin/sh -c "while true;do echo 'this is logs..';sleep 1;done" b89578415781997955293bbff1ccfc849bf8cad0f6af30193a16d51e576862b2 ubuntu@VM-0-4-ubuntu:~$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b89578415781 ubuntu "/bin/sh -c 'while t…" 9 seconds ago Up 7 seconds jovial_nightingale # 根据指定容器id来查看容器的日志信息 # -tf : 显示日志 # --tail number : 显示日志的条数 ubuntu@VM-0-4-ubuntu:~$ sudo docker logs -tf --tail 5 b89578415781 2020-06-24T02:01:55.077195969Z this is logs.. 2020-06-24T02:01:56.081186346Z this is logs.. 2020-06-24T02:01:57.085186008Z this is logs.. 2020-06-24T02:01:58.089245329Z this is logs.. 2020-06-24T02:01:59.093251030Z this is logs.. 2020-06-24T02:02:00.097191092Z this is logs.. 2020-06-24T02:02:01.101183828Z this is logs.. 2020-06-24T02:02:02.105495333Z this is logs.. 2020-06-24T02:02:03.109132666Z this is logs.. ^C # ctrl + c ubuntu@VM-0-4-ubuntu:~$
查看容器中的进程信息
1 2 3 4 5 6 7 8 9 10 11
# 查看当前正在运行的容器 ubuntu@VM-0-4-ubuntu:~$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5f39caf62a73 ubuntu "/bin/sh -c 'while t…" 6 seconds ago Up 4 seconds magical_leakey # 根据容器id查看当前正在运行的ubuntu容器的进程信息 ubuntu@VM-0-4-ubuntu:~$ sudo docker top 5f39caf62a73 UID PID PPID C STIME TTY TIME CMD root 17430 17403 0 10:14 ? 00:00:00 /bin/sh -c while true;do echo 'this is logs..';sleep 1;done root 17532 17430 0 10:14 ? 00:00:00 sleep 1
# 查看当前正在运行的容器 ubuntu@VM-0-4-ubuntu:~$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5f39caf62a73 ubuntu "/bin/sh -c 'while t…" 33 minutes ago Up 33 minutes magical_leakey # 方式一 : docker exec -it <容器id> /bin/bash # 根据id进入当前正在运行的ubuntu容器,特征为进入容器后开启一个新的终端 ubuntu@VM-0-4-ubuntu:~$ sudo docker exec -it 5f39caf62a73 /bin/bash root@5f39caf62a73:/# ls bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var # 方式二 : docker attach <容器id> # 根据id进入当前正在运行的ubuntu容器,特征为进入一个容器正在执行的终端 ubuntu@VM-0-4-ubuntu:~$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ed8c6b026ee1 ubuntu "/bin/sh -c 'while t…" 7 seconds ago Up 5 seconds stupefied_boyd ubuntu@VM-0-4-ubuntu:~$ sudo docker attach ed8c6b026ee1 this is logs.. this is logs.. this is logs.. this is logs.. this is logs.. this is logs.. ^Cubuntu@VM-0-4-ubuntu:~$