获取镜像

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

NAME:表示镜像仓库名称。TAG:表示镜像标签

如果不显式指定TAG,则默认会选择latest标签,也就是最新版本

 

[root@server01 ~]# docker pull ubuntu:16.04
16.04: Pulling from library/ubuntu
fe703b657a32: Pull complete  #层的id
f9df1fafd224: Pull complete 
a645a4b887f9: Pull complete 
57db7fe0b522: Pull complete 
Digest: sha256:e9938f45e51d9ff46e2b05a62e0546d0f07489b7f22fbc5288defe760599e38a
Status: Downloaded newer image for ubuntu:16.04
docker.io/library/ubuntu:16.04

 镜像文件是由若干层(layer)组成。当不同的镜像包括相同的层时,本地仅存储一份内容,以减少存储空间。

默认是从docker hub这个官方仓库地址下载镜像

从非官方仓库下载镜像,需要在镜像名称前指定完整的仓库地址

[root@server01 ~]# docker pull hub.c.163.com/public/ubuntu:16.04
16.04: Pulling from public/ubuntu
8a2df099fc1a: Pull complete 
09aa8e119200: Pull complete 
21a4b8922479: Pull complete 
a3ed95caeb02: Pull complete 
bd2a9dfe68fa: Pull complete 
f132d6d54cc2: Pull complete 
099b34b8b564: Pull complete 
23afed40f2e5: Pull complete 
Digest: sha256:aef5a02062dcb047849076148c2a68ceb64531a0de64f96e9662208cd9dcadeb
Status: Downloaded newer image for hub.c.163.com/public/ubuntu:16.04
hub.c.163.com/public/ubuntu:16.04

 

查看镜像信息

1 使用 images 命令列出镜像

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

docker images 等同于docker image ls

[root@server01 ~]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
ubuntu                        16.04               77be327e4b63        12 days ago         124MB
busybox                       v4                  887961b12ca4        3 weeks ago         1.44MB
busybox                       v3                  8df0cac73694        3 weeks ago         1.44MB

 字段信息:

REPOSITORY:来自哪个仓库。比如ubuntu 表示ubuntu系列的基础镜像

TAG:镜像的标签信息

IMAGE ID:镜像ID。若镜像ID相同,则实际指向同一个镜像,只是不同的标签而已

CREATED:镜像最后的更新时间

SIZE:镜像大小。优秀的镜像一般都很小

2 使用tag命令添加镜像标签

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

[root@server01 ~]# docker tag ubuntu:16.04 huazai007/ubuntu:v1
[root@server01 ~]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
huazai007/ubuntu              v1                  77be327e4b63        12 days ago         124MB

 3 使用inspect 命令查看镜像详细信息(制作者,适用架构,各层的数字摘要)

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

