Dockerfile

Docker Hub拥有大量高质的官方镜像:可直接使用的服务类镜像、语言应用镜像、基础操作系统镜像等,满足绝大部分需求。

此外,可以通过定制镜像的方式来满足实际使用中的特定需求。

定制镜像实际上就是以一个镜像为基础,定制每一层的配置和文件。

可以选择现有镜像为基础镜像,也可以选择scratch镜像(虚拟的概念,并不实际存在,表示一个空白的镜像)。

Dockerfile是包含了新镜像创建过程中的每一层修改、安装、构建、操作指令的文本格式脚本。每一条指令(Instruction)构建一层,描述该层应当如何构建。

Dockerfile支持\的换行方式和行首#的注释格式。良好的指令格式会让维护和排障更简便。

镜像是多层存储,每一层的内容并不会在下一层被删除,会一直伴随镜像。

因此镜像构建时,确保每一层只添加真正需要的内容,清理任何无关的文件和配置。

Dockerfile的常用指令

Dockerfile中的指令是通过docker build命令来执行的。

更多信息可参考:Dockerfile reference

- FROM 指定基础镜像。
Dockerfile中的必备指令,也是第一条指令。
“FROM scratch”表示选择空白镜像为基础镜像,所写的指令将作为镜像第一层开始存在,适合Linux下静态编译的程序,会让镜像体积更加小巧。
- RUN 执行命令。
每一个RUN指令都会新建立一层,在这一层执行对应的命令,然后commit这一层的修改,构成新的镜像。
为了简洁快速构建和满足低于Union FS的最大层数限制,可以在一个RUN指令里使用`&&`串联多个命令,从而将多层简化为一层。

docker build

Build an image from a Dockerfile
Usage: docker build [OPTIONS] PATH | URL | -

注意事项

  1. 必须使用PATH参数指定上下文目录。

    Docker是C/S结构,执行docker build命令其实是在服务端(Docker引擎)中构建的,而不是在本地构建。

    镜像构建时,通过指定上下文目录的方式,可以让服务端获得本地文件,从而能够构建包含本地文件的镜像。简而言之,需要加入镜像的本地文件必须存在于上下文目录中。

    docker build命令会将用户指定的上下文目录的内容打包并上传给Docker引擎。Docker引擎接收并展开就可以获得构建镜像所需的本地文件。
  2. 在上下文目录中,可以通过.dockerignore(语法类似.gitignore)排除不需要加入镜像的内容。
  3. COPY、ADD等指令中的源文件路径,使用的都是上下文(PATH)目录的相对路径。
  4. 如不指定dockerfile,Docker默认文件名为Dockerfile并放置于上下文目录中。

其它用法

  • 从URL构建
  • 用给定的tar压缩包构建:Docker引擎下载压缩包并自动解压缩,以其作为上下文开始构建。
  • 从标准输入中读取Dockerfile进行构建(缺少上下文,无法执行COPY等指令):docker build - < Dockerfilecat Dockerfile | docker build -
  • 从标准输入中读取上下文压缩包进行构建:docker build - < context.tar.gz

示例

编写dockfile

[root@CentOS7 docker]# pwd
/tmp/docker
[root@CentOS7 docker]# ls -l
总用量 0
drwxr-xr-x 2 root root 24 5月 8 23:52 data
drwxr-xr-x 3 root root 60 5月 9 00:13 test
[root@CentOS7 docker]# tree
.
├── data
│   └── sample.txt
└── test
├── aliyun-sources.txt
├── buildfile
└── dir
└── messages.txt 3 directories, 4 files
[root@CentOS7 docker]#
[root@CentOS7 docker]# cat /tmp/docker/data/sample.txt
1234567890
[root@CentOS7 docker]#
[root@CentOS7 docker]# cat /tmp/docker/test/aliyun-sources.txt
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted
deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse
[root@CentOS7 docker]#
[root@CentOS7 docker]# cat /tmp/docker/test/buildfile
# this is an example of Docker build.
FROM ubuntu
MAINTAINER anliven "anliven@yeah.net"
RUN echo 'Run docker build - create file!' > /tmp/file-create.log
COPY aliyun-sources.txt /etc/apt/sources.list
COPY dir/messages.txt /tmp/file-copy.log
VOLUME /tmp/docker/data
# ENV http_proxy="http://10.144.1.10:8080"
RUN apt-get update \
&& apt-get install -y inetutils-ping iproute net-tools \
&& apt-get install -y vim \
&& apt-get purge -y --auto-remove
[root@CentOS7 docker]#
[root@CentOS7 docker]# cat /tmp/docker/test/dir/messages.txt
Run docker build - copy file!
[root@CentOS7 docker]#

定制镜像

