Docker命令

Docker的常用命令

帮助命令

docker version      #显示docker的版本信息
docker info            #显示docker的系统信息,包括境像和容器的数量
docker 命令 --help# 帮助命令

帮助文档的地址:https://docs.docker.com/engine/reference/commandline

镜像命令

docker images 查看所有本地的主机上的镜像
[root@xxx /]# docker images
REPOSITORY			TAG	    	IMAGE ID		      CREATED	         	SIZE
he11o-world			Tatest		bf756fblae65		4 months ago		13.3kB

解释
REPOSITORY    镜像的仓库源
TAG		      	 镜像的标签
IMAGE ID		  镜像的id
CREATED		  镜像的创建时间
SIZE					镜像的大小

#可选项
-a,--all						   列出所有镜像
-q,--quiet  				    只显示ID

docker search 搜索镜像

[root@xxx /]# docker search mysql
NAME                            DESCRIPTION                                                    STARS     OFFICIAL   AUTOMATED
mysql                 MySQL is a widely used, open-source relation…    14338        [OK]       
mariadb             MariaDB Server is a high performing open sou…   5474         [OK]       

#可选项,通过搜藏来过滤
--filter=STARS=3000# 搜索出来的镜像就是STARS大于3000的

[root@xxx ~]# docker search mysql --filter=STARS=3000
NAME        					 DESCRIPTION                              			  STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   	14338     [OK]       
mariadb   MariaDB Server is a high performing open sou…    5474      [OK]       

拉取镜像docker pull 镜像名[:tag]

[root@163 ~]# docker pull mysql
Using default tag: latest  #如果不写tag,默认最新版本
latest: Pulling from library/mysql
72a69066d2fe: Pull complete 		#分层下载,docker  image的核心  联合文件系统
93619dbc5b36: Pull complete 
99da31dd6142: Pull complete 
626033c43d70: Pull complete 
37d5d7efb64e: Pull complete 
ac563158d721: Pull complete 
d2ba16033dad: Pull complete 
688ba7d5c01a: Pull complete 
00e060b6d11d: Pull complete 
1c04857f594f: Pull complete 
4d7cfa90e6ea: Pull complete 
e0431212d27d: Pull complete 
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709  #签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest	#真实地址

*docker pull mysql等价于docker pull docker.io/library/mysql:latest

*docker pull mysql:5.7
[root@163 ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Already exists 
93619dbc5b36: Already exists 
99da31dd6142: Already exists 
626033c43d70: Already exists 
37d5d7efb64e: Already exists 
ac563158d721: Already exists 
d2ba16033dad: Already exists //以上为重复部分
0ceb82207cd7: Pull complete 
37f2405cae96: Pull complete 
e2482e017e53: Pull complete 
70deed891d42: Pull complete 
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

删除镜像docker rmi

docker rmi -f   镜像ID			 //删除指定镜像

docker rmi -f   镜像ID 镜像ID 镜像ID //删除多个镜像

docker rmi -f  $(docker images -aq)	  //删除镜像z

容器命令

说明:我们有了镜像才可以创建容器,linux,下载一个 centos 镜像来测试学习
docker pull centos

新建容器并启动

docker run [可选参数] image

#参数说明
--name="Name"		   容器名字 tomcat01 tomcat02,用来区分容器
-d									后台方式运行
-it									使用交互方式运行,进入容器查看内容

-p								   指定容器的端口-p  8080:8080

     	 	-p ip:主机端口:容器端口
			-p 主机端口:容器端口 (常用)
			-p 容器端口
			容器端口

-P								   随机指定端口

#测试,启动并进入容器

[root@xxx ~]# docker run -it centos /bin/bash

[root@c0eed8b69c30 /]# ls   //查看容器内的centos,基础版本,很多命令都不完善
bin  etc   lib	  lost+found  mnt  proc  run   srv  tmp  var	
dev  home  lib64  media       opt  root  sbin  sys  usr

exit		//从容器中退回主机

列出所有运行的容器

#docker ps 命令
     	列出当前正在运行的容器

-a 		//列出当前正在运行的容器+带出历史运行过的容器
-n=? 	// 显示最近创建的容器
-q		//只显示容器的编号

退出容器

exit 				   //直接容器停止并退出  

Ctrl + P + Q 	//容不停止退出

删除容器

docker rm 容器id		//删除指定的容器,不能除正在运行的容器,如果要强制刚除 rm -f
docker rm -f $(docker ps -aq)		//删除所有的容器
docker ps -a -q|xargs docker rm 		// 删除所有的容器

启动和停止容器的操作

docker start 容器id			启动容器
docker restart 容器id		重启容器
docker stop 容器id			停止当前正在运行的容器
docker kill 容器id			  强制停止当前容器

常用其他命令

后台启动容器

#命令 docker run -d 镜像名!

[root@xxx /]# docker run -d centos

#问题docker ps,发现 centos 停止了

#常见的坑:docker 容器使用后台运行,就必须要有要一个前台进程,docker发现没有应用,就会自动停止

#nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了

查看日志

docker logs -f -t --tail 容器,没有日志

#自己编写一段shell脚本

[root@xxx /]# docker run -d centos /bin/sh -c "while true;do echo hhhhhh;sleep 1;done"

[root@xxx /]# docker ps

CONTAINER ID			IMAGE
dce7b86171bf			centos

#显示日志

-tf								//显示日志

--tai1 number			//要显示日志条数

[root@xxx /]# docker logs -tf --tai1 10 dce7b86171bf

查看容器中进程信息ps

#命令docker top 容器id
UID                 PID                 PPID        	    C                 STIME       
root                1432624             1432594             0                   20:14     
root                1452486             1432624             0                   20:20     

查看镜像的元数据

#命令 docker inspect 容器id

进入当前正在运行的容器

方式一:docker exec -it 容器id /bin/bash		//进入容器后开启一个新的终端,可以在里面操作

方式二:docker attach 容器id		 				 //进入容器正在执行的终端,不会启动新的进程

从容器内拷贝文件到主机上

docker  cp 容器id:容器内路径  目的的主机路径

#查看当将主机目录下

[root@163 home]# ls
hhh.java  redis  www
[root@163 home]#  docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
69687a39e545   centos    "/bin/bash"   2 minutes ago   Up 2 minutes             vigilant_blackburn

#进入docker容器内部

[root@163 home]# docker attach 69687a39e545
[root@69687a39e545 /]# cd /home
[root@69687a39e545 home]# ls

#在容器内新建一个文件

[root@69687a39e545 home]# touch test.java
[root@69687a39e545 home]# exit
exit
[root@163 home]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@163 home]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                      PORTS     NAMES
69687a39e545   centos    "/bin/bash"   4 minutes ago   Exited (0) 31 seconds ago             vigilant_blackburn

