一、什么是镜像
镜像就是打包好的环境与应用。
二、镜像的主要分类
- 操作系统类(如centos,ubuntu)
- 应用程序类(如web站点)
三、镜像加速器
国内从 Docker Hub 拉取镜像有时会遇到困难,此时可以配置镜像加速器。国内很多云服务商都提供了国内加速器服务。
添加国内镜像仓库
// 这里以网易的镜像仓库为例子
[root@docker ~]# vi /etc/docker/daemon.json
{
"registry-mirrors": [
"https://hub-mirror.c.163.com"
]
}
重新启动服务
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
检查加速器是否生效
执行$ docker info
,如果从结果中看到了如下内容,说明配置成功.
Registry Mirrors:
https://hub-mirror.c.163.com/
四、镜像基础操作
4.1 搜素镜像
docker search [关键字]
[root@docker ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 12908 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1761 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 760 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 102
4.2 拉取镜像
docker pull [image:tag]
不加tag,默认latest版本
[root@docker ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
c499e6d256d6: Pull complete
74cda408e262: Pull complete
ffadbd415ab7: Pull complete
Digest: sha256:282530fcb7cd19f3848c7b611043f82ae4be3781cb00105a1d593d7e6286b596
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
4.3 查看镜像列表
docker images
,docker image ls
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ed21b7a8aee9 34 hours ago 127MB
[root@docker ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ed21b7a8aee9 34 hours ago 127MB
4.4 给镜像打标签
docker tag
[root@docker ~]# docker tag nginx:latest nginx:v1.1
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ed21b7a8aee9 34 hours ago 127MB
nginx v1.1 ed21b7a8aee9 34 hours ago 127MB
4.5 导出镜像
docker save
// -o 指定导出路径
[root@docker ~]# docker save -o /data/nginx-v1.1.tar.gz nginx:v1.1
[root@docker ~]# ll /data/
total 127560
-rw------- 1 root root 130618880 Apr 1 20:58 nginx-v1.1.tar.gz
4.6 删除镜像
docker rmi
或者docker image rm
[root@docker ~]# docker rmi nginx:v1.1
Untagged: nginx:v1.1
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ed21b7a8aee9 34 hours ago 127MB
4.7导入镜像
docker load
[root@docker ~]# docker load -i /data/nginx-v1.1.tar.gz
Loaded image: nginx:v1.1
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ed21b7a8aee9 34 hours ago 127MB
nginx v1.1 ed21b7a8aee9 34 hours ago 127MB