Bootstrap

【docker 总结】第二篇 - Image 镜像

一,前言

上一篇,简单介绍了 docker,本篇介绍 Image 镜像

二,image 镜像

  • Docker 把应用程序及其依赖,打包在 image 文件里面。只有通过这个文件,才能生成 Docker 容器

  • image 文件可以看作是容器的模板

  • Docker 根据 image 文件生成容器的实例

  • 同一个 image 文件,可以生成多个同时运行的容器实例

  • 镜像不是一个单一的文件,而是有多层

  • 容器其实就是在镜像的最上面加了一层读写层,在运行容器里做的任何文件改动,都会写到这个读写层里。如果容器删除了,最上面的读写层也就删除了,改动也就丢失了

  • 我们可以通过docker history 查看镜像中各层内容及大小,每层对应着Dockerfile中的一条指令

// 。。。

  • 用户既可以使用 docker load 来导入镜像存储文件到本地镜像库,也可以使用 docker import 来导入一个容器快照到本地镜像库

  • 这两者的区别在于容器(import)快照文件将丢弃所有的历史记录和元数据信息(即仅保存容器当时的快照状态),而镜像(load)存储文件将保存完整记录,体积也要大

  • 此外,从容器(import)快照文件导入时可以重新指定标签等元数据信息

查看镜像

docker image ls

查找镜像

docker search ubuntu 

拉取镜像

docker  pull docker.io/hello-world 
  • docker image pull是抓取 image 文件的命令

  • docker.io/hello-world是 image 文件在仓库里面的位置,其中docker.io是 image的作者,hello-world是 image 文件的名字

  • Docker 官方提供的 image 文件,都放在docker.io组里面,所以它的是默认组,可以省略 docker image pull hello-world

删除镜像

docker rmi  hello-world

命令测试

查看本地全部镜像列表

bogon:~ brave$ docker image ls
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

查看镜像的全部版本:

备注:官方镜像可以省略“/”

bogon:~ brave$ docker search ubuntu
NAME                                                      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubuntu                                                    Ubuntu is a Debian-based Linux operating sys…   13235     [OK]       
dorowu/ubuntu-desktop-lxde-vnc                            Docker image to provide HTML5 VNC interface …   590                  [OK]

查看指定镜像的历史:

(镜像怎么来的)

bogon:~ brave$ docker image history ubuntu
Error response from daemon: No such image: ubuntu:latest

查看指定镜像的详情

bogon:~ brave$ docker image inspect ubuntu

ubuntu 有 4 层,每层都是一个镜像

拉取指定镜像

开始拉取
bogon:~ brave$ docker image pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Downloading [===========>                                       ]  18.48MB/83.52MB
                           
拉取完成
bogon:~ brave$ docker image pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete 
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest

拉群镜像(指定 tag 版本)

bogon:~ brave$ docker image pull centos:6
6: Pulling from library/centos
ff50d722b382: Downloading [==================>                                ]  25.71MB/69.84MB
                           
                           
bogon:~ brave$ docker image pull centos:6
6: Pulling from library/centos
ff50d722b382: Pull complete 
Digest: sha256:a93df2e96e07f56ea48f215425c6f1673ab922927894595bb5c0ee4c5a955133
Status: Downloaded newer image for centos:6
docker.io/library/centos:6

查看镜像列表:centos 6和最新版

bogon:~ brave$ docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
centos       6         5bf9684f4720   2 months ago   194MB
centos       latest    5d0da3dc9764   2 months ago   231MB

删除镜像

可以按照名字 或 ID 删除指定镜像

docker image rmi 镜像名 或 镜像 ID

prune

没有名字,也没有任何容器引用这个镜像,就会被干掉

docker image prune

打 tag

docker image tag centos brave/centos7:latest
docker image ls

推送镜像

1,需要注册账号,登录

docker login

2,push

docker push brave/centos7:latest

export、import、save、load

build