0%

docker命令分析--镜像相关


作者: 耗子007


所有命令均基于docker1.11版本

镜像相关的命令主要包括三类:

  • 镜像registry相关命令
  • 镜像构建相关命令
  • 镜像操作相关命令

镜像registry操作

在使用docker的过程中,可能需要从镜像registry获取镜像,或者把自己构建的镜像保存到registry。
包括下面几个命令:

1
2
3
4
5
6
login     Log in to a Docker registry
logout Log out from a Docker registry
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
search Search the Docker Hub for images
tag Tag an image into a repository

login和logout

这两个命令主要是用于登录和退出Docker registry的,比较简单,这里只给出基本用法。

  1. login命令
1
2
3
4
5
6
7
8
Usage: docker login [OPTIONS] [SERVER]

Log in to a Docker registry server, if no server is
specified "https://index.docker.io/v1/" is the default.

--help Print usage
-p, --password="" Password
-u, --username="" Username

如果没有指定服务器地址,默认服务器地址为:https://index.docker.io/v1/。服务器地址可以是自己搭建的本地仓库。

  1. logout命令
1
2
3
4
5
6
Usage: docker logout [SERVER]

Log out from a Docker registry, if no server is
specified "https://index.docker.io/v1/" is the default.

--help Print usage

pull、push和search

  1. pull命令:用于从registry下拉镜像或者
1
2
3
4
5
6
7
Usage: docker pull [OPTIONS] NAME[:TAG] | [REGISTRY_HOST[:REGISTRY_PORT]/]NAME[:TAG]

Pull an image or a repository from the registry

-a, --all-tags Download all tagged images in the repository
--disable-content-trust=true Skip image verification
--help Print usage

注:如果是在内网,需要配置代理,可以参考上篇文章。

用pull下载单个镜像

1
2
3
4
#获取默认debian:latest
$ docker pull debian
#指定debian的tag
$ docker pull debian:jessie

上面的pull镜像的方式,可以保证你获取的镜像永远是最新的版本的。但是,如果你想获取某个特定版本的,可以通过digest的方式获取。

1
2
3
4
5
6
7
8
9
$ docker pull ubuntu:14.04

14.04: Pulling from library/ubuntu
5a132a7e7af1: Pull complete
fd2731e4c50c: Pull complete
28a2f68d1120: Pull complete
a3ed95caeb02: Pull complete
Digest: sha256:45b23dee08af5e43a7fea6c4cf9c25ccf269ee113168c19722f87876677c5cb2
Status: Downloaded newer image for ubuntu:14.04

上面的镜像会包含一个Digest信息:sha256:45b23dee08af5e43a7fea6c4cf9c25ccf269ee113168c19722f87876677c5cb2
为了获取固定版本的镜像,可以通过下面的方式:

1
docker pull ubuntu@sha256:45b23dee08af5e43a7fea6c4cf9c25ccf269ee113168c19722f87876677c5cb2

从其他registry获取镜像或者仓库

1
docker pull myregistry.local:5000/testing/test-image

注:

  • docker pull默认从Docker hub上面下拉镜像。
  • myregistry是否是支持insecure方式,如果不支持可能需要一些配置才能pull成功

获取一个仓库的所有镜像

1
$ docker pull --all-tags fedora
  1. push命令:往registry推送镜像或者仓库
1
2
3
4
5
6
Usage: docker push [OPTIONS] NAME[:TAG]

Push an image or a repository to the registry

--disable-content-trust=true Skip image signing
--help Print usage

注:

  • 默认推送到Docker hub,可以推送的自己构建的registry。
  • –disable-content-trust=true可以跳过镜像签名
  1. search命令:在Docker hub搜索镜像
1
2
3
4
5
6
7
8
Usage: docker search [OPTIONS] TERM

Search the Docker Hub for images

--automated Only show automated builds
--help Print usage
--no-trunc Don't truncate output
-s, --stars=0 Only displays with at least x stars

注意:search的说明是在Docker hub上搜索,其实也可以用来搜索自己搭建的registry,但是,如果用registry容器镜像搭建的registry是没有打开search模块的。
因此,search功能在这样的registry上面是不能工作的。

通过镜像名搜索

1
$ docker search ubuntu

通过镜像名和stars次数搜索

1
2
3
4
5
$ docker search --stars=3 busybox
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
busybox Busybox base image. 325 [OK]
progrium/busybox 50 [OK]
radial/busyboxplus Full-chain, Internet enabled, busybox made... 8 [OK]

这里stars表示该镜像在Docker Hub上被人关注的次数。

查询自动构建的镜像

1
2
3
4
$ docker search --stars=3 --automated busybox
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
progrium/busybox 50 [OK]
radial/busyboxplus Full-chain, Internet enabled, busybox made... 8 [OK]

automated感觉用来标识非官方镜像

查询未截断描述的镜像

1
2
3
4
5
$ docker search --stars=3 --no-trunc busybox
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
busybox Busybox base image. 325 [OK]
progrium/busybox 50 [OK]
radial/busyboxplus Full-chain, Internet enabled, busybox made from scratch. Comes in git and cURL flavors. 8 [OK]

tag

tag命令用于修改镜像的仓库名和tag

1
2
3
4
5
Usage: docker tag [OPTIONS] IMAGE[:TAG] [REGISTRYHOST/][USERNAME/]NAME[:TAG]

Tag an image into a repository

--help Print usage

注:如果需要把镜像push到一个自定的registry,首先需要就是tag镜像到该registry的一个仓库(参考文章:搭建本地的Docker registry)。