当前位置:   article > 正文

Docker基本命令

Docker基本命令

本文简单介绍docker的最基础的命令

目录

帮助命令

镜像命令

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


帮助命令

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

镜像命令

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

  1. [root@localhost ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. hello-world latest feb5d9fea6a5 2 months ago 13.3kB
  4. # 解释
  5. REPOSITORY 镜像的仓库源
  6. TAG 镜像的标签
  7. IMAGE ID 镜像的ID
  8. CREATED 镜像的创建时间
  9. SIZE 镜像的大小
  10. # 命令的可选参数
  11. [root@localhost ~]# docker images --help
  12. Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
  13. List images
  14. Options:
  15. -a, --all Show all images (default hides intermediate images) 列出所有的镜像
  16. --digests Show digests
  17. -f, --filter filter Filter output based on conditions provided
  18. --format string Pretty-print images using a Go template
  19. --no-trunc Don't truncate output
  20. -q, --quiet Only show image IDs 只显示镜像的ID

docker search 镜像搜索

  1. [root@localhost ~]# docker search mysql
  2. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  3. mysql MySQL is a widely used, open-source relation… 11741 [OK]
  4. mariadb MariaDB Server is a high performing open sou… 4476 [OK]
  5. mysql/mysql-server Optimized MySQL Server Docker images. Create… 875 [OK]
  6. phpmyadmin phpMyAdmin - A web interface for MySQL and M… 383 [OK]
  7. centos/mysql-57-centos7 MySQL 5.7 SQL database server 92
  8. mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 89
  9. centurylink/mysql Image containing mysql. Optimized to be link… 59 [OK]
  10. databack/mysql-backup Back up mysql databases to... anywhere! 53
  11. # 命令的可选参数
  12. [root@localhost ~]# docker search --help
  13. Usage: docker search [OPTIONS] TERM
  14. Search the Docker Hub for images
  15. Options:
  16. -f, --filter filter Filter output based on conditions provided
  17. --format string Pretty-print search using a Go template
  18. --limit int Max number of search results (default 25)
  19. --no-trunc Don't truncate output
  20. # 举例:搜索星标大于3000的mysql
  21. [root@localhost ~]# docker search mysql --filter=STARS=3000
  22. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  23. mysql MySQL is a widely used, open-source relation… 11741 [OK]
  24. mariadb MariaDB Server is a high performing open sou… 4476 [OK]

docker pull 下载镜像

  1. # 下载镜像 docker pull 镜像名[:tag]
  2. # 演示
  3. [root@localhost ~]# docker pull mysql
  4. Using default tag: latest // 不添加tag 默认下载最新的版本。
  5. latest: Pulling from library/mysql
  6. a10c77af2613: Pull complete // 分层下载, docker image的核心,联合文件系统
  7. b76a7eb51ffd: Pull complete
  8. 258223f927e4: Pull complete
  9. 2d2c75386df9: Pull complete
  10. 63e92e4046c9: Pull complete
  11. f5845c731544: Pull complete
  12. bd0401123a9b: Pull complete
  13. 3ef07ec35f1a: Pull complete
  14. c93a31315089: Pull complete
  15. 3349ed800d44: Pull complete
  16. 6d01857ca4c1: Pull complete
  17. 4cc13890eda8: Pull complete
  18. Digest: sha256:aeecae58035f3868bf4f00e5fc623630d8b438db9d05f4d8c6538deb14d4c31b // 数字签名
  19. Status: Downloaded newer image for mysql:latest
  20. docker.io/library/mysql:latest # 真实的地址
  21. # 即 docker pull mysql 等价于 docker pull docker.io/library/mysql:latest
  22. # 下载MySQL5.7 (下载的版本必须是已经发布镜像中包含的)
  23. [root@localhost ~]# docker pull mysql:5.7
  24. 5.7: Pulling from library/mysql
  25. a10c77af2613: Already exists # 因为分层下载的原因,可以共用文件。
  26. b76a7eb51ffd: Already exists
  27. 258223f927e4: Already exists
  28. 2d2c75386df9: Already exists
  29. 63e92e4046c9: Already exists
  30. f5845c731544: Already exists
  31. bd0401123a9b: Already exists
  32. 2724b2da64fd: Pull complete
  33. d10a7e9e325c: Pull complete
  34. 1c5fd9c3683d: Pull complete
  35. 2e35f83a12e9: Pull complete
  36. Digest: sha256:7a3a7b7a29e6fbff433c339fc52245435fa2c308586481f2f92ab1df239d6a29
  37. Status: Downloaded newer image for mysql:5.7
  38. docker.io/library/mysql:5.7
  39. # 查看现在的已经有的镜像
  40. [root@localhost ~]# docker images
  41. REPOSITORY TAG IMAGE ID CREATED SIZE
  42. mysql 5.7 8b43c6af2ad0 11 days ago 448MB
  43. mysql latest b05128b000dd 11 days ago 516MB
  44. hello-world latest feb5d9fea6a5 2 months ago 13.3kB

docker rmi 删除镜像

  1. # 删除MySQL5.7 一般使用镜像的ID删除
  2. [root@localhost ~]# docker rmi -f 8b43c6af2ad0
  3. Untagged: mysql:5.7
  4. Untagged: mysql@sha256:7a3a7b7a29e6fbff433c339fc52245435fa2c308586481f2f92ab1df239d6a29
  5. Deleted: sha256:8b43c6af2ad08d95cdcb415d245446909a6cbc1875604c48c4325972e5b00442
  6. Deleted: sha256:aad43f4d2f66438acd2d156216cd544a728851238714975c38d9a690f68afc57
  7. Deleted: sha256:7b9addbc002c1e828aee7ec5c2679b04a591b6fa2b96002701ddee9d4ed54395
  8. Deleted: sha256:b00f8e4e6ce8920fb563615503f232799ab380b338c3f2cbb5e86a2d762a6e80
  9. Deleted: sha256:8fbabb17fd7b46a59cc15301741bf73a527b862f59cc6e84fae15b4dd5c425c0
  10. # 按镜像的ID删除所有的镜像
  11. # 格式 docker rmi -f 镜像ID1 镜像ID2 ...
  12. [root@localhost ~]# docker rmi -f $(docker images -aq)
  13. Untagged: mysql:latest
  14. Untagged: mysql@sha256:aeecae58035f3868bf4f00e5fc623630d8b438db9d05f4d8c6538deb14d4c31b
  15. Deleted: sha256:b05128b000ddbafb0a0d2713086c6a1cc23280dee3529d37f03c98c97c8cf1ed
  16. Deleted: sha256:2920230e18d6833c32c9f851905df9d3e2958a43b771c84908234ac031b25a45
  17. Deleted: sha256:a790dd6a368bc9aa7d1b251b46ac2fc718ebae5a38ed51ff89ff99955dadaa35
  18. Deleted: sha256:cd87c1db4b159f37f092e73a52c10d5ccb837ed7bfcdc3b008038540390454a4
  19. Deleted: sha256:7f92300b04af4aef96d5ef6fab1e27456cef354eca04733d396ad74478bee7d8
  20. Deleted: sha256:6a59f55eb4945598b4889ea269d79f8b99563c96e97ba2373e19712732d20352
  21. Deleted: sha256:87030c256d8077b4d969e5819f5da01ed08f29e115eaec61b58b3f3134175e1e
  22. Deleted: sha256:b1694d0bb0b1be63e940478b93aa34f46e18f8371539ccee3b5d580cbf576812
  23. Deleted: sha256:f323fd0baccb89f82a5711fa6291f3b4c977b85c3bbba59b1080205b498133b1
  24. Deleted: sha256:47a2799e90faa9d9aaaa4b63457390dcbf5b26ce67f0926821c50b982d741e32
  25. Deleted: sha256:156f55d34ef3e567ef39380f8d86f7c946927a099a43205de8721e60bfef526e
  26. Deleted: sha256:bb282bb84eb90a6040504a46f462ebe55cb9623df13219fc39f434a53ccd1687
  27. Deleted: sha256:77b323d4ec74aad770337f99a60e862a64ccc53f4775b5f4945df0e606f78b90
  28. Untagged: hello-world:latest
  29. Untagged: hello-world@sha256:cc15c5b292d8525effc0f89cb299f1804f3a725c8d05e158653a563f15e4f685
  30. Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412

容器命令

有了镜像才能创建容器
下载一个linux进行学习`docker pull centos`

  1. [root@localhost ~]# docker pull centos
  2. Using default tag: latest
  3. latest: Pulling from library/centos
  4. a1d0c7532777: Pull complete
  5. Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
  6. Status: Downloaded newer image for centos:latest
  7. docker.io/library/centos:latest

新建容器并启动

  1. docker run [可选参数] image
  2. # 参数说明
  3. --name="name" 容器名字 tomcat01 tomcat02 用来区分容器
  4. -d 后台方式运行
  5. -it 使用交互方式运行,进入容器查看内容
  6. -p 指定容器的端口 -p 8080:8080
  7. -p ip:主机端口:容器端口
  8. -p 主机端口:容器端口(常用)
  9. -p 容器端口
  10. -P 随机制定端口
  11. # 测试运行, 启动并且进入容器
  12. [root@localhost ~]# docker run -it centos /bin/bash
  13. # 进入容器中后,root@ 后面的提示符变成了镜像的ID
  14. [root@b2bc9c3ce6fe /]# ls # 查看的是容器内的文件
  15. bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
  16. # 从容器中退回主机
  17. [root@b2bc9c3ce6fe /]# exit
  18. exit

列出所有的运行的容器

  1. # docker ps 命令 列出当前正在运行的容器
  2. -a # 列出当前正在运行的容器 + 历史运行过的容器
  3. -n=? # 显示最近创建的容器
  4. -q # 只显示容器的编号
  5. [root@localhost ~]# docker ps
  6. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  7. [root@localhost ~]# docker ps -a
  8. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  9. b2bc9c3ce6fe centos "/bin/bash" 3 minutes ago Exited (0) About a minute ago determined_diffie
  10. 0e3286c60dfb feb5d9fea6a5 "/hello" 11 days ago Exited (0) 11 days ago trusting_beaver

退出容器

  1. exit # 直接退出容器并停止运行
  2. Ctrl + P + Q # 容器不停止并且退出

删除容器

  1. docker rm 容器id # 删除指定的容器,不能删除正在运行的容器。如果要强制删除 rm -f
  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 # 强制停止当前的容器

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/341921
推荐阅读
相关标签
  

闽ICP备14008679号