指令

docker build通过Dockerfile制作镜像

docker build [PATH] [-f Dockerfile]

其中PATH不写,默认执行指令的当前目录,不要用 /,这样将导致整个操作系统复制到镜像中

ENV

定义环境变量

ENV abc=hello
ENV abc=bye def=$abc
ENV ghi=$ab

使用环境变量

WORKDIR ${foo}   # WORKDIR /bar
ADD . $foo # ADD . /bar
COPY \$foo /quux # COPY $foo /quux

.dockerignore

# comment
*/temp*
*/*/temp*
temp?
RULE Behavior
# comment Ignored.
/temp Exclude files and directories whose names start with temp in any immediate subdirectory of the root. For example, the plain file /somedir/temporary.txt is excluded, as is the directory /somedir/temp.
//temp* Exclude files and directories starting with temp from any subdirectory that is two levels below the root. For example, /somedir/subdir/temporary.txt is excluded.
temp? Exclude files and directories in the root directory whose names are a one-character extension of temp. For example, /tempa and /tempb are excluded.

FROM 母镜像

FROM <image>[:<tag>] [AS <name>]

Dockerfile通过ARG设置编译变量,通过${xxxx}在编译过程中调用

ARG  CODE_VERSION=latest
FROM base:${CODE_VERSION}
CMD /code/run-app FROM extras:${CODE_VERSION}
CMD /code/run-extras

RUN 镜像制作指令

用于制作镜像过程之中,执行指令,比如拷贝文件,删除文件。

RUN [/bin/sh -c | cmd /S /C ]

linux下,默认采用 /bin/sh -c

window下,默认采用 cmd /S /C

RUN ["executable", "param1", "param2"]

不像shell form ,exec form 不用直接通过shell命令运行,比如 RUN [ "echo", "$HOME" ]就不会运行,如果希望exce from像shell form那样运行,必须写成RUN [ "sh", "-c", "echo $HOME" ]

CMD 容器启动默认执行脚本

  • CMD ["executable","param1","param2"] (exec form, this is the preferred form)
  • CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
  • CMD command param1 param2 (shell form)

CMD 节点在Dockerfile只能有一个,如果有多个CMD的话,最后出现的CMD生效,其他的忽略

CMD节点目的是给容器提供一个默认的运行指令。

LABEL 镜像标签

示例

LABEL "com.example.vendor"="ACME Incorporated"
LABEL com.example.label-with-value="foo"
LABEL version="1.0"
LABEL description="This text illustrates \
that label-values can span multiple lines."

EXPOSE 监听端口

EXPOSE <port> [<port>/<protocol>...]
EXPOSE 80/tcp
EXPOSE 80/udp

容器中定义好监听端口后,在docker run中如果不用 -p 重新指定监听端口,将直接默认端口映射出去

docker run -p 80:80/tcp -p 80:80/udp

ENV 镜像环境变量

ENV <key> <value>
ENV <key>=<value> ...

示例:

ENV myName="John Doe" myDog=Rex\ The\ Dog \
myCat=fluffy
ENV myName John Doe
ENV myDog Rex The Dog
ENV myCat fluffy

在docker run中可以通过 --env 重写参数

docker run --env <key>=<value>

ADD

  • ADD [--chown=:] ...
  • ADD [--chown=:] ["",... ""] (this form is required for paths containing whitespace)
ADD hom* /mydir/        # adds all files starting with "hom"
ADD hom?.txt /mydir/ # ? is replaced with any single character, e.g., "home.txt"

COPY

  • COPY [--chown=:] ...
  • COPY [--chown=:] ["",... ""] (this form is required for paths containing whitespace)

COPY hom* /mydir/ # adds all files starting with "hom"

COPY hom?.txt /mydir/ # ? is replaced with any single character, e.g., "home.txt"

ENTRYPOINT

  • ENTRYPOINT ["executable", "param1", "param2"] (exec form, preferred)
  • ENTRYPOINT command param1 param2 (shell form)

WORKDIR

ENV DIRPATH /path
WORKDIR $DIRPATH/$DIRNAME
RUN pwd

编写Dockerfiles的更多相关文章

  1. docker(四) 使用Dockerfile构建镜像

    下面以一个例子来演示构建镜像的过程. #在/tmp目录下演示 cd tmp mkdir build-redis-image 1.创建Dockerfile文件 vim Dockerfile 并写入如下内 ...

  2. Docker--Dockerfile引用及指令集的功能用法

    Dockerfile引用的官网文档:https://docs.docker.com/engine/reference/builder/ 编写Dockerfiles的最佳实践的官网文档:https:// ...

  3. Docker-Compose入门-(转载)

    Compose 是一个用户定义和运行多个容器的 Docker 应用程序.在 Compose 中你可以使用 YAML 文件来配置你的应用服务.然后,只需要一个简单的命令,就可以创建并启动你配置的所有服务 ...

  4. docker-compose入门--翻译

    在这一页,你将学习到如何构建一个简单的python的web应用,并通过Docker compose来运行.这个应用程序使用的是Flask框架,并维护着一个存储在reids里的点击计数器.由于这个案例使 ...

  5. 如何编写最佳的Dockerfile

    译者按: Dockerfile 的语法非常简单,然而如何加快镜像构建速度,如何减少 Docker 镜像的大小却不是那么直观,需要积累实践经验.这篇博客可以帮助你快速掌握编写 Dockerfile 的技 ...

  6. docker 应用-2(Dockerfile 编写以及镜像保存提交)

    我们可以从docker hub上pull别人的镜像,也可以将容器进行修改,然后commit镜像,并把镜像push到docker hub上被被人使用.但是,直接pull或者push镜像的方式太过笨重,尤 ...

  7. Dockerfile 编写

    转: https://blog.fundebug.com/2017/05/15/write-excellent-dockerfile/如何编写最佳的Dockerfile 译者按: Dockerfile ...

  8. Docker的简介以及Dockerfile编写与使用

    Docker的简介 Docker是在容器的基础上,进行了进一步的封装,极大的简化了容器的创建和维护.使得Docker技术比虚拟机技术更为轻便.快捷. 下面是两张对比图. 可以看到传统虚拟机技术是虚拟出 ...

  9. 自己编写k8s

    ## 基于Docker和Kubernetes的企业级DevOps实践训练营 ### 课程准备 1. 离线镜像包 百度:https://pan.baidu.com/s/1N1AYGCYftYGn6L0Q ...

随机推荐

  1. python中的字符串编码问题——1.理解编码和解码问题

    理解编码与解码(python2.7):1)编码 是根据一个想要的编码名称,把一个字符串翻译为其原始字节形式.>>> u_str=u'字符串编码aabbbcccddd'>> ...

  2. LeetCode题解之Peak Index in a MountainArray

    1 题目描述 2.问题分析 直接从后向前遍历,找到 A[i] > A[i-1] 即可. 3.代码 int peakIndexInMountainArray(vector<int>&a ...

  3. 【MySQL】无法启动mysql服务(位于本地计算机上)错误1067,进程意外中止

    好久没看MySQL了,今天启动起来找找感觉,尴尬了...发现服务启动不了.系统提示:无法启动mysql服务(位于本地计算机上)错误1067,进程意外中止. 解决过程: 1.在网上百度好久,看到一条解决 ...

  4. layui和bootstrap对比

    layui和bootstrap 对比 这两个都属于UI渲染框架. layui是国人开发的一套框架,2016年出来的,现在已更新到2.X版本了.比较新,轻量级,样式简单好看. bootstrap 相对来 ...

  5. [翻译] PTEHorizontalTableView

    PTEHorizontalTableView Horizontal UITableView inspired by EasyTableView. 水平滚动的UITableView,灵感来自于EasyT ...

  6. 使用 Bulk Copy 将大量数据复制到数据库

    如果一次要向数据库服务器提交多条记录 , 通常会执行多次Insert命令 , 这样就为要插入的每个记录执行一次与数据库服务器的往返 , 这就给服务器增加了压力 , 效率也大大的降低了... .Net  ...

  7. BZOJ 3171 循环格 最小费用流

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3171 题目大意: 一个循环格就是一个矩阵,其中所有元素为箭头,指向相邻四个格子.每个元 ...

  8. shell基础--test命令的使用

    test :用于文件类型检查和变量比较 一.用途: 1.判断表达式 2.判断字符串 3.判断整数 4.判断文件 测试例子: (1).test [root@~_~ day5]# cat test.sh ...

  9. 微信小程序 动态加载class

    <view class=' {{topid==1?"top": "untop"}}' >

  10. C. Serval and Parenthesis Sequence 【括号匹配】 Codeforces Round #551 (Div. 2)

    冲鸭,去刷题:http://codeforces.com/contest/1153/problem/C C. Serval and Parenthesis Sequence time limit pe ...