Docker入门第四章

Docker的常用命令


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

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

Docker的镜像命令

docker images 查看本地主机所有的镜像

[root@bogon ~]# docker images 
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   5 months ago   13.3kB

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

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

docker search 搜索镜像

[root@bogon ~]# docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   11263     [OK]       
mariadb                           MariaDB Server is a high performing open sou…   4278      [OK]       
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   836                  [OK]

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

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

[root@bogon ~]# docker search mysql --filter=STARS=5000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   11263     [OK] 

docker pull 下载镜像

# 下载镜像  docker pull 镜像名[:tag]
[root@bogon ~]# docker pull mysql
Using default tag: latest	#如果不写tag,默认就是latest
latest: Pulling from library/mysql
33847f680f63: Pull complete  #分层下载,docker image的核心	联合文件系统
5cb67864e624: Pull complete 
1a2b594783f5: Pull complete 
b30e406dd925: Pull complete 
48901e306e4c: Pull complete 
603d2b7147fd: Pull complete 
802aa684c1c4: Pull complete 
715d3c143a06: Pull complete 
6978e1b7a511: Pull complete 
f0d78b0ac1be: Pull complete 
35a94d251ed1: Pull complete 
36f75719b1a9: Pull complete 
Digest: sha256:8b928a5117cf5c2238c7a09cd28c2e801ac98f91c3f8203a8938ae51f14700fd		#签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest	#真实地址
 
#等价于它
docker pull mysql
docker pull docker.io/library/mysql:latest

