Docker镜像

xiaoxiao2021-02-28  148

1.1 怎么理解Docker的镜像

提到Docker的镜像,可以这样来理解,其实就是一个系统的Image,比如一个Centos系统镜像、一个Ubuntu系统镜像,又或者一个安装某个应用服务(Apapche/Mysql)打包而来的镜像。

镜像是创建Docker容器的前提,通过版本管理和增量的文件系统,Docker提供了一套简单的机制来创建和更新现有的镜像。

启动一个容器的时候,需要在本地有对应的镜像,如果本地不存在的话,Docker会尝试先从默认的镜像仓库去下载镜像,当然,你自己也可以配置,使用自定义的镜像仓库。

1.2 Docker镜像的具体操作

1.2.1 使用pull命令从Docker Hub仓库下载镜像到本地

命令格式:docker pull NAME[:TAG]

注意:在下载镜像的时候,如果不指定镜像的TAG,默认情况下会从仓库中下载最新版本的镜像

下载一个最新版的Ubuntu操作系统

[root@localhost ~]# docker pull ubuntu Using default tag: latest Cannot connect to the Docker daemon. Is the docker daemon running on this host? 下载镜像时出现以上报错现象

解决办法:

启动docker service

[root@localhost ~]# service docker start Redirecting to /bin/systemctl start docker.service

查看docker service是否启动成功 [root@localhost ~]# ps aux |grep docker root 13994 1.4 3.6 647544 36332 ? Ssl 03:26 0:00 /usr/bin/dockerd-current –add-runtime docker-runc=/usr/libexec/docker/docker-runc-current –default-runtime=docker-runc –exec-opt native.cgroupdriver=systemd –userland-proxy-path=/usr/libexec/docker/docker-proxy-current –selinux-enabled –log-driver=journald –signature-verification=false root 14001 0.2 0.5 287804 5932 ? Ssl 03:26 0:00 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock –shim docker-containerd-shim –metrics-interval=0 –start-timeout 2m –state-dir /var/run/docker/libcontainerd/containerd –runtime docker-runc –runtime-args –systemd-cgroup=true root 14250 0.0 0.0 112652 956 pts/0 S+ 03:27 0:00 grep –color=auto docker 可以看到docker进程已经起来

接着从默认仓库下载一个ubuntu镜像 [root@localhost ~]# docker pull ubuntu Using default tag: latest Trying to pull repository docker.io/library/ubuntu … latest: Pulling from docker.io/library/ubuntu d5c6f90da05d: Pull complete 1300883d87d5: Pull complete c220aa3cfc1b: Pull complete 2e9398f099dc: Pull complete dc27a084064f: Pull complete Digest: sha256:34471448724419596ca4e890496d375801de21b0e67b81a77fd6155ce001edad

在不带镜像TAG的情况下,其实就是下载ubuntu:latest,最新的镜像

当然,根据具体需求,也可以下载指定版本的镜像,比如我要下载Ubuntu14.04的镜像

docker pull ubuntu:14.04

如果不从默认的镜像下载镜像,想要从其他仓库下载镜像的话,那么就需要指定完整的镜像仓库地址,如

docker pull dl.dockerpool.com:5000/ubuntu

查看本地下载的镜像 [root@localhost ~]# docker images

注意:列出镜像信息的关键字段 a.镜像来自哪个仓库 b.镜像的标签,其实就是镜像系统的版本号 c.镜像的ID(唯一的) d.创建时间 e.镜像大小

好,现在就用下载到本地的镜像启动一个容器

[root@localhost ~]# docker run -ti ubuntu /bin/bash root@f346e2b8a20c:/#

如上,使用ubuntu镜像成功启动一个容器

根据镜像ID,查看镜像的详细信息 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/ubuntu latest ccc7a11d65b1 2 weeks ago 120.1 MB docker.io/hello-world latest 1815c82652c0 11 weeks ago 1.84 kB

