Docker 学习笔记(三)--Docker镜像与仓库

参考自:

镜像与仓库查看和删除镜像

镜像存放的位置 /var/lib/docker

列出镜像docker images [OPTIONS] [REPOSITORY]

-a, –all=falseShow all images (default hides intermediate images) –digests=falseShow digests -f, –filter=[]Filter output based on conditions provided –help=falsePrint usage –no-trunc=falseDon’t truncate output -q, –quiet=false Only show numeric IDs

镜像标签和仓库

当运行docker images 可以看到:

其中 PEROSITORY就是仓库,TAG 就是标签

查看镜像docker inspect [OPTIONS] CONTAINER|IMAGE [CONTAINER|IMAGE…]

-f, –format=Format the output using the given go template –help=falsePrint usage –type=Return JSON for specified type, (e.g image or container)

删除镜像docker rmi [OPTIONS] IMAGE [IMAGE…]

-f, –force=false Force removal of the image –help=falsePrint usage –no-prune=falseDo not delete untagged parents

获取和推送镜像查找镜像docker search [image]拉取镜像docker pull [image]

-a, –all-tags=false

加速 使用 –registry-mirror 选项 1. 修改: /etc/default/docker 2. 添加: DOCKER_OPTIOS = “–registry-mirror=http://XXXX”

推送镜像docker push

构建镜像

docker commit 通过容器构建 docker build 通过Dockerfile文件构建

docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

-a, –author=Author (e.g., “John Hannibal Smith hannibal@a-team.com”) -c, –change=[]Apply Dockerfile instruction to the created image –help=falsePrint usage -m, –message=Commit message -p, –pause=true Pause container during commit

例子:

docker run -d –name test_web -p 80:80 billvsme/web nginx -g “daemon off;”

使用Dockerfile构建镜像docker build [OPTIONS] PATH | URL | –

–no-cache=falseDo not use cache when building the image -q, –quiet=falseSuppress the verbose output generated by -t, –tag=Repository name (and optionally a tag) for the image

例子:

docker build -t =’billvsme/web’ ./

查看镜像构建的过程docker history [image]

Dockerfile格式

注释

# Comment

指令

FROM

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

MAINTAINER

MAINTAINER <name>指定镜像第作者学习指定当前镜像中运行的命令

RUN

RUN <command> (shell模式)RUN echo helloRUN [“executable”, “param1”, “param2”] (exec 模式)RUN [“/bin/bash”, “-c”, “echo hellow”]EXPOSE指定容器运行端口,只是告诉docker该容器会使用这个端口,但不会自动打开端口,而是要在run 中添加端口的映射指令。

例子:

#First DockerfileFROM ubuntu:14.04MAINTAINER billvsme “994171686@qq.com”RUN apt-get updateRUN apt-get install -y nginxEXPOSE 80

其他指令

CMD

CMD command param1 param2 (shell模式)CMD [‘executable’, “param1”, “param2”] (exec模式)CMD [‘param1’, ‘param2’] (作为ENTERYPOINT指令的默认参数)RUN 指令的命令是在容器构建时候运行的,CMD命令是在容器运行时运行的,,并且如果docker run 指定cmd命令,CMD命令会被覆盖

ENTERYPOINT

ENTERYPOINT command param1 param2(shell模式)ENTERYPOINT [‘executable’, “param1”, “param2”] (exec模式)跟CMD的区别就是,ENTERYPOINT 不会被覆盖,如果想覆盖,可以使用 docker run –entrypoint 覆盖

COPY

COPY <src> … <desc>COPY [“<src>”…”<dest>”] (适用于文件路径中有空格的情况)

ADD

ADD 于COPY基本相同,只是ADD提供了类似tar的解压功能,ADD src 可以是url

VOLUME

VOLUME [“/data”] 向容器添加卷

WORKDIR

WORKDIR /path/to/workdir在容器内部设置工作目录,后面CMD、ENTERYPOINT 都会在这个目录下执行

ENV

ENV <key><value>ENV <key>=<value>…设置环境变量

USER

USER uidUSER uid:gidUSER uid:groupUSER nginx以nginx 身份运行默认是root

ONBUILD

ONBUILD [INSTRUCTION]镜像触发器当一个镜像被其他镜像作为基础镜像时执行,这个触发器会被执行当子镜像在构建时会插入触发器当指令

天再高又怎样,踮起脚尖就更接近阳光。

Docker 学习笔记(三)--Docker镜像与仓库

相关文章:

你感兴趣的文章:

标签云: