Docker - 定制镜像
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 | -
注意事项
- 必须使用
PATH参数指定上下文目录。
Docker是C/S结构,执行docker build命令其实是在服务端(Docker引擎)中构建的,而不是在本地构建。
镜像构建时,通过指定上下文目录的方式,可以让服务端获得本地文件,从而能够构建包含本地文件的镜像。简而言之,需要加入镜像的本地文件必须存在于上下文目录中。
docker build命令会将用户指定的上下文目录的内容打包并上传给Docker引擎。Docker引擎接收并展开就可以获得构建镜像所需的本地文件。 - 在上下文目录中,可以通过.dockerignore(语法类似.gitignore)排除不需要加入镜像的内容。
- COPY、ADD等指令中的源文件路径,使用的都是上下文(PATH)目录的相对路径。
- 如不指定dockerfile,Docker默认文件名为Dockerfile并放置于上下文目录中。
其它用法
- 从URL构建
- 用给定的tar压缩包构建:Docker引擎下载压缩包并自动解压缩,以其作为上下文开始构建。
- 从标准输入中读取Dockerfile进行构建(缺少上下文,无法执行COPY等指令):
docker build - < Dockerfile或cat 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 ~]#
参考
Docker - 定制镜像的更多相关文章
- Docker定制镜像
定制镜像 除了使用定制好的镜像外,我们也可以通过定制实现符合自己环境的镜像. 在docker里面通过build方法来生成镜像,在生成镜像之前,我们需要一个Dockerfile脚本,脚本中包含的是一条一 ...
- 微服务架构 ------ Dockerfile定制镜像
Docker容器不仅仅是运行原生的容器,而是把我们的具体的项目能够布置到容器上面去,这就是Docker定制镜像需要做的事情. Docker容器 = new Docker镜像 镜像相当于类,容器相当 ...
- Docker入门之--定制镜像
1. 首先定制一个Web 服务器为例 1.1 启动镜像 执行下面命令 docker run --name webserver -d -p 80:80 nginx 1.2 查看容器和镜像状态 然后执行下 ...
- docker自定制镜像
概述 很多情况下我们需要自定制镜像,如果自定制过程中需要下载配置很多包,而且这些包之间还有依赖关系,那么如果我们手动去操作的话就会很麻烦,正确的做法是把操作的命令封装到一个文件里,然后直接执行这个文件 ...
- docker之常用命令、自定制镜像、公(私)仓库的上传和下载
一.docker命令 1.参数和命令汇总 1. 参数 Options: --config=~/.docker Location of client config files #客户端配置文件的位置 - ...
- Docker学习笔记三 Dockerfile 指令 定制镜像
本文地址:https://www.cnblogs.com/veinyin/p/10412079.html 镜像是分层存储的,每一层都是独立存在的,修改当前层并不会修改其依赖的上一层,删除某一层也只是 ...
- Docker Dockerfile 定制镜像(转)
转自: https://yeasy.gitbooks.io/docker_practice/ 及 https://blog.csdn.net/wo18237095579/article/details ...
- 【Docker自定制镜像之Dockerfile】
镜像的定制,就是定制每一层所添加的配置.文件,如果可以把每一层修改.安装.构建.操作的命令都写入到一个脚本中,用脚本来构建.定制镜像,这个脚本就是Dockerfile Dockerfile是一个文本文 ...
- Docker 学习笔记(二):Dockerfile 定制镜像
镜像的定制实际上就是定制每一层所添加的配置.文件. 如果我们可以把每一层修改.安装.构建.操作的命令都写入一个脚本,用这个脚本来构建.定制镜像,那么之前提及的无法重复的问题.镜像构建透明性的问题.体积 ...
随机推荐
- Java程序员应当知道的10个面向对象设计原则
面向对象设计原则是OOPS编程的核心, 但我见过的大多数Java程序员热心于像Singleton (单例) . Decorator(装饰器).Observer(观察者) 等设计模式,而没有把足够多的注 ...
- c#关于时间TimeHelper类的总结
using System; namespace DotNet.Utilities{ /// <summary> /// 时间类 /// 1.SecondToMinute( ...
- iOS 如何保持线程一直在运转(二)
一.接着上一篇通过NSThread可以方便的创建一个线程,并且启动线程的Runloop,在线程体中执行一个while循环 然后我们就可以方便得利用这个线程了 - (void)threadRun:(NS ...
- vue-miniQQ——基于Vue2实现的仿手机QQ单页面应用(接入了聊天机器人,能够进行正常对话)
使用Vue2进行的仿手机QQ的webapp的制作,作品由个人独立开发,源码中进行了详细的注释. 由于自己也是初学Vue2,所以注释写的不够精简,请见谅. 项目地址 https://github.com ...
- unity3d 中文乱码解决方法——cs代码文件格式批量转化UTF8
在Unity3d中经常会碰到中文乱码的问题,比如代码中的[AddComponentMenu("GameDef/AI/战机AI")],注释,中文文本等等 其原因在于,unity本身是 ...
- Akari谜题(关灯问题)的开灯解法
提高解时预处理的速度 本方法的结果是得到满足所有黑色有数字方块的一个带有未照亮的块的可能解集. 解集大小为 4~(3号块数量+1号块数量)+6~(2号块数量)-灯互相照射到的解的集合.集合中的灯为黄色 ...
- HDU-2298 Toxophily (三分法入门系列)
题意: 意大利炮射出炮弹的速度为v,求在(0,0)击中(x,y)处的目标,发射炮弹的角度. 题解: 设f(α)表示角度为α时, f(α) = vsin(α) * t - 4.9 * t * t ① ...
- 老李推荐:第6章1节《MonkeyRunner源码剖析》Monkey原理分析-事件源-事件源概览 2
事件要到那里去? 每个事件源处理类都维护着一个自己的事件队列, 在Monkey中叫做CommandQueue,里面装的是每个具体的MonkeyEvent事件.当来自网络的字串命令被翻译成对应的Monk ...
- bit ( 比特 )和 Byte(字节)的关系 以及 网速怎么算
今天来整理一下存储单位和网速的知识. 最近几天家里网不太好,所以就了解了一下网速和电脑的存储单位的关系. 一.存储单位的bit 和 Byte 1.bit(比特) bit也就是我们不一定听说过的比特,大 ...
- win7下用SSH连接linux虚拟机
本文来自转载:原文 [需求] 在win7环境下用SSH(SecureShell)连接本地的一台虚拟机上ubuntu(11.10)系统 [环境] win7,ubuntu,vmware(8.0) [方案 ...