镜像文件一般由若干个层(layer)组成,f17d81b4b692这样的串是层的唯一ID,(实际上完整的id是256比特,64个十六进制字符组 成),使用docker pull 命令下载中会获取并输出镜像的各层信息,当不同的镜像包括相同的层时,本地仅存了层的一份内容,减少了存储空间。
1、搜索镜像
命令选项:
-f –filter :过滤输出内容
–fromat string: 格式化输出内容
–limit int : 限制输出结果个数,默认25个
–no-trunc:不截断输出结果
[[email protected] ~]# docker search nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 10168 [OK] #搜索官方提供带nginx关键字的镜像 docker search --filter=is-Official=true nginx #搜索收藏数超过4的 docker search --filter=stars=4 nginx
2、拉取镜像并启动
#拉取Centos系统镜像,如果不指定TAG,则默认会选择latest标签,下载仓库中最新版本的镜像。
[[email protected] ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
Digest: sha256:67dad89757a55bfdfabec8abd0e22f8c7c12a1856514726470228063ed86593b
Status: Image is up to date for centos:latest
#生产环境应指定版本TAG镜像,拉取Nginx-1.14.1稳定版
[[email protected] ~]# docker pull nginx:1.14.1
1.14.1: Pulling from library/nginx
f17d81b4b692: Pull complete
f1998c324db0: Pull complete
e27db20b7cd9: Pull complete
Digest: sha256:15d670262978b801c857b7f13e0a743a2e69a001ef8edb90245debadc99fd38f
Status: Downloaded newer image for nginx:1.14.1
如果从非官方的仓库下载,需要在仓库名称前指定完整的仓库地址,例如:从网易蜂巢的镜像源下载ubuntu:18.04
$docker pull hub.c.163.com/public/ubuntu:18.04
#查看images
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 75835a67d134 4 weeks ago 200MB
nginx 1.14.1 8552ee3809ee 6 days ago 109MB
#使用交互式运行centos容器并分配伪终端
[[email protected] ~]# docker run -i -t centos /bin/bash
#查看容器系统版本
[[email protected] /]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
3、删除镜像
#使用标签删除,可以删除某个标签副本
[[email protected] ~]# docker rmi centos:latest
Untagged: centos:latest
#强制删除,一般不建议使用,会导致某些问题
[[email protected] ~]# docker rmi -f nginx:1.14.1
Untagged: nginx:1.14.1
Untagged: [email protected]:d59a1aa7866258751a261bae525a1842c7ff0662d4f34a355d5f36826abc0341
#使用镜像ID删除,会删除镜像文件本身
[[email protected] ~]# docker rmi 415381a6cb81
Untagged: redis:latest
Untagged: [email protected]:f30f134bd475d451ce3207fb128bcef8ff87d0f520a39cac0c4ea285819c42a9
Deleted: sha256:415381a6cb813ef0972eff8edac32069637b4546349d9ffdb8e4f641f55edcdd
#删除有正在运行该镜像创建的容器时,应先删除容器id,然后删除镜像id
查看hello-world 镜像id
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 4ab4c602aa5e 2 months ago 1.84kB
#查看容器id
[[email protected] ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8d60e035fb6a hello-world "/hello" 26 hours ago Exited (0) 26 hours ago naughty_liskov
#删除容器及镜像
[[email protected] ~]# docker rm 8d60e035fb6a
8d60e035fb6a
[[email protected] ~]# docker rmi 4ab4c602aa5e
4、清理镜像
使用Docker一段时间后,系统中会遗留一些临时的镜像文件,以及一些没有被使用的镜像,可以通过docker image prun命令清理,支持选项:
-a,-all :删除所有无用镜像,不光是临时镜像
-filter filter :只清理符合给定过滤器的镜像
-f,-force:强制删除镜像。
[[email protected] ~]# docker image prune -f Total reclaimed space: 0B
5、使用tag命令添加镜像标签
[[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 75835a67d134 4 weeks ago 200MB #添加一个自定义centos标签 [[email protected] ~]# docker tag centos:latest mycentos:7.5 [[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mycentos 7.5 75835a67d134 4 weeks ago 200MB
6、使用inspect命令查看详细信息
[[email protected] ~]# docker inspect centos:latest [ { "Id": "sha256:75835a67d1341bdc7f4cc4ed9fa1631a7d7b6998e9327272afea342d90c4ab6d", "RepoTags": [ "centos:latest", "mycentos:7.5" ······································································· #使用-f 指定获取特定参数 [[email protected] ~]# docker inspect -f {{".Architecture"}} centos:latest amd64
7、使用history命令查看镜像历史
[[email protected] ~]# docker history centos:latest IMAGE CREATED CREATED BY SIZE COMMENT 75835a67d134 4 weeks ago /bin/sh -c #(nop)CMD ["/bin/bash"] 0B <missing> 4 weeks ago /bin/sh -c #(nop)LABEL org.label-schema.sc… 0B <missing> 4 weeks ago /bin/sh -c #(nop) ADD file:fbe9badfd2790f074… 200MB
8、创建镜像
创建镜像的方法有以下3种方式:
(1)基于已有镜像的容器创建
(2)基于本地模板导入
(3)基于Dockerfile创建
(1)基于已有镜像的容器创建
命令格式:docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
主要选项:
-a:作者信息
-m:提交消息
-c:提交的时候执行Docker指令
-p:提交时暂停容器运行
运行docker run后,进入到该容器中,我们做一些变更,比如安装一些东西,然后针对这个容器进行创建新的镜像
#查看所有容器 [[email protected] ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c6a7b5f61ce7 centos "/bin/bash" 34 seconds Exited (0) 19 minutes ago wizardly_shockley [[email protected] ~]# docker start c6a //启动ID为c6a的容器,ID可简写为前3位 [[email protected] ~]# docker exec -it c6a /bin/bash //进入c6a容器 [[email protected] /]# yum install -y net-tools wget //安装工具包 [[email protected] /]# exit //退出容器 #//创建新的镜像 [[email protected] ~]# docker commit -m "centos_add-tools" -a "test" c6a mycentos:7.5 sha256:0ae99d448477d4878b18f1a22474ef9f252c87c53dd35e81462180682bbef34c [[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mycentos 7.5 0ae99d448477 3 seconds ago 301MB centos latest 75835a67d134 4 weeks ago 200MB
(2)基于本地模板导入
#在网上下载一个模板 [[email protected] ~]# wget https://download.openvz.org/template/precreated/contrib/ centos-7-x86_64-minimal-20170709.tar.xz #导入 [[email protected] ~]# cat centos-7-x86_64-minimal-20170709.tar.xz | docker import - centos-7 sha256:0e3e7b19f6c38417f920f41a1ec1270d9123f44195a1207ece4e338af57d408c #查看 [[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos-7 latest 0e3e7b19f6c3 15 seconds ago 387MB
(3)基于Dockerfile创建:
基于debian:stretch-slim镜像构建一个python3的新镜像
vim Dockerfile FROM debian:stretch-slim LABEL version="1.0" maintainer="docker user <[email protected]>" RUN apt-get update && \ apt-get install -y python3 && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* 构建 $ docker build -t python:3 . ---> eeb21f15c34b Successfully built eeb21f15c34b Successfully tagged python:3 查看创建的镜像 [[email protected] python]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE python 3 eeb21f15c34b 13 seconds ago 95.1MB
9、存出和载入镜像
#导出镜像到本地文件 [[email protected] python]# docker save -o python-3.tar python:3 [[email protected] python]# ls Dockerfile python-3.tar #载入镜像 docker load -i python-3.tar 或者 docker load < python-3.tar
10:上传镜像
需要先注册一个Docker hub账号 #登录 docker login #更改tag 使用方法:username/repository:tag docker tag python:3 anonym0x1/python:3 [[email protected] python]# docker push anonym0x1/python:3