0%

Docker--本地镜像使用

列出本地镜像列表

REPOSITORY:表示镜像的仓库源
TAG:镜像的标签
IMAGE ID:镜像ID
CREATED:镜像创建时间
SIZE:镜像大小

1
2
3
[root@centos7 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 4bb46517cac3 2 weeks ago 133MB

同一仓库源可以有多个 TAG,代表这个仓库源的不同个版本,使用 REPOSITORY:TAG 来定义不同的镜像。

1
2
3
4
[root@centos7 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx alpine 6f715d38cfe0 2 weeks ago 22.1MB
nginx latest 4bb46517cac3 2 weeks ago 133MB

获取新的镜像

当我们在本地主机上使用一个不存在的镜像时 Docker 就会自动下载这个镜像。如果我们想预先下载这个镜像,我们可以使用 docker pull 命令来下载它。

1
2
3
4
5
6
7
8
9
10
[root@centos7 ~]# docker pull nginx:alpine
alpine: Pulling from library/nginx
df20fa9351a1: Pull complete
3db268b1fe8f: Pull complete
f682f0660e7a: Pull complete
7eb0e8838bc0: Pull complete
e8bf1226cc17: Pull complete
Digest: sha256:a97eb9ecc708c8aa715ccfb5e9338f5456e4b65575daf304f108301f3b497314
Status: Downloaded newer image for nginx:alpine
docker.io/library/nginx:alpine

查找镜像

我们可以从 Docker Hub 网站来搜索镜像,Docker Hub 网址为: https://hub.docker.com/ 我们也可以使用 docker search 命令来搜索镜像。

NAME: 镜像仓库源的名称
DESCRIPTION: 镜像的描述
OFFICIAL: 是否 docker 官方发布
stars: 类似 Github 里面的 star,表示点赞、喜欢的意思。
AUTOMATED: 自动构建。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@centos7 ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 13677 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1867 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 782 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 127
bitnami/nginx Bitnami nginx Docker Image 89 [OK]
tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 88 [OK]
jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 82
alfg/nginx-rtmp NGINX, nginx-rtmp-module and FFmpeg from sou… 75 [OK]
nginxdemos/hello NGINX webserver that serves a simple page co… 59 [OK]
jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 53 [OK]
nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 41
privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 32 [OK]
schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 19 [OK]

删除镜像

镜像删除使用 docker rmi 命令

1
2
3
4
5
[root@centos7 ~]# docker rmi hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:7f0a9f93b4aa3022c3a4c147a449bf11e0941a1fd0bf4a8e6c9408b2600777c5
Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b
Deleted: sha256:9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63

更新镜像

新镜像之前,我们需要使用镜像来创建一个容器。

1
2
3
4
5
6
7
[root@centos7 ~]# docker run -itd centos:centos7.8.2003 /bin/bash
4d95f2fd7cfb5ca53fd98eabfe4745815a9530ff7f593957d3b10b4b110318e3
[root@centos7 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4d95f2fd7cfb centos:centos7.8.2003 "/bin/bash" 8 seconds ago Up 7 seconds angry_cori
[root@centos7 ~]# docker exec -it 4d95f2fd7cfb bash
[root@4d95f2fd7cfb /]# yum update -y

各个参数说明:
-m: 提交的描述信息
-a: 指定镜像作者
e218edb10161:容器 ID(docker ps获取)
runoob/ubuntu:v2: 指定要创建的目标镜像名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 通过命令docker commit 来提交容器副本 创建新的镜像
[root@centos7 ~]# docker commit -m="yum update" -a="user" 4d95f2fd7cfb user/centos:v2
sha256:8aee4e075a0d907c757bcc3946165447607bfe8d726888fad090fdc9ddda0718

# 查看是否生产新的镜像
[root@centos7 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
user/centos v2 8aee4e075a0d 12 seconds ago 203MB

# 启动新的镜像容器
[root@centos7 ~]# docker run -itd user/centos:v2 /bin/bash
799efd51587574113896b3c3218b08664b864c155490ca2728e65452a9f44901

# 查看启动的容器
[root@centos7 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
799efd515875 user/centos:v2 "/bin/bash" 4 seconds ago Up 3 seconds elastic_euclid
4d95f2fd7cfb centos:centos7.8.2003 "/bin/bash" 5 minutes ago Up 5 minutes angry_cori

# 停止旧的容器
[root@centos7 ~]# docker stop 4d95f2fd7cfb
4d95f2fd7cfb
# 删除旧的容器
[root@centos7 ~]# docker rm 4d95f2fd7cfb
4d95f2fd7cfb
# 查看剩余容器状态
[root@centos7 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
799efd515875 user/centos:v2 "/bin/bash" 3 minutes ago Up 3 minutes elastic_euclid

设置镜像标签

使用 docker tag 命令,为镜像添加一个新的标签。

1
2
3
4
5
6
7
8
9
10
11
12
13
# 查看镜像
[root@centos7 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx alpine 6f715d38cfe0 2 weeks ago 22.1MB

# 为镜像搭上tag号
[root@centos7 ~]# docker tag 6f715d38cfe0 nginx:new

# 确认最终状态,生成两个镜像,可以理解为复制镜像
[root@centos7 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx alpine 6f715d38cfe0 2 weeks ago 22.1MB
nginx new 6f715d38cfe0 2 weeks ago 22.1MB