Docker Image 命令详解

1、Docker 版本和信息查看

[root@Tang ~]# docker info
Client:
 Debug Mode: false

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 2
 Server Version: 19.03.5
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Native Overlay Diff: true
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: b34a5c8af56e510852c35414db4c1f4fa6172339
 runc version: 3e425f80a8c931f88e6d94a8c831b9d5aa481657
 init version: fec3683
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 3.10.0-957.el7.x86_64
 Operating System: CentOS Linux 7 (Core)
 OSType: linux
 Architecture: x86_64
 CPUs: 4
 Total Memory: 7.556GiB
 Name: Tang
 ID: ZTPM:GZBM:YJT6:4QY7:DW56:75VC:HLEO:3UDF:XINY:RRQM:LYDK:K7OV
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Registry Mirrors:
  https://registry.docker-cn.com/
  https://pz9**4bz.mirror.aliyuncs.com/
 Live Restore Enabled: false

WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled

2、Docker Image Help Command 帮助信息查看

Docker Image 官方帮助手册

2.1 Docker Image 帮助查看

[root@Tang ~]# docker image help

Usage:	docker image COMMAND

Manage images

Commands:
  build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

Run 'docker image COMMAND --help' for more information on a command.

2.2 Docker Image COMMAND 子命令帮助查看

[root@Tang ~]# docker image build --help
[root@Tang ~]# docker image ls --help
... ...

3、Docker Image Pull 下载镜像

[root@Tang ~]# docker image pull --help

Usage:	docker image pull [OPTIONS] NAME[:TAG|@DIGEST]

Pull an image or a repository from a registry

Options:
  -a, --all-tags                Download all tagged images in the repository
      --disable-content-trust   Skip image verification (default true)
  -q, --quiet                   Suppress verbose output
[root@Tang ~]# docker image pull centos:7        # 下载 centos:7 镜像
7: Pulling from library/centos
ab5ef0e58194: Pull complete 
Digest: sha256:4a701376d03f6b39b8c2a8f4a8e499441b0d567f9ab9d58e4991de4472fb813c
Status: Downloaded newer image for centos:7
docker.io/library/centos:7

4、Docker Image Ls 镜像查看

[root@Tang ~]# docker image ls --help

Usage:	docker image ls [OPTIONS] [REPOSITORY[:TAG]]

List images

Aliases:
  ls, images, list

Options:
  -a, --all             Show all images (default hides intermediate images)
      --digests         Show digests
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print images using a Go template
      --no-trunc        Don't truncate output
  -q, --quiet           Only show numeric IDs
[root@Tang ~]# docker image ls       # 可以看到刚才下载的 centos:7 镜像
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              4152a9608752        5 days ago          126MB
centos              7                   5e35e350aded        13 days ago         203MB
busybox             latest              020584afccce        3 weeks ago         1.22MB

5、Docker Image Prune 清除镜像

慎用此命令,此命令会把所有未使用的镜像进行删除。

[root@Tang ~]# docker image prune --help

Usage:	docker image prune [OPTIONS]

Remove unused images

Options:
  -a, --all             Remove all unused images, not just dangling ones
      --filter filter   Provide filter values (e.g. 'until=<timestamp>')
  -f, --force           Do not prompt for confirmation                    # 不提示确认信息

6、Docker Image Rm 删除指定镜像

[root@Tang ~]# docker image rm --help

Usage:	docker image rm [OPTIONS] IMAGE [IMAGE...]

Remove one or more images

Aliases:
  rm, rmi, remove

Options:
  -f, --force      Force removal of the image
      --no-prune   Do not delete untagged parents 
