Docker镜像

Docker运行容器之前需要有对应运行的镜像,如果本地找不到运行的镜像,docker会去默认的公共镜像仓库Docker Hub下载镜像,这个仓库是可配置的,默认是去Docker Hub下载镜像。

搜索镜像

命令格式:

docker search [OPTIONS] TERM

OPTIONS参数说明:

  • –automated=false :只列出 automated build类型的镜像;
  • –no-trunc=false :显示完整的镜像描述;
  • -s :列出收藏数不小于指定值的镜像。

举例说明,查找nginx的收藏数超过1000的镜像:

[root@izur9gz  docker]# docker search -s 1000 nginx
Flag --stars has been deprecated, use --filter=stars=3 instead
NAME                  DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                 Official build of Nginx.                        11436               [OK]
jwilder/nginx-proxy   Automated Nginx reverse proxy for docker con…   1599                                    [OK]

获取镜像

语法:参数TAG是可选的,如果不填,会默认选择lastest标签,也就是获取最新的镜像

docker pull [options] name[:tag|@digest]

OPTIONS说明:

  • -a :拉取所有 tagged 镜像;
  • –disable-content-trust :忽略镜像的校验,默认开启。

例如下面拉取ubuntu镜像(等同于docker pull ubuntu:lastest

docker pull ubuntu

例如下载18.10版本的ubuntu

docker pull ubuntu:18.10

运行命令:

[root@izur9gz docker]# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
6abc03819f3e: Pull complete
05731e63f211: Pull complete
0bd67c50d6be: Pull complete
Digest: sha256:f08638ec7ddc90065187e7eabdfac3c96e5ff0f6b2f1762cf31a4f49b53000a5
Status: Downloaded newer image for ubuntu:latest
[root@izur9gz docker]# docker pull ubuntu:18.10
18.10: Pulling from library/ubuntu
89074f19944e: Pull complete
6cd3a42e50df: Pull complete
26b902a7bf04: Pull complete
Digest: sha256:50c1dc36867d3caf13f3c07456b40c57b3e6a4dcda20d05feac2c15e357353d4
Status: Downloaded newer image for ubuntu:18.10

查看镜像

命令格式:

docker images [OPTIONS] [REPOSITORY[:TAG]]

OPTIONS说明:

  • -a :列出本地所有的镜像(含中间映像层,默认情况下,过滤掉中间映像层);
  • –digests :显示镜像的摘要信息;
  • -f :显示满足条件的镜像;
  • –format :指定返回值的模板文件;
  • –no-trunc :显示完整的镜像信息;
  • -q :只显示镜像ID。

获取容器/镜像的元数据命令格式:

docker inspect [OPTIONS] NAME|ID [NAME|ID...]

OPTIONS说明:

  • -f :指定返回值的模板文件;

  • -s :显示总的文件大小;

  • –type :为指定类型返回JSON。

运行命令:

[root@izur9gz docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              18.10               d23255d3a3b2        5 days ago          73MB
ubuntu              latest              7698f282e524        5 days ago          69.9MB
nginx               latest              53f3fd8007f7        13 days ago         109MB
[root@izur9gz docker]# docker images u*
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              18.10               d23255d3a3b2        5 days ago          73MB
ubuntu              latest              7698f282e524        5 days ago          69.9MB
[root@izur9gz docker]# docker inspect d23255d3a3b2
[
    {
        "Id": "sha256:d23255d3a3b2c0a728990e13ef26a8630c2d836d0f76b845dfa280df6960e871",
        "RepoTags": [
            "ubuntu:18.10"
        ],
        "RepoDigests": [
            "ubuntu@sha256:50c1dc36867d3caf13f3c07456b40c57b3e6a4dcda20d05feac2c15e357353d4"
        ],
        "Parent": "",
        ···
        ···
        ···

删除镜像

命令格式:

docker rmi [OPTIONS] IMAGE [IMAGE...]

OPTIONS说明:

  • -f :强制删除;
  • –no-prune :不移除该镜像的过程镜像,默认移除。

运行命令:

[root@izur9gz docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              18.10               d23255d3a3b2        5 days ago          73MB
ubuntu              latest              7698f282e524        5 days ago          69.9MB
nginx               latest              53f3fd8007f7        13 days ago         109MB
[root@izur9gz docker]# docker rmi d23255d3a3b2
Untagged: ubuntu:18.10
Untagged: ubuntu@sha256:50c1dc36867d3caf13f3c07456b40c57b3e6a4dcda20d05feac2c15e357353d4
Deleted: sha256:d23255d3a3b2c0a728990e13ef26a8630c2d836d0f76b845dfa280df6960e871
Deleted: sha256:b51db5d74c64ad915bedf6bea784c847d9a5dac9e8ecd95ac4a57607dbe5e588
Deleted: sha256:6e1303552a837a20c0523ac5134c95810324d65eaa6bbfd21a9f9d964747e277
Deleted: sha256:6276206df905e39c37c16b2a54c1875d9d7fadc73bd0fd39cc027c16c868fd69
[root@izur9gz docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              7698f282e524        5 days ago          69.9MB
nginx               latest              53f3fd8007f7        13 days ago         109MB

创建镜像

点此跳转

存出和载入镜像

存出镜像命令格式:

docker save [OPTIONS] IMAGE [IMAGE...]

OPTIONS 说明:

  • -o :输出到的文件。

运行命令:

[root@izur9gz docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              7698f282e524        5 days ago          69.9MB
nginx               latest              53f3fd8007f7        13 days ago         109MB
[root@izur9gz docker]# docker save -o ~/ubuntu.tar ubuntu
[root@izur9gz docker]# ll ~
total 70664
-rw------- 1 root root 72356352 May 21 16:53 ubuntu.tar

载入镜像命令格式:

docker load [OPTIONS]

OPTIONS 说明:

  • -i :指定导出的文件;
  • -q :精简输出信息。

导入镜像:

docker load --input ~/ubuntu.tar
docker load < ~/ubuntu.tar

上传镜像

命令格式:

docker push [OPTIONS] NAME[:TAG]

OPTIONS参数说明:

  • –disable-content-trust :忽略镜像的校验,默认开启

上传本地镜像mytest:v1到镜像仓库中(将本地的镜像上传到镜像仓库,要先登陆到镜像仓库)

docker push mytest:v1