[root@CentOS7 ~]# docker build --file /tmp/docker/test/buildfile --tag ubuntu:test /tmp/docker/test/
Sending build context to Docker daemon 5.12 kB
Step 1 : FROM ubuntu
---> f7b3f317ec73
Step 2 : MAINTAINER anliven <anliven@yeah.net>
---> Running in 7c1140a0cc72
---> 9a0fb45df847
Removing intermediate container 7c1140a0cc72
Step 3 : RUN echo 'Run docker build - create file!' > /tmp/file-create.log
---> Running in 33bb7a725234
---> 866235e56f75
Removing intermediate container 33bb7a725234
Step 4 : COPY aliyun-sources.txt /etc/apt/sources.list
---> 6de0504452f5
Removing intermediate container e98687ed3e37
Step 5 : COPY dir/messages.txt /tmp/file-copy.log
---> 8def1507d4f3
Removing intermediate container 66e68d3efc2d
Step 6 : VOLUME /tmp/docker/data
---> Running in 10cca5bef10e
---> 6e887fff9079
Removing intermediate container 10cca5bef10e
Step 7 : RUN apt-get update && apt-get install -y inetutils-ping iproute net-tools && apt-get install -y vim && apt-get purge -y --auto-remove
---> Running in 4c54d21066cd
Get:1 http://mirrors.aliyun.com/ubuntu xenial InRelease [247 kB]
Get:2 http://mirrors.aliyun.com/ubuntu xenial-updates InRelease [102 kB]
Get:3 http://mirrors.aliyun.com/ubuntu xenial-backports InRelease [102 kB]
......
......
......
Processing triggers for libc-bin (2.23-0ubuntu7) ...
Reading package lists...
Building dependency tree...
Reading state information...
0 upgraded, 0 newly installed, 0 to remove and 9 not upgraded.
---> 62b945705a30
Removing intermediate container 4c54d21066cd
Successfully built 62b945705a30
[root@CentOS7 ~]#

验证

