狂神说-Docker基础-学习笔记-02 Docker常用命令
1、帮助命令
docker version #查看版本信息
docker info #显示docker的系统信息
docker `命令` --help #最常用、查看某命令的使用方法
2、镜像命令
docker images #查看系统中已有的镜像
C:Usersz>docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 938b57d64674 3 weeks ago 448MB
mysql latest ecac195d15af 3 weeks ago 516MB
# REPOSITORY(镜像的仓库源) TAG(标签,也就是版本) IMAGE ID(镜像ID) CREATED(创建时间) SIZE(镜像大小)
#命令的可选项
-a, --all # 列出所有镜像
-f, --filter filter # Filter output base on confitions provided
--format string # Pretty-print images using a Go template
--no-trunc # Don't truncate output
-q, --quit # only show numeric IDS
docker search
查找镜像
# docker search 基本使用方法
C:Usersz>docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11676 [OK]
mariadb MariaDB Server is a high performing open sou… 4450 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 867 [OK]
percona Percona Server is a fork of the MySQL relati… 561 [OK]
C:Usersz>docker search mysql --filter=STARS=4000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11676 [OK]
mariadb MariaDB Server is a high performing open sou… 4450 [OK]
# docker search `imageName` --filter=`过滤条件`
C:Usersz>docker search mysql --filter=stars=4000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11676 [OK]
mariadb MariaDB Server is a high performing open sou… 4450 [OK]
docker pull
下载镜像
C:Usersz>docker pull mysql
Using default tag: latest # 默认下载最新版本镜像
latest: Pulling from library/mysql
……
Digest: sha256:6d7d4524463fe6e2b893ffc2b89543c81dec7ef82fb2020a1b27606666464d87
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
C:Usersz>docker pull mysql:5.7 # 下载指定版本镜像(要先确定这个版本官方镜像是否支持)
5.7: Pulling from library/mysql
……
通过观察可以发现,对于不同版本的mysql的下载是通过分片文件的形式实现的,已经存在的有效模块会被复用不会重新下载,这是docker的设计精髓。
docker rmi
删除镜像
docker rmi -f 镜像ID
# 删除全部镜像 使用$命令引用的方式(适用于Linux系统中)
docker rmi -f $(docker imges -aq)
C:Usersz>docker rmi -f feb5d9fea6a5
Untagged: hello-world:latest
Untagged: hello-world@sha256:cc15c5b292d8525effc0f89cb299f1804f3a725c8d05e158653a563f15e4f685
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
C:Usersz>docker rmi -f 938b57d64674 ecac195d15af
Untagged: mysql:5.7
Untagged: mysql@sha256:2db8bfd2656b51ded5d938abcded8d32ec6181a9eae8dfc7ddf87a656ef97e97
Deleted: sha256:938b57d64674c4a123bf8bed384e5e057be77db934303b3023d9be331398b761
Deleted: sha256:d81fc74bcfc422d67d8507aa0688160bc4ca6515e0a1c8edcdb54f89a0376ff1
Deleted: sha256:a6a530ba6d8591630a1325b53ef2404b8ab593a0775441b716ac4175c14463e6
Deleted: sha256:2a503984330e2cec317bc2ef793f5d4d7b3fd8d50009a4f673026c3195460200
Deleted: sha256:e2a4585c625da1cf4909cdf89b8433dd89ed5c90ebdb3a979d068b161513de90
Untagged: mysql:latest
Untagged: mysql@sha256:6d7d4524463fe6e2b893ffc2b89543c81dec7ef82fb2020a1b27606666464d87
Deleted: sha256:ecac195d15afac2335de52fd7a0e34202fe582731963d31830f1b97700bf9509
Deleted: sha256:451fe04d80b84c0b7aca0f0bbdaa5de7c7ac85a65389ed5d3ed492f63ac092e2
Deleted: sha256:814cbf8bc7f6bb85685e5b803e16a76406c30d1960c566eee76303ffac600600
Deleted: sha256:735f72e1d1b936bb641b6a1283e4e60bf10a0c36f8244a5e3f8c7d58fa95b98a
Deleted: sha256:f2d209a30c3950fadffb2d82e1faa434da0753bee7aacad9cdec7d8a7a83df37
Deleted: sha256:03b9f8c5331d9534d2372a144bcffc8402e5f7972c9e4b85c634bef203ec6d20
Deleted: sha256:80f5487a88b8061855e99782979ed6069a8dd1c7dfbb1eb63fe42a4a9d119436
Deleted: sha256:f791a6c727931d41c51f8bf24ee32a4dbf0169f372b174f1ff89b4836b97c48e
Deleted: sha256:4c88df098412e11a98936509f3cede57f87154b350b0f75d96713f6e1dd56101
Deleted: sha256:fdba3a2cd286d9a5f65fc00f5254048855ae7dc00f3b3fa3356981eb9a7fe6d0
Deleted: sha256:8b3a69042e0da82429d28be0c474e73290ba4908730de22b2200a7aac9b245bd
Deleted: sha256:90afe56a0643f5bf1b1e8ee147b40a8e12b3fdd7e26bc2d2c50180d68dd524d0
Deleted: sha256:e81bff2725dbc0bf2003db10272fef362e882eb96353055778a66cda430cf81b
# 删除全部镜像的 命令 $ 命令引用方式在 win10 无效
C:Usersz>docker rmi -f $(docker imges -aq)
unknown shorthand flag: 'a' in -aq)
See 'docker rmi --help'.
3、容器命令
有了镜像才能创建容器
docker pull centos
新建容器并启动
docker run [可选参数] image
# 参数说明
--name Name #容器名字 可以通过一个镜像来运行多个容器 例如使用 Tomcat01 Tomcat02 进行区分
-d # 后台方式运行
-i # 保持标准 输入方式打开
-t # 分配给容器一个 为终端
-it # -t 和 -i 一起使用 就会以可交互的方式运行,并进入容器查看内容
-p
-p ip:主机port:容器port
-ip 本主机port:容器port
-p 容器port
容器port(不使用 -p 参数 也可以直接指定 容器运行的容器端口)
-P # 随机指定端口
列出所有运行的容器
docker ps # 列出当前正在运行的容器
-a # 列出当前正在运行的容器 + 带出历史运行过的容器
-n=? # 显示最近创建的 前 n 个容器
-q # 只展示 容器的编号
退出容器
exit # 直接退出同时停止容器
ctrl + P + Q # 退出容器但不停止
删除容器
docker rm 容器id # 删除指定的容器,不能删除正在运行的容器, 如果要强制删除 rm -f
docker rm -f $(docker ps -aq) # 删除所有容器 (适用于Linux系统,windows 无效)
docker pa -a -q|xargs docker rm # 删除所有容器 (适用于Linux系统,windows 无效)
启动和停止容器
docker start 容器id
docker restart 容器id
docker stop 容器id
docker kill 容器id #强行停止当前容器
操作过程
C:Usersz>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 8 weeks ago 231MB
# 启动并进入centos容器 []
C:Usersz>docker run -it centos /bin/bash
[root@956311a69f5a /]# ls # 查看容器内的 centOS,这个镜像知识基础版本,很多命令都是不完善的
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@956311a69f5a /]# exit
exit
# exit 命令会直接 将容器强行关闭
#docker ps 查看正在运行的容器
C:Usersz>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
C:Usersz>docker ps -a #docker ps -a 查看所有容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
956311a69f5a centos "/bin/bash" 2 minutes ago Exited (0) 56 seconds ago affectionate_banzai
018dd3ae456f feb5d9fea6a5 "/hello" 2 hours ago Exited (0) 2 hours ago happy_wilbur
C:Usersz>docker ps -a -n=1 #参数 [-n=x] 对容器显示的数量进行限定
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
956311a69f5a centos "/bin/bash" 3 minutes ago Exited (0) About a minute ago affectionate_banzai
C:Usersz>docker run -it centos /bin/bash
[root@d7c733c12c36 /]#
C:Usersz>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d7c733c12c36 centos "/bin/bash" 30 seconds ago Up 29 seconds dreamy_galileo
C:Usersz>docker run --name centos01 -it centos
[root@21a52707ecb7 /]#
# ! 此处使用 ctrl + P + Q 快捷方式 以不终止后台运行的方式 返回到宿主机的命令行
C:Usersz>docker ps # 通过一个镜像可以运行多个容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
21a52707ecb7 centos "/bin/bash" 16 seconds ago Up 14 seconds centos01
d7c733c12c36 centos "/bin/bash" 10 minutes ago Up 10 minutes dreamy_galileo
C:Usersz>docker rm d7c733c12c36 # 不能直接删除正在运行的容器
Error response from daemon: You cannot remove a running container d7c733c12c36d7806ab103fb374e43cbae5fb224ea7ec1d68c80d86e3171ebb6. Stop the container before attempting removal or force remove
C:Usersz>docker rm -f d7c733c12c36 # [-f] force 强制删除容器
d7c733c12c36
C:Usersz>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
21a52707ecb7 centos "/bin/bash" About a minute ago Up About a minute centos01
C:Usersz>docker rm -f $(docker ps -aq) # 在win10中这个命令不起作用
unknown shorthand flag: 'a' in -aq)
See 'docker rm --help'.
C:Usersz>docker ps -a -q|xargs docker rm # 在win10中这个命令不起作用
'xargs' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
C:Usersz>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
21a52707ecb7 centos "/bin/bash" 6 minutes ago Up 6 minutes centos01
C:Usersz>docker stop 21a52707ecb7
21a52707ecb7
C:Usersz>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
C:Usersz>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
21a52707ecb7 centos "/bin/bash" 8 minutes ago Exited (0) 8 seconds ago centos01
C:Usersz>docker start 21a52707ecb7
21a52707ecb7
C:Usersz>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
21a52707ecb7 centos "/bin/bash" 9 minutes ago Up 25 seconds centos01