官网地址:https://docs.docker.com/engine/reference

1. 帮助命令

  1. docker version #查看版本信息
  2. docker info #查看具体的docker信息
  3. docker --help #帮助命令

2. 镜像命令

docker images

  1. docker images #查看本地所有镜像
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. hello-world latest bf756fb1ae65 4 months ago 13.3kB
  4. #解释:
  5. REPOSITORY 镜像的仓库员
  6. TAG 镜像的标签
  7. IMAGE ID 镜像的id
  8. CREATED 创建的时间
  9. SIZE 镜像的大小
  10. #可选项
  11. -a,--all #列出所有的镜像
  12. -q,--quiet #只显示镜像的id

docker search

  1. docker search #搜索镜像
  2. [root@wjh ~]# docker search mysql
  3. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  4. mysql MySQL is a widely used, open-source relation 9525 [OK]
  5. mariadb MariaDB is a community-developed fork of MyS 3457 [OK]
  6. mysql/mysql-server Optimized MySQL Server Docker images. Create 698 [OK]
  7. #可选项 用来做搜索过滤
  8. --filter=STARS=3000 #搜索出来的镜像就是STARS大于3000的
  9. [root@wjh ~]# docker search mysql --filter=STARS=3000
  10. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  11. mysql MySQL is a widely used, open-source relation 9525 [OK]
  12. mariadb MariaDB is a community-developed fork of MyS 3457 [OK]

docker pull

  1. docker pull 镜像名[:tag] #从仓库里下载镜像
  2. [root@wjh ~]# docker pull mysql
  3. Using default tag: latest #如果不写tag,默认就是最新的
  4. latest: Pulling from library/mysql
  5. afb6ec6fdc1c: Pull complete #分层下载,docker image的核心 联合文件系统
  6. 0bdc5971ba40: Pull complete
  7. 97ae94a2c729: Pull complete
  8. f777521d340e: Pull complete
  9. 1393ff7fc871: Pull complete
  10. a499b89994d9: Pull complete
  11. 7ebe8eefbafe: Pull complete
  12. 597069368ef1: Pull complete
  13. ce39a5501878: Pull complete
  14. 7d545bca14bf: Pull complete
  15. 211e5bb2ae7b: Pull complete
  16. 5914e537c077: Pull complete
  17. Digest: sha256:a31a277d8d39450220c722c1302a345c84206e7fd4cdb619e7face046e89031d #签名
  18. Status: Downloaded newer image for mysql:latest
  19. docker.io/library/mysql:latest #真是地址
  20. #等价于
  21. docker pull mysql = docker pull docker.io/library/mysql:latest
  22. #指定版本下载
  23. [root@wjh ~]# docker pull mysql:5.7
  24. 5.7: Pulling from library/mysql
  25. afb6ec6fdc1c: Already exists
  26. 0bdc5971ba40: Already exists
  27. 97ae94a2c729: Already exists
  28. f777521d340e: Already exists
  29. 1393ff7fc871: Already exists
  30. a499b89994d9: Already exists
  31. 7ebe8eefbafe: Already exists
  32. 4eec965ae405: Pull complete
  33. a531a782d709: Pull complete
  34. 270aeddb45e3: Pull complete
  35. b25569b61008: Pull complete
  36. Digest: sha256:d16d9ef7a4ecb29efcd1ba46d5a82bda3c28bd18c0f1e3b86ba54816211e1ac4
  37. Status: Downloaded newer image for mysql:5.7
  38. docker.io/library/mysql:5.7

image.png

docker rmi

  1. #docker rmi 删除镜像
  2. [root@wjh ~]# docker rmi -f 镜像id #删除指定的容器
  3. [root@wjh ~]# docker rmi -f 镜像id 镜像id 镜像id #删除多个容器
  4. [root@wjh ~]# docker rmi -f $(docker images -aq) #删除全部容器

3. 容器命令

有了镜像才可以创建容器,linux,下载一个centos镜像来测试学习。

  1. docker pull centos

docker run 新建容器并启动

  1. docker run [可选参数] image
  2. --name="Name" 容器名字 tomcat01 tomcat02...
  3. -d 后台方式运行
  4. -it 使用交互方式运行,进入容器查看内容
  5. -P 指定容器的端口 -p 也可以将容器端口映射起来 8080:8080
  6. -p ip:主机端口:容器端口
  7. -p 主机端口:容器端口(常用)
  8. -p 容器端口
  9. 容器端口
  10. -p 随机指定端口
  11. #测试,启动并进入容器
  12. [root@wjh ~]# docker run -it centos /bin/bash
  13. [root@55ffa71dadf4 /]# ls #容器内的centos,基础版本,很多命令都是不完善的
  14. bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
  15. #从容器中退回主机
  16. [root@55ffa71dadf4 /]# exit

docker ps 列出所有运行的容器

  1. #docker ps [可选参数] 命令
  2. #列出当前正在运行的容器
  3. -a #列出当前正在运行的容器 + 历史运行过的容器
  4. -n=? #显示最近创建的容器
  5. -q #只显示容器的编号
  6. #测试
  7. [root@wjh ~]# docker ps
  8. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  9. [root@wjh ~]# docker ps -a
  10. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  11. 55ffa71dadf4 centos "/bin/bash" 14 minutes ago Exited (0) 7 minutes ago reverent_banzai
  12. 030307ed9305 hello-world "/hello" 25 hours ago Exited (0) 25 hours ago eager_shtern
  13. a2e2a6ba153b hello-world "/hello" 26 hours ago Exited (0) 26 hours ago blissful_mirzakhani
  14. [root@wjh ~]# docker ps -n=1
  15. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  16. 55ffa71dadf4 centos "/bin/bash" 14 minutes ago Exited (0) 8 minutes ago reverent_banzai
  17. [root@wjh ~]# docker ps -q
  18. [root@wjh ~]# docker ps -aq
  19. 55ffa71dadf4
  20. 030307ed9305
  21. a2e2a6ba153b

exit 退出容器

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

docker rm 删除容器

  1. docker rm #删除指定的容器 不能删除正在运行的容器 rm -rf
  2. docker rm -f $(docker ps -aq) #删除所有的容器
  3. docker ps -a -q | xargs docker rm #删除所有的容器

启动和停止的容器操作

  1. docker start 容器id #启动容器
  2. docker restart 容器id #重启容器
  3. docker stop 容器id #停止当前正在运行的容器
  4. docker kill 容器id #强制停止当前的容器

后台启动容器

  1. docker run -d 镜像名
  2. [root@wjh ~]# docker run -d centos
  3. e629db38f58300f10d333fb80e22b30f7511dbd2fb1f6aa05e3efc805d77c1b8
  4. [root@wjh ~]# docker ps
  5. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  6. #问题: 发现后台启动的进程又停止了
  7. #原因: 容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止

查看日志

  1. docker logs
  2. #显示日志
  3. -tf #显示日志
  4. --tail number #要显示日志条数
  5. #常用命令:
  6. docker logs -tf --tail number 镜像名

查看容器中进程信息

  1. docker top 容器id

查看镜像的元数据

  1. docker inspect 容器id
  2. #可以看见有关容器的详情信息

进入当前正在运行的容器

  1. docker exec -it 容器id bashShell #如:docker exec -it 470671670cac /bin/bash
  2. docker attach 容器id #如:docker attach 470671670cac
  3. #两个区别:
  4. #docker exec #进入容器后,开启一个新的终端,可以在里面操作(常用)
  5. #docker attach #进入容器正在执行的终端,不会开启新的进程

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

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