[root@CentOS7 ~]# docker images ubuntu
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu test 62b945705a30 About a minute ago 202.1 MB
docker.io/ubuntu latest f7b3f317ec73 13 days ago 117.3 MB
[root@CentOS7 ~]#
[root@CentOS7 ~]# docker inspect --format "{{ .Config.Volumes }}" ubuntu:test
map[/tmp/docker/data:{}]
[root@CentOS7 ~]#
[root@CentOS7 ~]# docker history ubuntu:test
IMAGE CREATED CREATED BY SIZE COMMENT
62b945705a30 5 minutes ago /bin/sh -c apt-get update && apt-get inst 84.85 MB
6e887fff9079 6 minutes ago /bin/sh -c #(nop) VOLUME [/tmp/docker/data] 0 B
8def1507d4f3 6 minutes ago /bin/sh -c #(nop) COPY file:45739c777f02cabe8 30 B
6de0504452f5 6 minutes ago /bin/sh -c #(nop) COPY file:3d19f8187c6e93e48 655 B
866235e56f75 6 minutes ago /bin/sh -c echo 'Run docker build - create fi 32 B
9a0fb45df847 6 minutes ago /bin/sh -c #(nop) MAINTAINER anliven <anlive 0 B
f7b3f317ec73 13 days ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B
<missing> 13 days ago /bin/sh -c mkdir -p /run/systemd && echo 'doc 7 B
<missing> 13 days ago /bin/sh -c sed -i 's/^#\s*\(deb.*universe\)$/ 2.759 kB
<missing> 13 days ago /bin/sh -c rm -rf /var/lib/apt/lists/* 0 B
<missing> 13 days ago /bin/sh -c set -xe && echo '#!/bin/sh' > /u 745 B
<missing> 13 days ago /bin/sh -c #(nop) ADD file:141408db9037263a47 117.3 MB
[root@CentOS7 ~]#
[root@CentOS7 ~]# docker run -it ubuntu:test
root@702e453e458f:/#
root@702e453e458f:/# ls -l /tmp
total 8
drwxr-xr-x 3 root root 18 May 8 16:33 docker
-rw-r--r-- 1 root root 30 May 8 15:53 file-copy.log
-rw-r--r-- 1 root root 32 May 8 16:33 file-create.log
root@702e453e458f:/#
root@702e453e458f:/# cat /tmp/file-copy.log
Run docker build - copy file!
root@702e453e458f:/# cat /tmp/file-create.log
Run docker build - create file!
root@702e453e458f:/#
root@702e453e458f:/# ls -l /tmp/docker/
total 0
drwxr-xr-x 2 root root 6 May 8 16:40 data
root@702e453e458f:/# ls -l /tmp/docker/data/
total 0
root@702e453e458f:/#
root@702e453e458f:/# dpkg --list inetutils-ping |grep ii
ii inetutils-ping 2:1.9.4-1build1 amd64 ICMP echo tool
root@702e453e458f:/#
root@702e453e458f:/# dpkg --list iproute |grep ii
ii iproute 1:4.3.0-1ubuntu3 all transitional dummy package for iproute2
root@702e453e458f:/#
root@702e453e458f:/# dpkg --list net-tools |grep ii
ii net-tools 1.60-26ubuntu1 amd64 NET-3 networking toolkit
root@702e453e458f:/#
root@702e453e458f:/# exit
[root@CentOS7 ~]#

参考

如何使用Dockerfile构建镜像

Docker - 定制镜像的更多相关文章

  1. Docker定制镜像

    定制镜像 除了使用定制好的镜像外,我们也可以通过定制实现符合自己环境的镜像. 在docker里面通过build方法来生成镜像,在生成镜像之前,我们需要一个Dockerfile脚本,脚本中包含的是一条一 ...

  2. 微服务架构 ------ Dockerfile定制镜像

    Docker容器不仅仅是运行原生的容器,而是把我们的具体的项目能够布置到容器上面去,这就是Docker定制镜像需要做的事情.  Docker容器 = new Docker镜像  镜像相当于类,容器相当 ...

  3. Docker入门之--定制镜像

    1. 首先定制一个Web 服务器为例 1.1 启动镜像 执行下面命令 docker run --name webserver -d -p 80:80 nginx 1.2 查看容器和镜像状态 然后执行下 ...

  4. docker自定制镜像

    概述 很多情况下我们需要自定制镜像,如果自定制过程中需要下载配置很多包,而且这些包之间还有依赖关系,那么如果我们手动去操作的话就会很麻烦,正确的做法是把操作的命令封装到一个文件里,然后直接执行这个文件 ...

  5. docker之常用命令、自定制镜像、公(私)仓库的上传和下载

    一.docker命令 1.参数和命令汇总 1. 参数 Options: --config=~/.docker Location of client config files #客户端配置文件的位置 - ...

  6. Docker学习笔记三 Dockerfile 指令 定制镜像

    本文地址:https://www.cnblogs.com/veinyin/p/10412079.html  镜像是分层存储的,每一层都是独立存在的,修改当前层并不会修改其依赖的上一层,删除某一层也只是 ...

  7. Docker Dockerfile 定制镜像(转)

    转自: https://yeasy.gitbooks.io/docker_practice/ 及 https://blog.csdn.net/wo18237095579/article/details ...

  8. 【Docker自定制镜像之Dockerfile】

    镜像的定制,就是定制每一层所添加的配置.文件,如果可以把每一层修改.安装.构建.操作的命令都写入到一个脚本中,用脚本来构建.定制镜像,这个脚本就是Dockerfile Dockerfile是一个文本文 ...

  9. Docker 学习笔记(二):Dockerfile 定制镜像

    镜像的定制实际上就是定制每一层所添加的配置.文件. 如果我们可以把每一层修改.安装.构建.操作的命令都写入一个脚本,用这个脚本来构建.定制镜像,那么之前提及的无法重复的问题.镜像构建透明性的问题.体积 ...

随机推荐

  1. 使sublimetext3在ubuntu下可以打中文和在windows的dos命令行下正常显示中文

    学习闲暇之余,总结一下在windows和ubuntu下使用sublimetext3遇到的问题 一.关于sublimetext3在windows的dos命令行下不能编译运行中文的解决方案: 因为dos命 ...

  2. iOS用户行为追踪——无侵入埋点

    本文章系作者原创文章,如需转载学习,请注明该文章的原始出处和网址链接.  在阅读的过程中,如若对该文章有不懂或值得优化的建议,欢迎大家加QQ:690091622 进行技术交流和探讨. 前言:  前几日 ...

  3. java与xml之间的转换(jaxb)

    使用java提供的JAXB来实现java到xml之间的转换,先创建两个持久化的类(Student和Classroom): Classroom: package com.model; public cl ...

  4. 【干货分享】sketch 前端开发常用技巧总结

    sketch横空出世,移动端的应用越来越多的采用sketch来做,前端开发也需要掌握更多sketch技巧. (1) sketch导出图片时,如何快速选择多个图层? 1. 在画布上任一点单击并拖拽出一个 ...

  5. linux命令之查看字符集

    lucifer@abc:~$ locale -a 查看本地字符集lucifer@abc:~$ locale -m 查看所有支持的字符集将文件从gb2312转为utf8iconv -f gb2312 - ...

  6. 老李分享:loadrunne动态查询db2数据库

    老李分享:loadrunne动态查询db2数据库   poptest老李认为性能测试脚本开发不仅仅涉及到脚本开发的技术层面,同时也要对一些其他技术有所了解动态链接库的技术,线程安全等等,建议在做下面的 ...

  7. 老李推荐:第2章3节《MonkeyRunner源码剖析》了解你的测试对象: NotePad窗口Activity之NoteEditor简介

    老李推荐:第2章3节<MonkeyRunner源码剖析>了解你的测试对象: NotePad窗口Activity之NoteEditor简介   我们在增加和编辑一个日记的时候会从NotesL ...

  8. Influxdb1.2.2安装_Windows

    一.文件准备 1.1 文件名称 influxdb-1.2.2_windows_amd64.zip 1.2 下载地址 https://portal.influxdata.com/downloads [注 ...

  9. Java 中的 String 类常用方法

    字符串广泛应用在Java编程中,在Java中字符串属于对象,String 类提供了许多用来处理字符串的方法,例如,获取字符串长度.对字符串进行截取.将字符串转换为大写或小写.字符串分割等. Strin ...

  10. javaScript 基础学习笔记

    边看视频和书记得有点杂. 1.插入JS标签 一种是在文档中插入<script></script>标签.另一种是把javaScript代码放在.js文件中.放在head中如. & ...