基本命令

docker 版本信息:docker version

  1. [root@iZ2ze9n36uysrbp267t19fZ ~]# docker version
  2. Client: Docker Engine - Community
  3. Version: 19.03.14
  4. API version: 1.40
  5. Go version: go1.13.15
  6. Git commit: 5eb3275d40
  7. Built: Tue Dec 1 19:20:42 2020
  8. OS/Arch: linux/amd64
  9. Experimental: false
  10. Server: Docker Engine - Community
  11. Engine:
  12. Version: 19.03.14
  13. API version: 1.40 (minimum version 1.12)
  14. Go version: go1.13.15
  15. Git commit: 5eb3275d40
  16. Built: Tue Dec 1 19:19:17 2020
  17. OS/Arch: linux/amd64
  18. Experimental: false
  19. containerd:
  20. Version: 1.2.6
  21. GitCommit: 894b81a4b802e4eb2a91d1ce216b8817763c29fb
  22. runc:
  23. Version: 1.0.0-rc8
  24. GitCommit: 425e105d5a03fabd737a126ad93d62a9eeede87f
  25. docker-init:
  26. Version: 0.18.0
  27. GitCommit: fec3683

Docker 系统信息: docker info

  1. [root@iZ2ze9n36uysrbp267t19fZ ~]# docker info
  2. Client:
  3. Debug Mode: false
  4. Server:
  5. Containers: 6
  6. Running: 0
  7. Paused: 0
  8. Stopped: 6
  9. Images: 4
  10. Server Version: 19.03.14
  11. Storage Driver: overlay2
  12. Backing Filesystem: xfs
  13. Supports d_type: true
  14. Native Overlay Diff: true
  15. Logging Driver: json-file
  16. Cgroup Driver: cgroupfs
  17. Plugins:
  18. Volume: local
  19. Network: bridge host ipvlan macvlan null overlay
  20. Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
  21. Swarm: inactive
  22. Runtimes: runc
  23. Default Runtime: runc
  24. Init Binary: docker-init
  25. containerd version: 894b81a4b802e4eb2a91d1ce216b8817763c29fb
  26. runc version: 425e105d5a03fabd737a126ad93d62a9eeede87f
  27. init version: fec3683
  28. Security Options:
  29. seccomp
  30. Profile: default
  31. Kernel Version: 4.18.0-147.8.1.el8_1.x86_64
  32. Operating System: CentOS Linux 8 (Core)
  33. OSType: linux
  34. Architecture: x86_64
  35. CPUs: 2
  36. Total Memory: 3.552GiB
  37. Name: iZ2ze9n36uysrbp267t19fZ
  38. ID: NX3M:OJTA:OXEY:CM3C:YFUZ:S4CA:2ET4:5NVS:AEH5:DR7F:MXHL:3VL5
  39. Docker Root Dir: /var/lib/docker
  40. Debug Mode: false
  41. Registry: https://index.docker.io/v1/
  42. Labels:
  43. Experimental: false
  44. Insecure Registries:
  45. 127.0.0.0/8
  46. Registry Mirrors:
  47. https://numn9rsu.mirror.aliyuncs.com/
  48. Live Restore Enabled: false

镜像管理