[root@Tang ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              4152a9608752        5 days ago          126MB
centos              7                   5e35e350aded        13 days ago         203MB
busybox             latest              020584afccce        3 weeks ago         1.22MB
[root@Tang ~]# docker image rm nginx:latest           # 删除 nginx:latest 镜像
Untagged: nginx:latest
Untagged: nginx@sha256:9916837e6b165e967e2beb5a586b1c980084d08eb3b3d7f79178a0c79426d880
Deleted: sha256:4152a960875253728e0ba43da37d023e4626c43a268ca0a9c6083119542119fb
Deleted: sha256:1cdfdca2c291c5d20901fcedf580cfb67252d515e4d8bcbd5c9f0d60d7a1ee10
Deleted: sha256:dd9ce25e5c89862300ac75fc6cd2071c572aabeff7e4685238a1d41066af6500
Deleted: sha256:b67d19e65ef653823ed62a5835399c610a40e8205c16f839c5cc567954fcf594
[root@Tang ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              7                   5e35e350aded        13 days ago         203MB
busybox             latest              020584afccce        3 weeks ago         1.22MB

7、Docker Image Tag 对原有的镜像打 tag,生成新镜像

[root@Tang ~]# docker image tag --help

Usage:	docker image tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
[root@Tang ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              7                   5e35e350aded        13 days ago         203MB
busybox             latest              020584afccce        3 weeks ago         1.22MB
### 源镜像是 busybox:latest,目的镜像是 busybox:v.1125 ###
[root@Tang ~]# docker image tag busybox:latest busybox:v.1125    
[root@Tang ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              7                   5e35e350aded        13 days ago         203MB
busybox             latest              020584afccce        3 weeks ago         1.22MB
busybox             v.1125              020584afccce        3 weeks ago         1.22MB

8、Docker Image Save 保存镜像

[root@Tang ~]# docker image save --help

Usage:	docker image save [OPTIONS] IMAGE [IMAGE...]

Save one or more images to a tar archive (streamed to STDOUT by default)

Options:
  -o, --output string   Write to a file, instead of STDOUT

[root@Tang ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              7                   5e35e350aded        13 days ago         203MB
busybox             latest              020584afccce        3 weeks ago         1.22MB
busybox             v.1125              020584afccce        3 weeks ago         1.22MB
[root@Tang ~]# docker image save busybox:v.1125 > myimage.tar                # 直接保存到当前目录
[root@Tang ~]# ls my*
myimage.tar
[root@Tang ~]# docker image save busybox:latest -o /root/mybusyboximage.tar  # 保存到别的目录
[root@Tang ~]# ls my*
mybusyboximage.tar  myimage.tar

9、Docker Image Load 加载镜像

Load 和 Save 常结合起来,对镜像进行传输。具体的可查看以下链接。

使用 Docker Image Save 和 Load 实现对 Docker 镜像的传输

[root@Tang ~]# docker image load --help

Usage:	docker image load [OPTIONS]

Load an image from a tar archive or STDIN

Options:
  -i, --input string   Read from tar archive file, instead of STDIN
  -q, --quiet          Suppress the load output
[root@Tang ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              7                   5e35e350aded        13 days ago         203MB
busybox             latest              020584afccce        3 weeks ago         1.22MB
busybox             v.1125              020584afccce        3 weeks ago         1.22MB
[root@Tang ~]# docker image rm busybox:v.1125    # 删除镜像 busybox:v.1125
Untagged: busybox:v.1125
[root@Tang ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              7                   5e35e350aded        13 days ago         203MB
busybox             latest              020584afccce        3 weeks ago         1.22MB
[root@Tang ~]# docker load < myimage.tar        # 加载镜像 busybox:v.1125  
Loaded image: busybox:v.1125
[root@Tang ~]# docker image ls                  # 加载成功
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              7                   5e35e350aded        13 days ago         203MB
busybox             latest              020584afccce        3 weeks ago         1.22MB
busybox             v.1125              020584afccce        3 weeks ago         1.22MB
[root@Tang ~]# docker image rm busybox:latest   # 删除镜像 busybox:latest
Untagged: busybox:latest
[root@Tang ~]# docker image ls                  # 删除成功
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              7                   5e35e350aded        13 days ago         203MB
busybox             v.1125              020584afccce        3 weeks ago         1.22MB
[root@Tang ~]# docker load -i /root/mybusyboximage.tar   # 指定加载镜像的源
Loaded image: busybox:latest
[root@Tang ~]# docker image ls                  # 镜像加载成功
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              7                   5e35e350aded        13 days ago         203MB
busybox             latest              020584afccce        3 weeks ago         1.22MB
busybox             v.1125              020584afccce        3 weeks ago         1.22MB

10、Docker Image History 显示镜像的操作历史

[root@Tang ~]# docker image history  --help

Usage:	docker image history [OPTIONS] IMAGE

Show the history of an image

Options:
      --format string   Pretty-print images using a Go template
  -H, --human           Print sizes and dates in human readable format (default true)
      --no-trunc        Don't truncate output
  -q, --quiet           Only show numeric IDs
[root@Tang ~]# docker image history busybox:latest
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
020584afccce        3 weeks ago         /bin/sh -c #(nop)  CMD ["sh"]                   0B                  
<missing>           3 weeks ago         /bin/sh -c #(nop) ADD file:1141b81e5149cc37c…   1.22MB              
[root@Tang ~]# docker image history centos:7
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
5e35e350aded        13 days ago         /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
<missing>           13 days ago         /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B                  
<missing>           13 days ago         /bin/sh -c #(nop) ADD file:45a381049c52b5664…   203MB               

11、Docker Image Inspect 显示镜像的详细信息

[root@Tang ~]# docker image inspect --help

Usage:	docker image inspect [OPTIONS] IMAGE [IMAGE...]

Display detailed information on one or more images

Options:
  -f, --format string   Format the output using the given Go template
[root@Tang ~]# docker image inspect centos:7   # 显示 CentOS:7 的详细信息
[
    {
        "Id": "sha256:5e35e350aded98340bc8fcb0ba392d809c807bc3eb5c618d4a0674d98d88bccd",
        "RepoTags": [
            "centos:7"
        ],
        "RepoDigests": [
            "centos@sha256:4a701376d03f6b39b8c2a8f4a8e499441b0d567f9ab9d58e4991de4472fb813c"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2019-11-12T00:20:33.943019377Z",
        "Container": "66db091183f2f98b5f52eace5d6d4d6662af823bd85640b2afa4d42ffbe9b03e",
        "ContainerConfig": {
            "Hostname": "66db091183f2",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"/bin/bash\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:a0232440f519e6fde80830ff51d9c6bf6ef628b844a826986f995c635baa4ff9",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20191001",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "DockerVersion": "18.06.1-ce",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:a0232440f519e6fde80830ff51d9c6bf6ef628b844a826986f995c635baa4ff9",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20191001",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 203001545,
        "VirtualSize": 203001545,
        "GraphDriver": {
            "Data": {
                "MergedDir": "/var/lib/docker/overlay2/a3d731ecf92becdac1114f6c381ea8b945f70dfdce56f87494b71e095d21c0c4/merged",
                "UpperDir": "/var/lib/docker/overlay2/a3d731ecf92becdac1114f6c381ea8b945f70dfdce56f87494b71e095d21c0c4/diff",
                "WorkDir": "/var/lib/docker/overlay2/a3d731ecf92becdac1114f6c381ea8b945f70dfdce56f87494b71e095d21c0c4/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:77b174a6a187b610e4699546bd973a8d1e77663796e3724318a2a4b24cb07ea0"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]

12、Docker Image Import 从归档文件中创建镜像

[root@Tang ~]# docker image import --help

Usage:	docker image import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]

Import the contents from a tarball to create a filesystem image

Options:
  -c, --change list      Apply Dockerfile instruction to the created image
  -m, --message string   Set commit message for imported image
[root@Tang ~]# docker image import myimage.tar   # 一般需要指定镜像名字
sha256:126eb2a67c6b346d9eeadb98eb4e281a353d169c01c709513e89e70b545d8da2
[root@Tang ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              126eb2a67c6b        6 seconds ago       1.44MB
centos              7                   5e35e350aded        13 days ago         203MB
busybox             latest              020584afccce        3 weeks ago         1.22MB
busybox             v.1125              020584afccce        3 weeks ago         1.22MB
[root@Tang ~]# docker image import myimage.tar busybox:v112502   
sha256:c9be125a00e8f84232ea8a2493ed7391886e34feb71ed91f20999e98fa7d7b2a
[root@Tang ~]# docker image ls 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             v112502             c9be125a00e8        4 seconds ago       1.44MB
<none>              <none>              126eb2a67c6b        33 seconds ago      1.44MB
centos              7                   5e35e350aded        13 days ago         203MB
busybox             latest              020584afccce        3 weeks ago         1.22MB
busybox             v.1125              020584afccce        3 weeks ago         1.22MB

13、Docker Image Push 上传镜像只仓库

[root@Tang ~]# docker image push --help

Usage:	docker image push [OPTIONS] NAME[:TAG]

Push an image or a repository to a registry

Options:
      --disable-content-trust   Skip image signing (default true)

14、Docker Image Build 创建镜像

后续会有 博客 详细介绍此命令的使用。

[root@Tang ~]# docker image build --help

Usage:	docker image build [OPTIONS] PATH | URL | -

Build an image from a Dockerfile

Options:
      --add-host list           Add a custom host-to-IP mapping (host:ip)
      --build-arg list          Set build-time variables
      --cache-from strings      Images to consider as cache sources
      --cgroup-parent string    Optional parent cgroup for the container
      --compress                Compress the build context using gzip
      --cpu-period int          Limit the CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int           Limit the CPU CFS (Completely Fair Scheduler) quota
  -c, --cpu-shares int          CPU shares (relative weight)
      --cpuset-cpus string      CPUs in which to allow execution (0-3, 0,1)
      --cpuset-mems string      MEMs in which to allow execution (0-3, 0,1)
      --disable-content-trust   Skip image verification (default true)
  -f, --file string             Name of the Dockerfile (Default is 'PATH/Dockerfile')
      --force-rm                Always remove intermediate containers
      --iidfile string          Write the image ID to the file
      --isolation string        Container isolation technology
      --label list              Set metadata for an image
  -m, --memory bytes            Memory limit
      --memory-swap bytes       Swap limit equal to memory plus swap: '-1' to enable unlimited swap
      --network string          Set the networking mode for the RUN instructions during build (default "default")
      --no-cache                Do not use cache when building the image
      --pull                    Always attempt to pull a newer version of the image
  -q, --quiet                   Suppress the build output and print image ID on success
      --rm                      Remove intermediate containers after a successful build (default true)
      --security-opt strings    Security options
      --shm-size bytes          Size of /dev/shm
  -t, --tag list                Name and optionally a tag in the 'name:tag' format
      --target string           Set the target build stage to build.
      --ulimit ulimit           Ulimit options (default [])

15、Docker Search 镜像搜索

[root@Tang ~]# docker search --hep
unknown flag: --hep
See 'docker search --help'.
[root@Tang ~]# docker search --help

Usage:	docker search [OPTIONS] TERM

Search the Docker Hub for images

Options:
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print search using a Go template
      --limit int       Max number of search results (default 25)
      --no-trunc        Don't truncate output
[root@Tang ~]# docker search centos
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
centos                             The official build of CentOS.                   5696                [OK]                
ansible/centos7-ansible            Ansible on Centos7                              126                                     [OK]
jdeathe/centos-ssh                 OpenSSH / Supervisor / EPEL/IUS/SCL Repos -114                                     [OK]
consol/centos-xfce-vnc             Centos container with "headless" VNC session…   100                                     [OK]
序号 参数 说明
1 NAME 镜像名称
2 DESCRIPTION 镜像说明
3 STARS 点赞数量
4 OFFICIAL 是否官方版本
5 AUTOMATED 是否自动构建版本
发布了158 篇原创文章 · 获赞 7 · 访问量 9742

猜你喜欢

转载自blog.csdn.net/weixin_44983653/article/details/103233574
今日推荐