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 定制镜像
镜像的定制实际上就是定制每一层所添加的配置.文件. 如果我们可以把每一层修改.安装.构建.操作的命令都写入一个脚本,用这个脚本来构建.定制镜像,那么之前提及的无法重复的问题.镜像构建透明性的问题.体积 ...
随机推荐
- Linux中的grep命令
grep - print lines matching a pattern 参数: -a 将binary文件以text文件的方式查找 -i 忽略大小写 --color=zuto 加颜色匹配字符串 -v ...
- Windows Mobile 常用键值VK对应表
#define VK_TSOFT2 VK_F2 // Softkey 2 #define VK_TTALK VK_F3 // Talk ...
- 让Xcode日志输出中文
有的时候xcode打印后台返回的日志,明明后台返回的是中文,但是在xcode的日志里面却不是中文,而是unicode编码,这个就比较坑,因为看不到内容. 其实解决办法有两种: 第一种就是给xcode安 ...
- zookeeper的安装与部署-集群
1.Zookeeper的下载与解压 通过后面的链接下载Zookeeper: Zookeeper下载在此我们下载zookeeper-3.4.5下载后解压至安装目录下,本文我们解压到目录:/ ...
- python_21_线程+进程+协程
python_线程_进程_协程 什么是线程? -- os能够进行运算调度的最小单位,被包含在进程之中,是一串指令的集合 -- 每个线程都是独立的,可以访问同一进程下所有的资源 什么是进程? -- 每个 ...
- Spring基础学习(一)—初识Spring
一.Spring的使用 1.导入jar包 2.编写实体类 Person.java public class Person{ private String name; public void say() ...
- Andorid开发中如何去除标题栏title
有两种方法可以去除标题栏的title. 1.在代码中实现 在setContentView()方法之前加上这一句 requestWindowFeature(Window.FEATURE_NO_TITLE ...
- MCDownloader(iOS下载器)说明书
示例 前言 很多iOS应用中都需要下载数据,并对这些下载的过程和结果进行管理,因此我才有了写这个MCDownloader的想法.在IOS 文件下载器-MCDownloadManager这篇文章中,我使 ...
- JavaScript 格式化时间
//格式化 yyyy-MM-dd hh:mm:ss function renderTime(date) { if (date == '' || date == null) { return ''; } ...
- sublime text3 在ubutun下的下载和配置
最近在学习 Javascript,在 w3c school 上把教程看完了,也算个刚刚入门的水平,一直都是在 win 系统 上练习. 但是因为写 python 代码的 pycharm 和 git 配置 ...