[root@server01 ~]# docker inspect ubuntu:16.04
[
    {
        "Id": "sha256:77be327e4b63498e0faaa065d30d7c0e5499e42a09275ee19e27fd9a93cde7d7",
        "RepoTags": [
            "huazai007/ubuntu:v1",
            "aa:v1",
            "ubuntu:16.04"
        ],
        "RepoDigests": [
            "ubuntu@sha256:e9938f45e51d9ff46e2b05a62e0546d0f07489b7f22fbc5288defe760599e38a"
        ],
。。。。。。。。。。。。。。等等

 -f 指定输出内容

[root@server01 ~]# docker inspect -f {{.ContainerConfig.Hostname}} ubuntu:16.04
039b7d3536d8

 4 使用history命令查看镜像历史 =================》可以说一下 docker 特有的文件系统和存储原理

Usage:    docker history [OPTIONS] IMAGE

该命令将列出镜像文件各层的创建信息

[root@server01 ~]# docker history ubuntu:16.04
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
77be327e4b63        12 days ago         /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
<missing>           12 days ago         /bin/sh -c mkdir -p /run/systemd && echo 'do…   7B                  
<missing>           12 days ago         /bin/sh -c set -xe   && echo '#!/bin/sh' > /…   745B                
<missing>           12 days ago         /bin/sh -c rm -rf /var/lib/apt/lists/*          0B                  
<missing>           12 days ago         /bin/sh -c #(nop) ADD file:1f70668251e2e58ce…   124MB 

 注意:过长的命令被截断了,可以使用docker history ubuntu:16.04 --no-trunc 显示完整命令

搜寻镜像

Usage:    docker search [OPTIONS] TERM

搜索官方(Official)提供的带nginx关键字的镜像

[root@server01 ~]# docker search -f=is-official=true nginx
NAME                DESCRIPTION                STARS               OFFICIAL            AUTOMATED
nginx               Official build of Nginx.   12749               [OK]                

 搜索所有收藏数超过20的关键词保持nginx的镜像。输出结果将按照星界进行排序

[root@server01 ~]# docker search -f=stars=20 nginx
NAME                          DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                         Official build of Nginx.                        12749               [OK]                
jwilder/nginx-proxy           Automated Nginx reverse proxy for docker con…   1748                                    [OK]
richarvey/nginx-php-fpm       Container running Nginx + PHP-FPM capable of…   758                                     [OK]
linuxserver/nginx             An Nginx container, brought to you by LinuxS…   94                  

 

删除和清理镜像

1 使用标签删除镜像

Usage:    docker rmi [OPTIONS] IMAGE [IMAGE...]

[root@server01 ~]# docker rmi busybox:v4
Untagged: busybox:v4
Deleted: sha256:887961b12ca4fc6454248fad0fe77c46344d3d1b5ed732257a8d0caf8a91314d

当同一个镜像用有多个标签的时候,docker rmi  命令试试删除了该镜像多个标签中的指定标签而已,不会影响镜像文件。

当镜像只有一个标签的时候,docker rmi会彻底删除该镜像的所有文件层

[root@server01 ~]# docker rmi -f huazai007_redis:v1
Untagged: huazai007_redis:v1
Deleted: sha256:d8de015bb9e69b299700d121fe7f254d4c5a3442193f7bf44ded5b072492f218
Deleted: sha256:9a1e3a8ec3ecef1b865a3822a523ac63c2ca79024b8f9f582015a7254c78e3c9

 2 使用镜像ID来删除镜像

报错信息表示:该镜像被使用,要删的话+ -f 强制删

[root@server01 ~]# docker rmi 2073e0bcb60e
Error response from daemon: conflict: unable to delete 2073e0bcb60e (must be forced) - image is referenced in multiple repositories
[root@server01 ~]# docker rmi -f 2073e0bcb60e
Untagged: hehe:v1
Untagged: nginx:latest
Untagged: nginx@sha256:ad5552c786f128e389a0263104ae39f3d3c7895579d45ae716f528185b36bc6f
Deleted: sha256:2073e0bcb60ee98548d313ead5eacbfe16d9054f8800a32bedd859922a99a6e1

 3 清理镜像

注意:-a 慎用,它会删除所有无用镜像,而不仅仅是临时镜像文件

[root@server01 ~]# docker image  prune -a
WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y
Deleted Images:
untagged: hub.c.163.com/public/ubuntu:16.04

 

创建镜像

1 基于已有容器创建

Usage:    docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Create a new image from a container's changes
首先:启动一个容器,并入容器终端,创建文件test

[root@server01 ~]# docker run -it ubuntu:16.04 /bin/bash
root@b6fb0a6bb5a8:/# touch test
root@b6fb0a6bb5a8:/# 

 -a:作者

-m:提交信息

b6fb0a6bb5a8:这是原始镜像ID

huazai007:v1  生成新的镜像

[root@server01 ~]# docker commit -a "huazai007" -m "add new file" b6fb0a6bb5a8 huazai007:v1
sha256:cf06117dc26c297ca4065b59d19717b4feadb504efb8954ef977c5c509239531
[root@server01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
huazai007           v1                  cf06117dc26c        4 seconds ago        124MB

 2 基于本地模板导入

[root@server01 ~]# docker import --help

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

Import the contents from a tarball to create a filesystem image

首先:将id为fc2f2ca7626d的容器按日期保存为tar文件。

(docker export):用于持久化容器

(docker save):用于持久化镜像

[root@server01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
fc2f2ca7626d        nginx               "nginx -g 'daemon of…"   5 minutes ago       Up 5 minutes                80/tcp              objective_swanson
b6fb0a6bb5a8        ubuntu:16.04        "/bin/bash"              22 minutes ago      Exited (0) 19 minutes ago                       crazy_bose
[root@server01 ~]# docker export -o nginx-20200306.tar fc2f2ca7626d
[root@server01 ~]# ll
总用量 127304
-rw-r--r--. 1 root root         0 2月  10 14:54 10
-rw-r--r--. 1 root root         0 2月  10 14:53 5
-rw-------. 1 root root      1257 2月  10 00:55 anaconda-ks.cfg
-rw-------. 1 root root   1446400 2月  10 17:03 busybox.tar.gz
-rw-------. 1 root root 128901632 3月   5 23:55 nginx-20200306.tar

 再次:从镜像归档文件nginx-20200306.tar创建镜像,命名为huazai007/nginx:v1

[root@server01 ~]# docker import nginx-20200306.tar  huazai007/nginx:v1
sha256:e86d007dfd78973a608080b6efa9cf26691880615dc927b96d228bc1df84fd8f
[root@server01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
huazai007/nginx     v1                  e86d007dfd78        7 seconds ago       125MB

 3 基于Dockerfile创建

利用给定的指令描述基于某个基础镜像(仅包含一个操作系统)来创建新镜像。

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

Build an image from a Dockerfile

cd  /usr/local/src/workshop

vim Dockerfile

[root@server01 workshop]# cat Dockerfile 
FROM busybox:1.27.2
COPY index.html /data/htdocs/

 开始构建镜像

[root@server01 workshop]# docker build -t huazai007/busybox:v1 .
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM busybox:1.27.2
1.27.2: Pulling from library/busybox
0ffadd58f2a6: Pull complete 
Digest: sha256:bbc3a03235220b170ba48a157dd097dd1379299370e1ed99ce976df0355d24f0
Status: Downloaded newer image for busybox:1.27.2
 ---> 6ad733544a63
Step 2/2 : COPY index.html /data/htdocs/
 ---> 8990dc141bc3
Successfully built 8990dc141bc3
Successfully tagged huazai007/busybox:v1
[root@server01 workshop]# pwd
/usr/local/src/workshop
[root@server01 workshop]# ll
总用量 8
-rw-r--r--. 1 root root 50 3月   6 00:11 Dockerfile
-rw-r--r--. 1 root root  5 3月   6 00:09 index.html

 运行容器并进入容器内

[root@server01 workshop]# docker run -it huazai007/busybox:v1
/ # ls 
bin   data  dev   etc   home  proc  root  sys   tmp   usr   var
/ # ls /data/
htdocs
/ # ls /data/htdocs/
index.html
/ # 

 

存出镜像和载入镜像

(docker export):用于持久化容器

(docker save):用于持久化镜像

1 存储镜像

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

Save one or more images to a tar archive (streamed to STDOUT by default)
导出busybox:1.27.2镜像为 busybox_20200206.tar

[root@server01 ~]# docker save -o busybox_20200206.tar busybox:1.27.2
[root@server01 ~]# ll
总用量 128620

-rw-------. 1 root root   1347072 3月   6 00:35 busybox_20200206.tar

 2 载入镜像

Usage:    docker load [OPTIONS]

Load an image from a tar archive or STDIN

[root@server01 ~]# docker load -i busybox_20200206.tar 
Loaded image: busybox:1.27.2
[root@server01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

busybox             1.27.2              6ad733544a63        2 years ago         1.13MB

 

上传镜像

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

Push an image or a repository to a registry

默认上传到docker hub官方仓库

首先登陆官方仓库

[root@server01 .docker]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: huazai007
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@server01 .docker]# 

 然后:给镜像添加新的标签(和你的注册用户名一致!

[root@server01 ~]# docker tag ubuntu:16.04 huazai007/ubuntu:v1
[root@server01 ~]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
huazai007/ubuntu              v1                  77be327e4b63        12 days ago         124MB

 

[root@server01 ~]# docker push huazai007/busybox
The push refers to repository [docker.io/huazai007/busybox]
dd46f9c78bba: Pushed 
0271b8eebde3: Mounted from library/busybox 
v1: digest: sha256:d3f0c183c2a6032feda46f47500ab531abe4e3f46b40da5ffa9b4e772d135945 size: 734