查看所有镜像:dockers images

  1. [root@iZ2ze9n36uysrbp267t19fZ ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. nginx latest ae2feff98a0c 4 months ago 133MB
  4. atlassian/jira-software latest 50be61a3920f 4 months ago 808MB
  5. idoop/zentao latest f28461be398f 9 months ago 170MB
  6. k4yt3x/video2x latest 3033b136074e 9 months ago 6.69GB

搜索所有镜像:docker search busybox

  1. [root@iZ2ze9n36uysrbp267t19fZ ~]# docker search nginx
  2. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  3. nginx Official build of Nginx. 14752 [OK]
  4. jwilder/nginx-proxy Automated Nginx reverse proxy for docker con 2018 [OK]
  5. richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of 813 [OK]
  6. jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho 181
  7. linuxserver/nginx An Nginx container, brought to you by LinuxS 142
  8. tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp 122 [OK]
  9. jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 104 [OK]
  10. bitnami/nginx Bitnami nginx Docker Image 96 [OK]
  11. alfg/nginx-rtmp NGINX, nginx-rtmp-module and FFmpeg from sou 91 [OK]
  12. jasonrivers/nginx-rtmp Docker images to host RTMP streams using NGI 89 [OK]
  13. nginxdemos/hello NGINX webserver that serves a simple page co 67 [OK]
  14. privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al 53 [OK]

拉取下载:docker pull busybox:latest

  1. [root@iZ2ze9n36uysrbp267t19fZ ~]# docker pull nginx
  2. Using default tag: latest
  3. latest: Pulling from library/nginx
  4. f7ec5a41d630: Pull complete
  5. aa1efa14b3bf: Pull complete
  6. b78b95af9b17: Pull complete
  7. c7d6bca2b8dc: Pull complete
  8. cf16cd8e71e0: Pull complete
  9. 0241c68333ef: Pull complete
  10. Digest: sha256:75a55d33ecc73c2a242450a9f1cc858499d468f077ea942867e662c247b5e412
  11. Status: Downloaded newer image for nginx:latest
  12. docker.io/library/nginx:latest
  13. # 下载指定版本
  14. [root@iZ2ze9n36uysrbp267t19fZ ~]# docker pull nginx:1.17.0
  15. 1.17.0: Pulling from library/nginx
  16. fc7181108d40: Pull complete
  17. c4277fc40ec2: Pull complete
  18. 780053e98559: Pull complete
  19. Digest: sha256:bdbf36b7f1f77ffe7bd2a32e59235dff6ecf131e3b6b5b96061c652f30685f3a
  20. Status: Downloaded newer image for nginx:1.17.0

导出:docker save busybox > busybox.tar

  1. [root@iZ2ze9n36uysrbp267t19fZ tmp]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. nginx 1.17.0 719cd2e3ed04 22 months ago 109MB
  4. [root@iZ2ze9n36uysrbp267t19fZ tmp]# docker save nginx:1.17.0 > nginx:1.17.0.tar
  5. [root@iZ2ze9n36uysrbp267t19fZ tmp]# ls
  6. nginx:1.17.0.tar

导入:docker load < busybox.tar

  1. [root@iZ2ze9n36uysrbp267t19fZ tmp]# docker load < nginx\:1.17.0.tar
  2. cf5b3c6798f7: Loading layer 58.45MB/58.45MB
  3. d9569ca04881: Loading layer 54.52MB/54.52MB
  4. d7acf794921f: Loading layer 3.584kB/3.584kB
  5. Loaded image: nginx:1.17.0
  6. [root@iZ2ze9n36uysrbp267t19fZ tmp]# docker images
  7. REPOSITORY TAG IMAGE ID CREATED SIZE
  8. nginx 1.17.0 719cd2e3ed04 22 months ago 109MB

删除:docker rmi busybox:latest

  1. [root@iZ2ze9n36uysrbp267t19fZ tmp]# docker rmi 7
  2. Untagged: nginx:1.17.0
  3. Deleted: sha256:719cd2e3ed04781b11ed372ec8d712fac66d5b60a6fb6190bf76b7d18cb50105
  4. Deleted: sha256:e9b6506fb887de50972aefd99d7c5eb56b1a8e757ed953cdfecb86b5359bcb22
  5. Deleted: sha256:55d9d9692a9615a28d183a42bc3881a72a39393feba3664e669e7affb78daa76
  6. Deleted: sha256:cf5b3c6798f77b1f78bf4e297b27cfa5b6caa982f04caeb5de7d13c255fd7a1e

更改镜像名:docker tag busybox:latest busybox:test

  1. [root@iZ2ze9n36uysrbp267t19fZ tmp]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. nginx 1.17.10 9beeba249f3e 11 months ago 127MB
  4. [root@iZ2ze9n36uysrbp267t19fZ tmp]# docker tag nginx:1.17.10 nginx:test
  5. [root@iZ2ze9n36uysrbp267t19fZ tmp]# docker images
  6. REPOSITORY TAG IMAGE ID CREATED SIZE
  7. nginx 1.17.10 9beeba249f3e 11 months ago 127MB
  8. nginx test 9beeba249f3e 11 months ago 127MB

查看镜像创建历史:docker history busybox

  1. [root@iZ2ze9n36uysrbp267t19fZ tmp]# docker history nginx:1.17.10

容器管理

运行容器:docker run -d —name=busybox busybox:latest ping 114.114.114.114

查看运行的容器:docke ps, docker ps -a

查看容器中运行的进程:docker top busybox

查看容器资源占用:docker stats busybox

容器:docker start/restart/stop/kill busybox

暂停容器:docker pause/unpause busybox

强制删除容器:docker rm -f busybox

执行命令:docker exec -it busybox ls

复制文件:docker cp busybox:/etc/hosts hosts

查看容器日志:docker logs -f busybox

查看容器/镜像的元信息:docker inspect busybox

格式化输出:docker inspect -f ‘{{.Id}}’busybox

Inspect 语法参考

查看容器内文件结构:docker diff busybox

  1. [root@iZ2ze9n36uysrbp267t19fZ tmp]# docker run -d --name=nginx nginx:1.17.10
  2. fa0e60f3ea9946f22b9ccd39f191d46296d0a064f6b76a8f684f65d1036b9ec1
  3. [root@iZ2ze9n36uysrbp267t19fZ tmp]# docker ps -a
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. fa0e60f3ea99 nginx:1.17.10 "nginx -g 'daemon of…" 8 seconds ago Up 8 seconds 80/tcp nginx
  6. [root@iZ2ze9n36uysrbp267t19fZ tmp]# docker diff nginx:1.17.10
  7. Error response from daemon: No such container: nginx:1.17.10
  8. [root@iZ2ze9n36uysrbp267t19fZ tmp]# docker diff nginx
  9. C /var
  10. C /var/cache
  11. C /var/cache/nginx
  12. A /var/cache/nginx/client_temp
  13. A /var/cache/nginx/fastcgi_temp
  14. A /var/cache/nginx/proxy_temp
  15. A /var/cache/nginx/scgi_temp
  16. A /var/cache/nginx/uwsgi_temp
  17. C /run