Docker 运行容器前需要本地存在对应的镜像,如果本地不存在该镜像,Docker 会从镜像仓库下载该镜像。
Docker镜像
获取镜像
从 Docker 镜像仓库获取镜像的命令是 docker pull
。其命令格式为:
$ docker pull [选项] [Docker Registry 地址[:端口号]/]仓库名[:标签]
具体的选项可以通过 docker pull --help
命令看到,这里我们说一下镜像名称的格式
Docker 镜像仓库地址:地址的格式一般是 <域名/IP>[:端口号]
。默认地址是 Docker Hub(docker.io
)。
仓库名:如之前所说,这里的仓库名是两段式名称,即 <用户名>/<软件名>
。对于 Docker Hub,如果不给出用户名,则默认为 library
,也就是官方镜像。
PS D:\> docker pull ubuntu:18.04
18.04: Pulling from library/ubuntu
Digest: sha256:0fedbd5bd9fb72089c7bbca476949e10593cebed9b1fb9edf5b79dbbacddd7d6
Status: Image is up to date for ubuntu:18.04
docker.io/library/ubuntu:18.04
列出镜像
要想列出已经下载下来的镜像,可以使用 docker image ls
命令。
PS D:\> docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
test20210608ubuntu v1 78ba2af0dfd4 24 hours ago 63.1MB
ubuntu 18.04 5a214d77f5d7 18 months ago 63.1MB
hello-world latest feb5d9fea6a5 18 months ago 13.3kB
PS D:\>
删除本地镜像
如果要删除本地的镜像,可以使用 docker image rm
命令,其格式为:
$ docker image rm [选项] <镜像1> [<镜像2> ...]
用 ID、镜像名、摘要删除镜像
PS D:\> docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
test20210608ubuntu v1 78ba2af0dfd4 24 hours ago 63.1MB
ubuntu 18.04 5a214d77f5d7 18 months ago 63.1MB
hello-world latest feb5d9fea6a5 18 months ago 13.3kB
PS D:\> docker image rm 78b
Untagged: test20210608ubuntu:v1
Deleted: sha256:78ba2af0dfd4352c1b621bc3e9769556c12dec4b918c175e35c0fb16f8afa29e
Deleted: sha256:932a6bae4790489982e95401e5e5ab1c02719af61d5ca8facd5a49159f5b2999
我们也可以用镜像名
,也就是 <仓库名>:<标签>
,来删除镜像。
PS D:\> docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 18.04 5a214d77f5d7 18 months ago 63.1MB
hello-world latest feb5d9fea6a5 18 months ago 13.3kB
PS D:\> docker image rm ubuntu:18.04
Untagged: ubuntu:18.04
Untagged: ubuntu@sha256:0fedbd5bd9fb72089c7bbca476949e10593cebed9b1fb9edf5b79dbbacddd7d6
Deleted: sha256:5a214d77f5d747e6ed81632310baa6190301feeb875cf6bf9da560108fa09972
Deleted: sha256:824bf068fd3dc3ad967022f187d85250eb052f61fe158486b2df4e002f6f984e
PS D:\> docker image rm hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359
PS D:\>
当然,更精确的是使用 镜像摘要
删除镜像。
PS D:\> docker image ls --digests
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
ubuntu latest sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322 ba6acccedd29 17 months ago 72.8MB
PS D:\>