标签:-- 指令 构建 build 镜像 Docker Dockerfile
一、概念
1、基于容器生成镜像
通过 docker commit
命令将现有的容器提交来生成新的镜像。
原理:容器启动后的修改都保存在可写层,通过对可写层的修改生成新的镜像。
[root@hqs docker-hello]# docker commit --help
Usage: docker commit [OPTIONS选项] CONTAINER容器 [REPOSITORY仓库名[:TAG标签]]
Create a new image from a containers changes
Options:
-a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>") # 指定作者
-c, --change list Apply Dockerfile instruction to the created image # 允许使用dockerfile的指令
-m, --message string Commit message # 提交信息
-p, --pause Pause container during commit (default true) # 创建镜像过程中容器暂停(挂起)
基于容器生成镜像无法重复、构建缺乏透明性和体积偏大的问题,因此不推荐使用这种方式构建镜像。
2、Dockerfile 构建镜像
Dockerfile是由一系列指令和参数构成的脚本,每一条指令构建一层
,因此每一条指令的内容就是描述该层应当如何构建,一个Dockerfile包含了构建镜像的完整指令。
Dockerfile就是一个脚本来构建和定制镜像,把每一层的修改、安装、构建、操作都写入脚本。以此来解决体积、镜像构建透明等问题。
Dockerfile是一个文本文件,包含一条条指令(Instruction),每一条指令构建一层,每一条指令的内容,就是描述该层应当如何构建。
3、docker build命令
基于 dockerfile 构建镜像使用 docker build
命令。命令是通过 Dockerfile文件
和 构建上下文(Build Context)
来构建镜像的。
注意:
- 不要把多余的文件放到构建上下文中————一般就要创建一个空目录作为构建上下文
- 一般将 Dockerfile直接命名为
Dockerfile
,并放在上下文根目录,否则构建找不到(可以用-f指定) - Dockerfile的每个指令都被独立执行并创建一个新镜像。
# 语法
[root@localhost docker]# docker build --help
Usage: docker build [OPTIONS选项] PATH路径 | URL | -
Build an image from a Dockerfile
Options:
--add-host list Add a custom host-to-IP mapping (host:ip) # 添加映射
--build-arg list Set build-time variables # 设置镜像创建时的变量
--cache-from strings Images to consider as cache sources
--cgroup-parent string Optional parent cgroup for the container
--compress Compress the build context using gzip # 压缩构建的内容
--cpu-period int Limit the CPU CFS (Completely Fair Scheduler) period # 限制 CPU CFS周期
--cpu-quota int Limit the CPU CFS (Completely Fair Scheduler) quota # 限制 CPU CFS配额
-c, --cpu-shares int CPU shares (relative weight) # 设置cpu使用权重
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1) # 指定使用的CPU id
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1) # 指定使用的内存 id
--disable-content-trust Skip image verification (default true) # 忽略校验,默认开启
-f, --file string Name of the Dockerfile (Default is 'PATH/Dockerfile') # 设置Dockerfile名字
--force-rm Always remove intermediate containers # 总是删除中间容器
--iidfile string Write the image ID to the file
--isolation string Container isolation technology # 使用容器隔离技术
--label list Set metadata for an image # 设置镜像使用的元数据
-m, --memory bytes Memory limit # 内存限制
--memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap # 设置Swap的最大值为内存+swap,"-1"表示不限swap
--network string Set the networking mode for the RUN instructions during build (default "default") # 在构建期间设置RUN指令网络模式
--no-cache Do not use cache when building the image # 构建镜像不使用缓存
--pull Always attempt to pull a newer version of the image # 总是尝试拉取新版本镜像
-q, --quiet Suppress the build output and print image ID on success # 安静模式,成功后只打印镜像ID
--rm Remove intermediate containers after a successful build (default true) # 构建成功后删除中间容器
--security-opt strings Security options # 安全选项
--shm-size bytes Size of /dev/shm # /dev/shm大小
-t, --tag list Name and optionally a tag in the 'name:tag' format # 镜像的名字及标签
--target string Set the target build stage to build. # 目标构建阶段设为build
--ulimit ulimit Ulimit options (default []) # ?
# 最简案例
docker build .
4、Dockerfile格式
指令:
- 指令不区分大小写,建议大写。
- 指令可以指定若干参数。
- Docker按顺序执行指令。
- Dockerfile文件必须以
FROM
指令开头。
注释:
#
号开头的行都将被视为注释。解析器指令除外。- 行中其他位置的
#
视为参数的一部分(不视为注释)。
解析器指令:
- 不添加镜像,也不会出现在构建步骤。
- 语法:
# 指令 = 值
,e.g:escape=\
。 - 一旦注释、空行、解析器指令被处理,不再搜寻解析器指令,全格式化为注释。————》解析器指令必须在Dockerfile的首部。
5、.dockerignore文件
可以添加.dockerignore
文件到构建上下文中来定义要排除的文件和目录。
用途:构建镜像时,在将构建上下文传给docker daemon进程时,命令行接口就修改了上下文以排除匹配的文件或目录。有助于避免发送大型文件或敏感文件。
使用要点:
- 解释为换行符分隔的模式列表(一行一行读取)
标签:--,指令,构建,build,镜像,Docker,Dockerfile 来源: https://www.cnblogs.com/xiugeng/p/16074850.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。