Docker 镜像与库

Docker Imageker Image 镜像
容器的基石
层叠的只读文件系统
联合加载(union mount)
镜像存储位置为 /var/lib/docker 也可通过docker info命令查看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=false Force removal of the image
--no-prune=false Do not delete untagged parents
Docker 镜像与库

查找镜像
Doceker Hub
https://registry.hub.docker.com
$ docker search [ OPTIONS ] TERM
--automated=false Only show automated builds
--no-trunc=false Don`t truncate output
-s, --stars=0 Only displays with at least x stars
最多返回25个结果

拉取镜像
$ docker pull [ OPTIONS ] NAME [ :TAG ]
-a, --all-tags=false Download all tagged images in the repository
Docker 镜像与库
使用 --registry-mirror选项
1、修改 /etc/default/docker
2、添加:DOCKER_OPTS= “--registry-mirror=http://MIRROR-ADDR
https://www.daocloud.io (国内镜像源)

推送镜像
$ docker push NAME[:TAG]
Docker 镜像与库

构建镜像
1.保存对容器的修改,并再次使用
2.自定义镜像的能力
3.一软件的形式打包并分发服务及其运行环境

一、构建镜像的方式
$ docker commit 通过容器构建
$ docker commit [ OPTIONS ] CONTAINER [ REPOSITORY[:TAG] ]
-a, --author="" Author
e.g.,"John Hannibal Smith [email protected]"
-m, --message=" " Commit message
-p, --pause=true Pause container during commit
Docker 镜像与库
Docker 镜像与库

$ docker build 通过Dockerfile 文件构建
1.创建Dockerfile
2.使用$docker build 命令
Dockerfile是指包含一系列命令的文本文件
创建第一个Dockerfile 文件实例
¥#First Dockerfile
FROM ubuntu:14.04
MAINTAINER dormancypress “[email protected]
RUN apt-get update
RUN apt-get install -y nginx
EXPOSE 80
创建例子:
Docker 镜像与库
Docker 镜像与库
$docker build [ OPTIONS ] PATH | URL | -
--force-rm=false
--no-cache=false
--pull=false
-q,--quiet=false
--rm=true
-t,--tag=" "
Docker 镜像与库

Dockerfile指令---指令格式
$# Comment
INSTRUCTION argument

FROM指令:
FROM <image>
FROM <image>:<tag>
<image>已经存在的镜像,基础镜像,必须是第一条非注释指令

MAINTAINER指令:
MAINTAINER <name>
指定镜像的作者信息,包含镜像的所有者和联系信息

RUN指令:
指定当前镜像中运行的命令

  1. RUN <command> (shell指令)
    /bin/sh -c command
    RUN echo hello

  2. RUN [ "executabke","param1","param2" ] (exec模式)
    RUN [ "/bin/bash","-c","echo hello" ]

EXPOSE指令:
EXPOSE <port> [ <port>...]
指定运行该镜像的容器使用的端口
在容器运行时,我们仍需指定端口号
$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模式)
可以使用docker run --entrypoint 覆盖

ADD指令:
ADD <src>... <dest>
ADD [ "<src>"..."<dest>" ] (适用于文件路径中有空格的情况)

COPY指令:
COPY <src>... <dest>
COPY [ "<src>"..."<dest>" ] (适用于文件路径中有空格的情况)

ADD vs. COPY
ADD 包含类似tar的解压功能
如果单纯复制文件,Docker推荐使用COPY

VOLUME指令:
VOLUME [ "/data" ]

WORKDIR指令:(使用绝对路径)
WORKDIR /path/to/workdir

ENV指令:
ENV <key> <value>
ENV <key>=<value>

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 ]
Docker 镜像与库
Docker 镜像与库

转载于:https://blog.51cto.com/zhanx/2105769