[root@localhost ~]# docker inspect ccc7a11d65b1 [ { “Id”: “sha256:ccc7a11d65b1b5874b65adb4b2387034582d08d65ac1817ebc5fb9be1baa5f88”, “RepoTags”: [ “docker.io/ubuntu:latest” ], “RepoDigests”: [ “docker.io/ubuntu@sha256:34471448724419596ca4e890496d375801de21b0e67b81a77fd6155ce001edad” ], “Parent”: “”, “Comment”: “”, “Created”: “2017-08-10T20:13:42.07991257Z”, “Container”: “098997d78aa83d76ddf6c55c37892844c0d6517a72d3a335c841347c968f734a”, “ContainerConfig”: { “Hostname”: “8089fe031125”, “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:490eac3b7ec54965ae09d3075a391e7b36b11f9b59121a199065facd774a072e”, “Volumes”: null, “WorkingDir”: “”, “Entrypoint”: null, “OnBuild”: null, “Labels”: {} }, “DockerVersion”: “17.03.2-ce”, “Author”: “”, “Config”: { “Hostname”: “8089fe031125”, “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:490eac3b7ec54965ae09d3075a391e7b36b11f9b59121a199065facd774a072e”, “Volumes”: null, “WorkingDir”: “”, “Entrypoint”: null, “OnBuild”: null, “Labels”: {} }, “Architecture”: “amd64”, “Os”: “linux”, “Size”: 120093185, “VirtualSize”: 120093185, “GraphDriver”: { “Name”: “devicemapper”, “Data”: { “DeviceId”: “9”, “DeviceName”: “docker-253:0-101078950-49f968cd8e8d352c1323521eb07bf00d63a263edd20a153679627c82b16e182d”, “DeviceSize”: “10737418240” } }, “RootFS”: { “Type”: “layers”, “Layers”: [ “sha256:8aa4fcad5eeb286fe9696898d988dc85503c6392d1a2bd9023911fb0d6d27081”, “sha256:25e0901a71b8c6a9df21590604a70517eb7b074071ef6af1033d50037baf3dd5”, “sha256:625c7a2a783b4736cf488efd1fafc41736b9998ec087e20da946c30522ec9ad7”, “sha256:9c42c2077cdea659ac116f148b63ded203d0e83f017accb6d7987377d1363673”, “sha256:a09947e71dc0d591901870f2fd3c18565339b960f6bb32793d14604a85d67393” ] } } ] [root@localhost ~]#

1.2.2 在远端仓库使用Search命令进行搜索和过滤

使用docker search 命令可以搜索远端仓库中的镜像 用法:docker search 镜像

例如:搜索仓库中的mysql镜像

注意几列关键信息 第一列:镜像索引 第二列:镜像名字 第三列:镜像描述 第四列:镜像星级描述,在上面的表示越受欢迎, 第五列:该镜像是否为官方创建,有OK,就表示镜像是官方创建 第六列:该镜像是否为自动创建

1.2.3 如何删除镜像标签和镜像文件

在docker容器里面删除一个镜像,有两种方法 第一种:根据镜像的标签删除镜像,因为一个镜像可以对应多个标签 先来看看现在本地已有的Images [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/ubuntu latest ccc7a11d65b1 3 weeks ago 120.1 MB docker.io/centos latest 328edcd84f1b 4 weeks ago 192.5 MB docker.io/hello-world latest 1815c82652c0 11 weeks ago 1.84 kB

为了更让大家明白,怎么通过镜像的TAG来删除一个镜像,现在先给centos镜像增加一个标签 [root@localhost ~]# docker tag docker.io/centos:latest centos:latest

然后再查看centos镜像,发现多了一个centos:latest

好,现在我们需要把这个新添加的标签的镜像删除

大家需要注意的是,当一个镜像对应多个标签的时候,使用docker rmi 只是删除对应标签的镜像,其他标签的镜像不受影响。

但是,当镜像只剩下一个标签的时候,可就要小心了,如果你还是坚持要删除该镜像的话,就真的彻底删除这个镜像了。不信,你看看

第二种:根据镜像的ID删除镜像,一个镜像唯一对应一个ID,特别需要注意的是,正在运行的容器镜像不能正常删除,只能强制删除

首先,以centos镜像创建一个容器 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/ubuntu latest ccc7a11d65b1 3 weeks ago 120.1 MB docker.io/centos latest 328edcd84f1b 4 weeks ago 192.5 MB docker.io/hello-world latest 1815c82652c0 11 weeks ago 1.84 kB [root@localhost ~]# docker run docker.io/centos echo ‘hello! i am here ’ hello! i am here

查看本地现在所有运行的容器

1.2.4 如何创建用户定制的镜像并且保存为外部文件

1.2.5 如何向Docker Hub仓库推送自己制作的镜像

转载请注明原文地址: https://www.6miu.com/read-19787.html

最新回复(0)