#将文件拷贝出来到主机上

[root@163 home]# docker cp 69687a39e545:/home/test.java /home
Successfully copied 1.54kB to /home
[root@163 home]# ls
hhh.java  redis  test.java  www

#拷贝是一个手动过程,使用-v卷的技术可以实现

小结

在这里插入图片描述

attachAttach to a running container当前 she11 下 attach 连接指定运行镜像
buildBuild an imagerfrom a Dockerfile通过 Dockerfile 定制镜像
commitCreate a new image from a container changes提交当前容器为新的镜像
cpCopy files/folders from the containers filesystem to the host path从容器中搏贝指定文件或者目录到宿主机中
createCreate a new container创建一个新的容器,同 run,但不启动容器
diffInspect changes on a container’s fiesystem查看 docker 容器变化
eventsGet real time events from the server从 docker 服务获取容器实时事件
execRun a command in an existing container在已存在的容器上运行命令
exportStream the contents of a container as a tar archive导出容器的内容作为一个 tar 归档文件[对应import]
historyShow the history of an image展示一个镜像形成历史
imagesList images列出系统当前镜像
importCreate a new filesystem image from the contents of a tarball从tar包中的内容创建一个新的文件系统映像[对应export]
infoDisplay system-wide information显示系统相关信息
inspectReturn low-Tevel information on a container查看容器详细信息
killKill a running containerkill 指定 docker 容器
loadLoad an image from a tar archive从一个 tar 包中加载一个像[对应 save]
loginRegister or Login to the docker registry server注册或者陆一个 docker 源服务器
logoutLog out from a Docker registry server从当前 Docker registry 退出
logsFetch the logs of a container输出当前容器日志信息
portLookup the public-facing port which is NAT-ed to PRIVATE_PORT查看映射端口对应的容器内部源端口
pausePause all processes within a container暂停容器
psList containers列出容器列表
pullPull an image or a repository from the docker registry server从docker镜像源服务器拉取指定镜像或者库镜像
pushPush an image or a repository to the docker registry server推送指定镜像或者库镜像至docker源服务器
restartRestart a running container重启运行的容器
rmRemove one or more containers移除一个或者多个容器
rmiRemove one or more images移除一个或多个镜像[无容器使用该镜像才可删除,否则需删除相关容器才可继续或 -f 强制删除]
runRun a command in a new container创建一个新的容器并运行一个命令
saveSave an image to a tar archive保存一个镜像为一个 tar 包[对应 load]
searchSearch for an image on the Docker Hub在 docker hub 中搜索镜像
startStart a stopped containers启动容器
stopStop a running containers停止容器
tagTag an image into a repository给源中镜像打标签
topLookup the running processes of a container查看容器中运行的进程信息
unpauseUnpause a paused container取消暂停容器
versionShow the docker version information查看 docker 版本号
waitBlock until a container stops,then print its exit code截取容器止时的退出状态值
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值