Docker 常用命令2

Posted ahtornado

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker 常用命令2相关的知识,希望对你有一定的参考价值。

 #只显示当前本地镜像的ID 

[[email protected] ~]# docker images -q
9f5834b25059

#显示全部的本地镜像的ID 
[[email protected] ~]# docker images -qa
9f5834b25059
65b27d3bd74d

#显示镜像的摘要信息
[[email protected] ~]# docker images --digests 
REPOSITORY TAG DIGEST IMAGE ID CREATED VIRTUAL SIZE
hello-world latest <none> 9f5834b25059 4 days ago 1.84 kB

 #显示镜像的完整信息
[[email protected] ~]# docker images --no-trunc
REPOSITORY TAG IMAGE ID CREATED
VIRTUAL SIZE
hello-world latest 9f5834b25059239faef06a9ba681db7b7c572fc0d87d2b140b10e90e50902b53 4 days ago
1.84 kB
#列出收藏数大于30的镜像

[[email protected] ~]# docker search -s 30 tomcat 
NAME DESCRIPTION STARS OFFICIAL AUTOMATEDAUTOMATED
tomcat Apache Tomcat is an open source implementa... 2246 [OK]
tomee Apache TomEE is an all-Apache Java EE cert... 59 [OK]
dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 ba... 51 [OK]
davidcaste/alpine-tomcat Apache Tomcat 7/8 using Oracle Java 7/8 wi... 31 [OK]

#下载镜像
[[email protected] ~]# docker pull tomcat

#等价于 docker pull tomcat:latest
#删除镜像
[[email protected] ~]# docker rmi -f hello-world
Untagged: hello-world:latest
Deleted: 9f5834b25059239faef06a9ba681db7b7c572fc0d87d2b140b10e90e50902b53
Deleted: 65b27d3bd74d2cf4ea3aa9e250be6c632f0a347e8abd5485345c55fa6eed0258
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
tomcat latest 0260919306ba 6 days ago 462.4 MB

#删除所有的镜像(i:表示镜像)

[[email protected] ~]# docker rmi -f $(docker images -qa) 

#运行容器
[[email protected] ~]# docker run -it centos
[[email protected] /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 09:00 ? 00:00:00 /bin/bash
root 15 1 0 09:00 ? 00:00:00 ps -ef


#查看正在运行的容器
[[email protected] ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
d1e49e5ec93d centos "/bin/bash" About a minute ago Up About a minute
naughty_fermi

#退出并关闭容器
[[email protected] /]# exit (#容器停止并退出,ctrl+P+Q 容器不停止的情况下退出容器)

#列出上一次运行的容器(不是数字‘1’)

[[email protected] ~]# docker ps -l 
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
b8b5c1a57b2c centos "/bin/bash" 6 minutes ago Exited (0) 21 seconds ago
compassionate_wilson

 #列出上3次运行的容器

[[email protected] ~]# docker ps -n 3
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
b8b5c1a57b2c centos "/bin/bash" 8 minutes ago Exited (0) 2 minutes ago
compassionate_wilson
c668511d9dc3 hello-world "/hello" 30 hours ago Exited (0) 30 hours ago
angry_curie

#历史运行的容器

[[email protected] ~]# docker ps -a 
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
b8b5c1a57b2c centos "/bin/bash" 6 minutes ago Exited (0) 28 seconds ago
compassionate_wilson
c668511d9dc3 hello-world "/hello" 30 hours ago Exited (0) 30 hours ago
angry_curie

#运行一个centos容器,指定别名为mycentos

[[email protected] ~]# docker run -it --name mycentos ab9a80ab07d0 
[[email protected] /]#

[[email protected] ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
5783a20d79c9 ab9a80ab07d0 "/bin/bash" 18 seconds ago Up 17 seconds
mycentos

#查看运行过的容器,并启动已经关闭的容器
[[email protected] ~]# docker ps -n 3
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
5783a20d79c9 ab9a80ab07d0 "/bin/bash" 8 minutes ago Up 8 minutes
mycentos
b8b5c1a57b2c centos "/bin/bash" 21 minutes ago Exited (0) 15 minutes ago
compassionate_wilson
c668511d9dc3 hello-world "/hello" 30 hours ago Exited (0) 30 hours ago
angry_curie
[[email protected] ~]# docker start b8b5c1a57b2c
b8b5c1a57b2c
[[email protected] ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
5783a20d79c9 ab9a80ab07d0 "/bin/bash" 9 minutes ago Up 9 minutes
mycentos
b8b5c1a57b2c centos "/bin/bash" 22 minutes ago Up 6 seconds
compassionate_wilson

#停止容器
[[email protected] ~]# docker stop ab9a80ab07d0 

#强制停止容器

[[email protected] ~]# docker kill ab9a80ab07d0 

 #删除已经停止的容器

[[email protected] ~]# docker rm ab9a80ab07d0

#强制删除容器,不管是否停止

[[email protected] ~]# docker rm -f ab9a80ab07d0 

#强制删除所有的容器 docker ps -qa|xargs docker rm
[[email protected] ~]# docker rm -f $(docker ps -qa) 

 #后台启动一个centos容器,启动后就退出了

[[email protected] ~]# docker run -d centos
566663e3632122a7b10c5ea76e116ffc12e9173f6075f7c25d76721a026955d2


[[email protected] ~]# docker run -d centos /bin/sh -c "while true;do echo hello zzyy;sleep 2;done"
c0f8dd6e7e7806d16eee45abbc3481afd2c2c64450f76b9dfefca734a0f71f76
[[email protected] ~]#

[[email protected] ~]# docker logs c0f8dd6e7e78
hello zzyy
hello zzyy
hello zzyy
hello zzyy
hello zzyy
hello zzyy

[[email protected] ~]# docker logs -t -f --tail 5 c0f8dd6e7e78
2019-01-05T09:32:47.259055965Z hello zzyy
2019-01-05T09:32:49.262340607Z hello zzyy
2019-01-05T09:33:07.290292608Z hello zzyy
2019-01-05T09:33:09.293592246Z hello zzyy
2019-01-05T09:33:11.304916080Z hello zzyy
2019-01-05T09:33:13.308580989Z hello zzyy
2019-01-05T09:33:15.310645986Z hello zzyy
2019-01-05T09:33:17.312546777Z hello zzyy
2019-01-05T09:33:19.314682982Z hello zzyy




































































































以上是关于Docker 常用命令2的主要内容,如果未能解决你的问题,请参考以下文章

Docker快速入门——Docker常用命令

Docker常用命令大全

Docker容器常用命令

3. Docker学习之路 —— Docker常用命令

Docker Centos 64位 安装及常用命令

Docker Centos 64位 安装及常用命令