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

参考自:
http://www.jikexueyuan.com/course/859.html
http://www.jikexueyuan.com/course/876.html

镜像与仓库

查看和删除镜像

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

列出镜像

docker images [OPTIONS] [REPOSITORY]

-a, –all=false Show all images (default hides intermediate images)
–digests=false Show digests
-f, –filter=[] Filter output based on conditions provided
–help=false Print usage
–no-trunc=false Don’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=false Print 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=false Print usage
–no-prune=false Do 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=false Print 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=false Do not use cache when building the image
-q, –quiet=false Suppress 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 hello
RUN ["executable", "param1", "param2"] (exec 模式)
RUN ["/bin/bash", "-c", "echo hellow"]

EXPOSE
指定容器运行端口,只是告诉docker该容器会使用这个端口,但不会自动打开端口,而是要在run 中添加端口的映射指令。

例子:

#First Dockerfile
FROM ubuntu:14.04
MAINTAINER billvsme "994171686@qq.com"
RUN apt-get update
RUN apt-get install -y nginx
EXPOSE 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 uid
USER uid:gid
USER uid:group
USER nginx
以nginx 身份运行
默认是root

ONBUILD

ONBUILD [INSTRUCTION]
镜像触发器
当一个镜像被其他镜像作为基础镜像时执行,这个触发器会被执行
当子镜像在构建时会插入触发器当指令
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值