Docker的常用命令

帮助命令:

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

帮助文档的地址:

https://docs.docker.com/reference/

镜像命令:

1、docker images — 查看所有本地的主机上的镜像

[root@VM-0-10-centos /]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    feb5d9fea6a5   2 months ago   13.3kB
解释:
参数解释
REPOSITORY镜像的仓库源
TAG镜像的标签
IMAGE ID镜像的ID
CREATED镜像的创建时间
SIZE镜像的大小
可选项:
参数解释
-a ,–all列出所有镜像
-q,–quiet只显示镜像的id

2、docker search — 搜索镜像

[root@VM-0-10-centos /]# docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   11722     [OK]       
mariadb                           MariaDB Server is a high performing open sou…   4471      [OK]       
...
可选项,通过搜藏来过滤
--filter=STARS=3000		# 搜索出来的镜像就是STARS大于3000的
[root@VM-0-10-centos /]# docker search mysql --filter=STARS=3000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   11722     [OK]       
mariadb   MariaDB Server is a high performing open sou…   4471      [OK]       

3、docker pull — 下载镜像

# 下载镜像 docker pull 镜像名[:tag]
[root@VM-0-10-centos /]# docker pull mysql
Using default tag: latest			#如果不写tag,默认就是latest	
latest: Pulling from library/mysql
a10c77af2613: Pull complete 		# 分层下载,docker images核心 联合文件系统
b76a7eb51ffd: Pull complete 
258223f927e4: Pull complete 
2d2c75386df9: Pull complete 
63e92e4046c9: Pull complete 
f5845c731544: Pull complete 
bd0401123a9b: Pull complete 
3ef07ec35f1a: Pull complete 
c93a31315089: Pull complete 
3349ed800d44: Pull complete 
6d01857ca4c1: Pull complete 
4cc13890eda8: Pull complete 
Digest: sha256:aeecae58035f3868bf4f00e5fc623630d8b438db9d05f4d8c6538deb14d4c31b	# 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest	# 真实地址

docker pull mysql
#等价于
docker.io/library/mysql:latest
# 指定版本下载
[root@VM-0-10-centos /]# docker pull mysql:5.7
5.7: Pulling from library/mysql
a10c77af2613: Already exists 	
b76a7eb51ffd: Already exists 
258223f927e4: Already exists 
2d2c75386df9: Already exists 
63e92e4046c9: Already exists 
f5845c731544: Already exists 
bd0401123a9b: Already exists 
2724b2da64fd: Pull complete 
d10a7e9e325c: Pull complete 
1c5fd9c3683d: Pull complete 
2e35f83a12e9: Pull complete 
Digest: sha256:7a3a7b7a29e6fbff433c339fc52245435fa2c308586481f2f92ab1df239d6a29
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

4、docker rmi — 删除镜像

# 查看所有镜像
[root@VM-0-10-centos /]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         5.7       8b43c6af2ad0   7 days ago     448MB
mysql         latest    b05128b000dd   7 days ago     516MB
hello-world   latest    feb5d9fea6a5   2 months ago   13.3kB
# docker rmi -f 镜像id		#删除指定的镜像
# docker rmi -f 镜像id 镜像id 镜像id		#删除多个指定的镜像
[root@VM-0-10-centos /]# docker rmi -f 8b43c6af2ad0
Untagged: mysql:5.7
Untagged: mysql@sha256:7a3a7b7a29e6fbff433c339fc52245435fa2c308586481f2f92ab1df239d6a29
Deleted: sha256:8b43c6af2ad08d95cdcb415d245446909a6cbc1875604c48c4325972e5b00442
Deleted: sha256:aad43f4d2f66438acd2d156216cd544a728851238714975c38d9a690f68afc57
Deleted: sha256:7b9addbc002c1e828aee7ec5c2679b04a591b6fa2b96002701ddee9d4ed54395
Deleted: sha256:b00f8e4e6ce8920fb563615503f232799ab380b338c3f2cbb5e86a2d762a6e80
Deleted: sha256:8fbabb17fd7b46a59cc15301741bf73a527b862f59cc6e84fae15b4dd5c425c0
# 删除所有镜像
[root@VM-0-10-centos /]# docker rmi -f $(docker images -aq)
# 查看镜像
[root@VM-0-10-centos /]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

容器命令:

说明:有了镜像才可以创建容器

# docker pull centos		# 下载一个CentOS镜像来测试学习
[root@VM-0-10-centos /]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete 
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest

1、新建容器并启动

docker run [可选参数] image
# 参数说明
-a stdin: 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项;

-d: 后台运行容器,并返回容器ID;

-i: 以交互模式运行容器,通常与 -t 同时使用;

-P: 随机端口映射,容器内部端口随机映射到主机的端口

-p: 指定端口映射,格式为:主机(宿主)端口:容器端口

-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;

--name="nginx-lb": 为容器指定一个名称;

--dns 8.8.8.8: 指定容器使用的DNS服务器,默认和宿主一致;

--dns-search example.com: 指定容器DNS搜索域名,默认和宿主一致;

-h "mars": 指定容器的hostname;

-e username="ritchie": 设置环境变量;

--env-file=[]: 从指定文件读入环境变量;

--cpuset="0-2" or --cpuset="0,1,2": 绑定容器到指定CPU运行;

-m :设置容器使用内存最大值;

--net="bridge": 指定容器的网络连接类型,支持 bridge/host/none/container: 四种类型;

--link=[]: 添加链接到另一个容器;

--expose=[]: 开放一个端口或一组端口;

--volume , -v: 绑定一个卷

2、测试

# 启动并进入容器
[root@VM-0-10-centos /]# docker run -it centos /bin/bash
[root@02846c1c59a7 /]# ls	# 查看容器内的centos,基础版本,很多命令不完善
bin  etc   lib	  lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
# 从容器中退回主机 
[root@02846c1c59a7 /]# exit
exit
[root@VM-0-10-centos /]# ls
bin   data  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
boot  dev   home  lib64  media       opt  root  sbin  sys  usr

3、列出所有运行的容器 ---- docker ps 命令

		# 列出当前正在运行的容器
 -a		# 列出当前正在运行的容器+带出历史运行过的容器
[root@VM-0-10-centos /]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@VM-0-10-centos /]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS                     PORTS     NAMES
02846c1c59a7   centos         "/bin/bash"   5 minutes ago   Exited (0) 2 minutes ago             mystifying_euclid
e9638e508e8c   feb5d9fea6a5   "/hello"      21 hours ago    Exited (0) 21 hours ago              recursing_colden
-n=# 显示最近创建的容器
[root@VM-0-10-centos /]# docker ps -a -n=1
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                     PORTS     NAMES
02846c1c59a7   centos    "/bin/bash"   9 minutes ago   Exited (0) 5 minutes ago             mystifying_euclid
-q		# 只显示容器的编号
[root@VM-0-10-centos /]# docker ps -aq
02846c1c59a7
e9638e508e8c

4、退出容器

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

[root@VM-0-10-centos /]# docker run -it centos /bin/bash
[root@d024c5927244 /]# [root@VM-0-10-centos /]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
d024c5927244   centos    "/bin/bash"   36 seconds ago   Up 35 seconds

5、删除容器

docker rm 容器id					# 删除指定的容器,不能删除正在运行的容器,如果要强制删除 rm -f
[root@VM-0-10-centos /]# docker rm 02846c1c59a7
02846c1c59a7
docker rm -f $(docker ps -aq)		# 删除所有的容器
[root@VM-0-10-centos /]# docker ps -aq
d024c5927244
e9638e508e8c
[root@VM-0-10-centos /]# docker rm -f $(docker ps -aq)
d024c5927244
e9638e508e8c
[root@VM-0-10-centos /]# docker ps -aq
docker ps -a -q | xargs docker rm	# 删除所有的容器

6、启动和停止容器

docker start 容器id		# 启动容器
docker restart 容器id	# 重启容器
docker stop 容器id		# 停止当前正在运行的容器
docker kill 容器id		# 强制停止当前容器
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值