3、Docker常用命令

============================
\color{#4285f4}{K}\color{#ea4335}{a}\color{#fbbc05}{r}\color{#4285f4}{y}\color{#34a853}{开}\color{#ea4335}{源}
============================

帮助命令

docker version  # 显示docker的版本信息
docker info        #显示docker的系统信息
docker --help    #万能命令

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

镜像命令

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

docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        4 months ago        13.3kB

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

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

docker search 搜索镜像

docker search mysql

#可选项 、 通过搜索收藏来过滤
docker search mysql -- filter=STARS=3000 搜索出来的镜像就是stars大于3000的
image.png

docker pull 下载镜像

# 下载镜像 docker pull 镜像名[:tag]
[root@izuf68ul8fasmt5nd7o6exz /]# docker pull mysql
Using default tag: latest  #如果不写版本号 默认就是最新版
latest: Pulling from library/mysql
afb6ec6fdc1c: Pull complete   #分层下载 docker image的核心 联合文件系统
0bdc5971ba40: Pull complete 
97ae94a2c729: Pull complete 
f777521d340e: Pull complete 
1393ff7fc871: Pull complete 
a499b89994d9: Pull complete 
7ebe8eefbafe: Pull complete 
597069368ef1: Pull complete 
ce39a5501878: Pull complete 
7d545bca14bf: Pull complete 
0f5f78cccacb: Pull complete 
623a5dae2b42: Pull complete 
Digest: sha256:beba993cc5720da07129078d13441745c02560a2a0181071143e599ad9c497fa
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真实地址

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

# 指定版本下载docker pull mysql :5.7
[root@izuf68ul8fasmt5nd7o6exz /]# docker pull mysql:5.7
5.7: Pulling from library/mysql
afb6ec6fdc1c: Already exists 
0bdc5971ba40: Already exists 
97ae94a2c729: Already exists 
f777521d340e: Already exists 
1393ff7fc871: Already exists 
a499b89994d9: Already exists 
7ebe8eefbafe: Already exists 
4eec965ae405: Pull complete 
a531a782d709: Pull complete 
10e94c02b508: Pull complete 
799a94b968ef: Pull complete 
Digest: sha256:5c9fd7949bc0f076429fa2c40d0e7406e095bdb5216a923257b31972a6f3ae4f
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

image.png

卸载镜像
docker rmi

#根据id删除镜像
 docker rmi -f 镜像id  

#根据id删除多个镜像
 docker rmi -f 镜像id     docker rmi -f 镜像id    docker rmi -f 镜像id  

#递归删除所有镜像
docker rmi -f $(docker images -aq)  # 删除全部的镜像

容器命令

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

docker pull centos

新建容器并启动

docker run [可选参数] image

#参数说明
--name "Name" 容器名字 tomcat01  tomcat02 ,用来区分容器
-d   后台方式运行 
-it   使用交互方式运行
-p  指定容器的端口  -p 8080:8080
      -p ip:主机端口:容器端口
      -p 主机端口:容器端口(常用)
      -p 容器端口
        容器端口
-P  随机指定端口


# 测试  启动并进入容器
[root@izuf68ul8fasmt5nd7o6exz /]# docker run -it centos
#查看容器内的centos
[root@b7c0294302ad /]# ls
bin  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr

#退出镜像内的centos
exit

列出所有运行中的容器

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

[root@izuf68ul8fasmt5nd7o6exz /]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATU
S              PORTS               NAMES
[root@izuf68ul8fasmt5nd7o6exz /]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                          PORTS               NAMES
b7c0294302ad        centos              "/bin/bash"         6 minutes ago       Exited (0) About a minute ago                       loving_roentgen
5e51e4a00c2d        hello-world         "/hello"            6 hours ago         Exited (0) 6 hours ago                              vibrant_liskov

退出容器

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

删除容器

docker rm  容器id   #删除指定的容器 ,不能删除正在运行的容器  如果要强制删除  rm -f
docker rm   -f  $(docker ps -aq)   #递归删除

启动和停止容器的操作

docker start       容器id #启动容器
docker restart   容器id #重启容器
docker stop       容器id #停止容器
docker kill          容器id# 杀死容器

关注kari自建博客站
http://blog.petchk.cn/

关注kari的git博客https://chenhuakai.github.io/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。