小洪維尼 发表于 2019-2-22 06:24:49

Docker 镜像与库

  Docker Imageker Image 镜像
容器的基石
层叠的只读文件系统
联合加载(union mount)
镜像存储位置为 /var/lib/docker也可通过dockerinfo命令查看docker存储的驱动和位置。
  列出镜像
$ docker images[ OPTSIONS ][ REPOSITORY ]
-a , --all=false
-f , --filter=[]
--no-trunc=false
-q , --quiet=false
  1、REPOSITORY仓库
2、TAG   标签
  查看镜像
$ docker inspect [ OPTIONS ]CONTAINER|IMAGE[ CONTAINER|IMAGE... ]
-f, --format= “”
删除镜像
$ docker rmi[ OPTIONS ]   IMAGE[ IMAGE... ]
-f ,--force=falseForce removal of the image
--no-prune=falseDo not delete untaggedparents
https://s1.运维网.com/images/blog/201804/20/1c8ca03f7abf65e876b5ff76ac8e6245.png
  查找镜像
Doceker Hub
https://registry.hub.docker.com
$ docker search[ OPTIONS ]TERM
--automated=false   Only show automated builds
--no-trunc=falseDon`t truncate output
-s, --stars=0 Only displays with at least x stars
最多返回25个结果
  拉取镜像
$ docker pull [ OPTIONS ]NAME [ :TAG ]
-a, --all-tags=falseDownload all tagged images in the repository
https://s1.运维网.com/images/blog/201804/20/8c84062cc00e5c2c7f19bac59102aedf.png
使用 --registry-mirror选项
1、修改/etc/default/docker
2、添加:DOCKER_OPTS= “--registry-mirror=http://MIRROR-ADDR”
https://www.daocloud.io(国内镜像源)
  推送镜像
$ docker push NAME[:TAG]
https://s1.运维网.com/images/blog/201804/20/19da68f960fca149d386623726a16897.png
  构建镜像
1.保存对容器的修改,并再次使用
2.自定义镜像的能力
3.一软件的形式打包并分发服务及其运行环境
  一、构建镜像的方式
$ docker commit   通过容器构建
$ docker commit[ OPTIONS ]CONTAINER [ REPOSITORY[:TAG] ]
-a, --author=""Author
e.g.,"John Hannibal Smith hannibal@a-team.com"
-m, --message=" "   Commit message
-p, --pause=truePause container during commit
https://s1.运维网.com/images/blog/201804/20/72dff9a6bca025f67e3acd04000579c2.png
https://s1.运维网.com/images/blog/201804/20/830f736eedc36a5773cf0ba1098f1e93.png
  $ docker build通过Dockerfile 文件构建
1.创建Dockerfile
2.使用$docker build 命令
Dockerfile是指包含一系列命令的文本文件
创建第一个Dockerfile 文件实例
¥#First Dockerfile
FROM ubuntu:14.04
MAINTAINERdormancypress“dormancypress@outlook.com”
RUN apt-get update
RUN apt-get install -y nginx
EXPOSE 80
创建例子:
https://s1.运维网.com/images/blog/201804/20/e8784bf8378fd98de70418ef99fe9b6a.png
https://s1.运维网.com/images/blog/201804/20/e2a6e4447e16f337a172e2148f12c623.png
$docker build [ OPTIONS ] PATH | URL | -
--force-rm=false
--no-cache=false
--pull=false
-q,--quiet=false
--rm=true
-t,--tag=" "
https://s1.运维网.com/images/blog/201804/20/f318df84cf0c5a1c800166d5277648b9.png
  Dockerfile指令---指令格式
$# Comment
INSTRUCTIONargument
  FROM指令:
FROM
FROM:
已经存在的镜像,基础镜像,必须是第一条非注释指令
  MAINTAINER指令:
MAINTAINER
指定镜像的作者信息,包含镜像的所有者和联系信息
  RUN指令:
指定当前镜像中运行的命令

[*]  RUN   (shell指令)
/bin/sh-ccommand
RUNecho hello
[*]RUN [ "executabke","param1","param2" ]   (exec模式)
RUN [ "/bin/bash","-c","echo hello" ]
  EXPOSE指令:
EXPOSE    [ ...]
指定运行该镜像的容器使用的端口
在容器运行时,我们仍需指定端口号
$docker run -p 80 -d dormancypress/df_test1 nginx -g "daemon off;"
  CMD指令:
CMD [ "executable","param1","param2" ]   (exe模式)
CMD command param1 param2(shell模式)
CMD [ "param1","param2" ](作为ENTRYPOINT指令的默认参数)
  ENTRYPOINT指令:
ENTRYPOINT [ "executable","param1","param2" ]   (exe模式)
ENTRYPOINT command param1 param2(shell模式)
可以使用dockerrun --entrypoint 覆盖
  ADD指令:
ADD ...
ADD [ ""..."" ]   (适用于文件路径中有空格的情况)
  COPY指令:
COPY ...
COPY [ ""..."" ]   (适用于文件路径中有空格的情况)
  ADD vs. COPY
ADD 包含类似tar的解压功能
如果单纯复制文件,Docker推荐使用COPY
  VOLUME指令:
VOLUME [ "/data" ]
  WORKDIR指令:(使用绝对路径)
WORKDIR/path/to/workdir
  ENV指令:
ENV
ENV =
  USER 指令:
USER daemon
USER nginx(uid gid等)
USER user ,USER uid, USER user:group , USER uid:gid
USER user:group , USER uid:group
  ONBYILD指令:
ONBYILD [ INSTRUCTION ]
镜像触发器
当一个镜像被其他镜像作为基础镜像时执行
  Docckerfile构建过程
1、从基础镜像运行一个容器
2、执行一条指令,对容器做出修改
3、执行类似docker commit的操作,提交一个新的镜像层
4、再基于刚提交的镜像运行一个新容器
5、执行Dockerfile中的下一条指令,直至所有指令执行完毕
  使用中间层镜像进行调试
查找错误
  构建缓存
不适用缓存
$docker build --no-cache
  查看镜像构建的过程
$docker history [ image ]
https://s1.运维网.com/images/blog/201804/23/12a8d0a9368db603cab407aea3011582.png
https://s1.运维网.com/images/blog/201804/23/a08aaa81eb677a2e60bd5c8ce13473fb.png



页: [1]
查看完整版本: Docker 镜像与库