#指定版本下载
[root@bogon ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
33847f680f63: Already exists 
5cb67864e624: Already exists 
1a2b594783f5: Already exists 
b30e406dd925: Already exists 
48901e306e4c: Already exists 
603d2b7147fd: Already exists 
802aa684c1c4: Already exists 
5b5a19178915: Pull complete 
f9ce7411c6e4: Pull complete 
f51f6977d9b2: Pull complete 
aeb6b16ce012: Pull complete 
Digest: sha256:be70d18aedc37927293e7947c8de41ae6490ecd4c79df1db40d1b5b5af7d9596
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

Docker入门第四章

docker rmi 删除镜像

#删除指定的镜像
[root@bogon ~]# docker rmi -f 8cf625070931  #删除mysql5.7的镜像文件
Untagged: mysql:5.7
Untagged: mysql@sha256:be70d18aedc37927293e7947c8de41ae6490ecd4c79df1db40d1b5b5af7d9596
Deleted: sha256:8cf6250709314f2fcd2669e8643f5d3bdebfe715bddb63990c8c96e5d261d6fc
Deleted: sha256:452fe6896278c26338d547f8d1092011d923785247c46629b374d3477fe28c84
Deleted: sha256:bd40bf60af5d06e6b93eaf5a648393d97f70998faa3bfa1b85af55b5a270cb35
Deleted: sha256:c43e9e7d1e833650e0ed54be969d6410efa4e7fa6e27a236a44a2b97e412ee93
Deleted: sha256:70f18560bbf492ddb2eadbc511c58c4d01e51e8f5af237e3dbb319632f16335b

[root@bogon ~]# docker images	#可以看到已经删除mysql5.7了
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         latest    c60d96bd2b77   3 weeks ago    514MB
hello-world   latest    d1165f221234   5 months ago   13.3kB

#删除多个镜像

docker rmi -f 镜像ID 镜像ID 镜像ID 镜像ID

#删除全部镜像
[root@bogon ~]# docker rmi -f $(docker images -aq)   #  $()可以传入参数,将docker images -aq 查询所有image的镜像id
Untagged: mysql:latest
Untagged: mysql@sha256:8b928a5117cf5c2238c7a09cd28c2e801ac98f91c3f8203a8938ae51f14700fd
Deleted: sha256:c60d96bd2b771a8e3cae776e02e55ae914a6641139d963defeb3c93388f61707
Deleted: sha256:5c8c91273faab368a6d659156f2569fa9f40b0e0139222fdf9eef073df4b3797
Deleted: sha256:33d8196a776f42a16f10395b66f10f91443b1fb194bca2a9b8dfb0deff5babb8
Deleted: sha256:3ec63323025213e3cabf17ac7933506dc5520ec49226a9764418f77ea60d35c8
Deleted: sha256:1f129b005b51b049ac84ed0775b82096d480b7d9308a9a137697f37346562266
Deleted: sha256:80ed209bd0434faa1ce31fbaab8508124dddf8f6502c5736ee4b8e46697a8477
Deleted: sha256:e53f0d35c77064014a5c1c1332d84d5f421a58418ca9c208bc470691c0e483e3
Deleted: sha256:75209fb28131d5537e73406ff0f6f508f3eb1f4d86c43d1d16df76fd28b9cc35
Deleted: sha256:34a01bee1a62a01034ffc3da48a3cb45716a0cf2e264c26663e02288e81c7ec2
Deleted: sha256:9f8bca37a56017fd3462d4fc329b0b20f97c2dd4c15e55a8e6ad1c023ab5552b
Deleted: sha256:c8a6e3f9a2412c28cd8c48e2c7bed5e7fbaa0ab6649add2dbe8641cb29b967f6
Deleted: sha256:0a26eacdbd862e75d064d817e8a5bcf5e060c2680c10f77ffa52757c0b8c3328
Deleted: sha256:814bff7343242acfd20a2c841e041dd57c50f0cf844d4abd2329f78b992197f4
Untagged: hello-world:latest
Untagged: hello-world@sha256:0fe98d7debd9049c50b597ef1f85b7c1e8cc81f59c8d623fcb2250e8bec85b38
Deleted: sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726

[root@bogon ~]# docker images	#可以看到已经删除全部镜像了
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

Docker的容器命令

说明:有了镜像才可以创建容器,下载一个centos镜像来测试

[root@bogon ~]# docker pull centos		#下载centos镜像
Using default tag: latest
latest: Pulling from library/centos
7a0437f04f83: Pull complete 
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest


[root@bogon ~]# docker images		#下载完成
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
centos       latest    300e315adb2f   8 months ago   209MB

docker run image 新建容器并启动

docker run [可选参数]	image

#参数说明
--name="Name"	容器名称 tomcat01 tomcat02,用来区分容器
-d				后台方式运行
-it				使用交互方式运行,进入容器查看内容
-p				指定容器的端口 -p 8080:8080
	#-p有四种使用方式
	-p  ip:主机端口:容器端口
	-p	主机端口:容器端口
	-p	容器端口
	容器端口

-p	随机指定端口


#测试,启动并进入容器
[root@bogon ~]# docker run -it centos /bin/bash

#如果出现WARNING: IPv4 forwarding is disabled. Networking will not work.
解决办法:
vi /etc/sysctl.conf
net.ipv4.ip_forward=1  #添加这段代码

然后重启虚拟机

#查看是否修改成功 (备注:返回1,就是成功)
[root@docker-node2 ~]# sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 1


#从容器中退回到主机
[root@c8eb77459317 /]# exit 
exit
[root@lvshihao /]# ls    #[root@bogon ~]变成[root@lvshihao /]是因为我修改了一下hostname(也就是主机名)
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

docker ps 列出所有的运行的容器

#docker ps 命令
        	#列出当前正在运行的容器
	-a	#列出当前正在运行的容器,包括历史运行过的容器
	-n=?	#显示最近创建的容器 ?代表显示最近创建的前几个容器
	-q	#只显示容器的编号

[root@lvshihao /]# docker ps	#列出当前正在运行的容器
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

[root@lvshihao /]# docker ps -a	#列出当前正在运行的容器,包括历史运行过的容器
CONTAINER ID   IMAGE          COMMAND       CREATED             STATUS                         PORTS     NAMES
c8eb77459317   centos         "/bin/bash"   18 minutes ago      Exited (0) 15 minutes ago                pedantic_goldberg
3fd14ec500c4   centos         "/bin/bash"   29 minutes ago      Exited (1) 21 minutes ago                fervent_borg
9186fe713fdd   d1165f221234   "/hello"      About an hour ago   Exited (0) About an hour ago             intelligent_bouman

退出容器

exit			#直接容器停止并退出
Ctrl + P + Q 	#容器不停止退出

docker rm 删除容器

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

 启动和停止容器的操作

docker start 容器id	#启动容器
docker restart 容器id	#重启容器
odcker stop	容器id	#停止当前正在运行的的容器
docker kill	容器id	#强制停止当前容器
上一篇:Unity-SHA256加密


下一篇:Win7和Linux下利用命令查看文件md5、sha1、sha256