docker镜像默认存储位置:/var/lib/docker (使用docker info命令可以查看)
[root@localhost docker]# docker info |grep Dir Docker Root Dir: /var/lib/docker
列出镜像
docker images OPTSIONS REPOSITORY
OPTSIONS 选项
-a 显示所有镜像,默认不显示中间层的镜像
-f 在显示时的过滤条件
--no-trunc=false 不使用截断的模式显示,默认情况查看的是被截断的镜像(截断唯一ID)
-q 只显示唯一ID
REPOSITORY 仓库
[root@localhost docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 07c86167cdc4 2 years ago 188 MB REPOSITORY:镜像所属的仓库名 TAG:镜像的标签名,标示每个仓库不同标签名 IMAGE ID:镜像的唯一ID,和镜像的文件名对应(不过被截断了) CREATED:镜像创建的时间 SIZE:镜像的大小
REPOSITORY(镜像仓库):是一些关联镜像的集和,如:ubuntu是一个大仓库,每个镜像对应不同版本
和REGISTRY(仓库)不同,这是docker组件中的仓库,提供docker镜像的存储服务
即:REGISTRY中有很多REPOSITORY,REPOSITORY中有很多IMAGE(相互独立)
TAG:镜像的标签,用来区分不同的镜像,一个REPOSITORY:TAG就是一个完整的镜像,对应一个完整的镜像ID,默认TAG使用latest
[root@localhost docker]# docker images --no-trunc REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest sha256:07c86167cdc4264926fa5d2894e34a339ad27f730e8cc81a16cd21b7479e8eac 2 years ago 188 MB [root@localhost docker]# docker images -a REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> 220d2912ab1d 2 years ago 188 MB ubuntu latest 07c86167cdc4 2 years ago 188 MB <none> <none> cc77a2e3d72c 2 years ago 188 MB <none> <none> c8fa7cdceff3 2 years ago 188 MB [root@localhost docker]# docker images -q 07c86167cdc4 [root@localhost docker]# docker images ubuntu REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 07c86167cdc4 2 years ago 188 MB
查看年镜像的详细信息
docker inspect OPTIONS CONTAINER|IMAGE
删除镜像
docker rmi OPTIONS IMAGE
-f 强制删除
--no-prune=false 保留被打标签的父镜像
总结
docker images 列出镜像
Repository & Tag 镜像标签和仓库
docker inspect 查看镜像信息
dcoker rmi 删除镜像
获取和推送镜像
查找镜像
查找镜像有两种方式:
Docker Hub官方网站:registory 要注册
docker search OPTIONS TERM
docker search OPTIONS TERM
--automated=false 显示自动化构建
--no-trunc=false 不进行输出截断
-s,--stars=0 只显最低星级
[root@localhost docker]# docker search -s 3 ubuntu Flag --stars has been deprecated, use --filter=stars=3 instead NAME DESCRIPTION STARS OFFICIAL AUTOMATED ubuntu Ubuntu is a Debian-based Linux operating s... 7716 [OK] dorowu/ubuntu-desktop-lxde-vnc Ubuntu with openssh-server and NoVNC 184 [OK] rastasheep/ubuntu-sshd Dockerized SSH service, built on top of of... 148 [OK] ansible/ubuntu14.04-ansible Ubuntu 14.04 LTS with ansible 92 [OK]
拉取镜像
对查找到的镜像进行下载
docker pull OPTIONS NAME:TAR
-a 下载仓库中所有标签的镜像
--registry-mirror 指定镜像源,默认时docker官网提供的
[root@localhost docker]# docker pull ubuntu:14.04 14.04: Pulling from library/ubuntu 324d088ce065: Pull complete 2ab951b6c615: Pull complete 9b01635313e2: Pull complete 04510b914a6c: Pull complete 83ab617df7b4: Pull complete Digest: sha256:b8855dc848e2622653ab557d1ce2f4c34218a9380cceaa51ced85c5f3c8eb201 Status: Downloaded newer image for ubuntu:14.04 [root@localhost docker]# docker images -a REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu 14.04 8cef1fa16c77 3 weeks ago 223 MB
使用国内仓库源(本地也同理)
在拉取镜像的时候使用--registry-mirror选项
指定镜像源,就要修改配置:
[root@localhost docker]# vim /etc/default/docker ###添加 DOCKER_OPTS="--registry-mirror=https://registry.docker-cn.com" 要重新启动docker守护进程
推送镜像
docker push IMAGE
总结
docker search 查找镜像
docker pull 下载(拉取)镜像
docker push 上传镜像
构建Docker镜像
构建镜像,可以保存对容器的设置、修改,并可移植化的使用;提供了自定义镜像的能力,以软件的形式打包并奋发服务及运行环境(在其他有docker服务的主机上也可以使用)
docker提供了两种构建镜像的方式
docker commit 通过容器构建
docker build 通过Dockerfile文件构建
commit命令构建
commit通过容器来构建镜像
docker commit OPTIONS CONTAINER REPOSITORY:TAG
-a 指定镜像作者
-m 记录镜像构建的信息
-p 不暂停正在运行的容器进行构建
具体示例:
1,创建一个容器,添加nginx服务,然后退出
[root@localhost docker]# docker run -it -p 80 --name commit ubuntu:latest /bin/bash root@b113160a5513:/# apt-get install -y nginx root@b113160a5513:/# exit exit
2,通过容器构建一个镜像
[root@localhost packages]#docker commit -a 'fsx' -m 'nginx' commit fsx/nginx [root@localhost packages]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE fsx/nginx latest b4f46238b357 3 minutes ago 206 MB ubuntu 14.04 8cef1fa16c77 3 weeks ago 223 MB wordpress 4.4.2 e146b48c1da2 2 years ago 517 MB ubuntu latest 07c86167cdc4 2 years ago 188 MB
这样就可以直接使用fsx/nginx镜像运行一个容器
[root@localhost packages]# docker run -d --name fsx_nginx -p 80 fsx/nginx nginx -g "daemon off;" 25b6b7aa5a33912e2408ea7144ebff7a5f0a4859c3b3ee287e5da33333525716 [root@localhost packages]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 25b6b7aa5a33 fsx/nginx "nginx -g 'daemon ..." 4 seconds ago Up 4 seconds 0.0.0.0:32778->80/tcp fsx_nginx [root@localhost packages]# curl localhost:32778 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
bulid构建镜像
1,创建Dockerfile
2,使用docker build 命令
Dockerfile文件是包含了一系列命令的文本文件,如:
#一个Dockerfile文件例子 FROM ubuntu:latest MAINTAINER ubuntu "fsx" RUN apt-get update RUN apt-get install -y nginx EXPOSE 80
docker build OPTIONS PATH|URL|-
--force-rm=false
--no-cache=false
--pull=false
-q
--rm=true
-t="" 指定构建出的镜像的名字
示例:
[root@localhost Docker_file]# docker build -t="fsx_nginx2" .
Dockerfile指令
dockerfile指令格式:
Comment 井号开头的是注释
INSTRUCTION argument 具体指令是以大写的指令名开始,后面跟着参数信息
如:(常见的四个指令:FROM MAINTAINER RUN EXPOSE)
#一个Dockerfile文件例子 注释 FROM ubuntu:latest 指令 MAINTAINER ubuntu "fsx" RUN apt-get update RUN apt-get install -y nginx EXPOSE 80
FROM <images> 必须是已经存在的镜像,称之为基础镜像,必须是dockerfile中第一条非注释命令,因为之后所有的指令都使用该镜像
FROM <image>:<tag>MAINTAINER <name> 指定作者信息,通常包含镜像的所有者和联系信息
RUN 指定当前镜像中运行的命令,具体有两种模式
RUN <command> (shell模式)
RUN [ "execitable","param1","param2"] (exec模式)
EXPOSE <port>[<port>... ] 指定运行该镜像的容器使用的端口,可以使用多个EXPOSE命令
其他一些Dockerfile指令:
指定容器运行时,运行的命令
CMD 指定容器运行的默认行为
CMD [ "executable","param1","param2"] (exec模式)
CMD command param1 param2 (shell模式)
CMD [ "param1","param2" ] (作为ENTRYPOINT指令的默认参数)
ENTRYPOINT 不会被启动容器时添加的命令覆盖
ENTRYPOINT [ "executable","param1","param2" ] (exec模式)
ENTRYPOINT command param1 param2 (shell模式)
设置镜像的目录和文件
ADD 将文件或者目录复制到docker容器中
ADD <src> ... <dest> 对于资源文件:可以是本地地址(相对地址),也可以是URL(远程)
对于目标路径,是绝对路径
ADD包含了类似tar的解压缩功能,即对tar包直接ADD,会自动解压
COPY是单纯的复制文件
ADD [ "<src>..."dest""] (适用于文件路径中有空格的情况)
COPY
COPY <src> ... <dest>
COPY [ "<src>..."dest""] (适用于文件路径中有空格的情况)
VOLUME 向基于镜像的容器添加卷,可以存在于一个或者多个目录,可以绕过联合文件系统,提供共享数据或者数据持久化的功能
VOLUME [ "/data"]
指定镜像构建,容器运行时的环境指令
WORKDIR 在容器内部设置工作目录
WORKDIR /path/to/workdir
ENV 设置环境变量
ENV <key><value>
ENV <key>=<value>...
USER 指定镜像以什么用户身份运行,默认是root
USER daemon
USER user:group|gid
USER uid:gid|group
类似触发器的指令
ONBUILD 当一个镜像被用作其他镜像的基础镜像时,这个触发器会被执行,会在构建过程中插入指令
ONBUILD [INSTRUCTION]
Dockerfile构建过程
从基础镜像运行一个容器
执行一条指令,对容器进行修改
执行类似docker commit的操作,提交一个新的镜像层
再基于刚提交的新的镜像运行一个新容器
再执行Dockerfile中的下一条指令,反复2,3,4操作,直到所有指令执行完毕
具体示例:
编辑Dockerfile文件
build过程
[root@localhost docker_file]# docker build -t="test_docker" . Sending build context to Docker daemon 2.048 kB Step 1/5 : FROM ubuntu:latest //第一步,构建一个新镜像,id=07c86167cdc4;提交一个新的镜像,然后到step2 ---> 07c86167cdc4 Step 2/5 : MAINTAINER fsx@qq.com //根据step1的镜像,运行一个新的容器,id=c1859d653b48;执行2指令,commit一个新的镜像,id=f24ab1ce2890,然后到step3 ---> Running in c1859d653b48 ---> f24ab1ce2890 Removing intermediate container c1859d653b48 //删除容器c1859d653b48 Step 3/5 : ENV REFRESH_DATE 2018-05-26 //根据step2的镜像,运行一个容器,id=f7f79e9da9f9;执行3指令,commit一个新的镜像,id=5684fbf70082,然后到step4 ---> Running in f7f79e9da9f9 ---> 5684fbf70082 Removing intermediate container f7f79e9da9f9 //删除容器f7f79e9da9f9 Step 4/5 : RUN apt-get install -y nginx //根据step3的镜像,运行一个容器,id=baa7e7e96d92;执行4指令,commit一个新的镜像,id=c6d35a8e6583,然后到step5 ---> Running in baa7e7e96d92 Reading package lists... 。。。下载安装nginx过程 Removing intermediate container baa7e7e96d92 Step 5/5 : EXPOSE 80 ---> Running in 0503ec976f53 ---> c6d35a8e6583 Removing intermediate container 0503ec976f53 //删除容器0503ec976f53 Successfully built c6d35a8e6583 //成功构建新的镜像,id=c6d35a8e6583
注意,build的过程没有删除过程中的镜像,这就给了调试每一层镜像的方法,这有利于查找错误
构建缓存
由于,Dockerfile的build整个不过,并不删除中间层镜像,所以就可以将中间层镜像当作缓存,这就大大提高了build构建过程。
如,二次构建同一个Dockerfile文件中的内容
[root@localhost docker_file]# docker build -t="docker_test1" . Sending build context to Docker daemon 2.048 kB Step 1/5 : FROM ubuntu:latest ---> 07c86167cdc4 Step 2/5 : MAINTAINER fsx@qq.com ---> Using cache ---> f24ab1ce2890 Step 3/5 : ENV REFRESH_DATE 2018-05-26 ---> Using cache ---> 5684fbf70082 Step 4/5 : RUN apt-get install -y nginx ---> Using cache ---> 6f625bef4bf3 Step 5/5 : EXPOSE 80 ---> Using cache ---> c6d35a8e6583 Successfully built c6d35a8e6583 //有cache字段哦!!!
但是,有时候,我们不希望使用缓存,那就需要在构建时,添加参数--no-cache
[root@localhost docker_file]# docker build -t="docker_test2" --no-cache . Sending build context to Docker daemon 2.048 kB Step 1/5 : FROM ubuntu:latest ---> 07c86167cdc4 Step 2/5 : MAINTAINER fsx@qq.com ---> Running in 10586de75d26 ---> 9219e29e3301 Removing intermediate container 10586de75d26 Step 3/5 : ENV REFRESH_DATE 2018-05-26 ---> Running in 37fc82669236 ---> 000e54c484d2 ...完整过程,代码就不贴了
不使用缓存的第二个方法:在Dockerfile中ENV环境变量设置时,更改时间,也可以刷新缓存;当然在ENV之前的都会使用缓存之后的就刷新了缓存
查看镜像的构建过程,Docker提供了子命令
docker history [image]
[root@localhost docker_file]# docker history ubuntu IMAGE CREATED CREATED BY SIZE COMMENT 07c86167cdc4 2 years ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B 220d2912ab1d 2 years ago /bin/sh -c sed -i 's/^#\s*\(deb.*universe\... 1.9 kB cc77a2e3d72c 2 years ago /bin/sh -c echo '#!/bin/sh' > /usr/sbin/po... 195 kB c8fa7cdceff3 2 years ago /bin/sh -c #(nop) ADD file:b9504126dc55908... 188 MB
总结
主要学习:
commit命令构建镜像方法
build命令构建镜像方法
Dockerfile文件指令
Dockerfile执行过程
构建镜像中间层调试
Dockerfile镜像缓存
查看镜像构建过程