dockerfile介绍 和 docker build常用命令介绍

网友投稿 1395 2022-05-30

docker使用教程相关系列 目录

目录

什么是 dockerFile

docker镜像制作的工作逻辑

Dockerfile的基本结构

Dockerfile格式 案例:

Build镜像

我们之前学习,docker的镜像都是官方给定义好的,我们可不可以 自己创造镜像呢。

什么是 dockerfile

dockerfile,相当于是一个文档,客户可以基于dockerfile生成新的容器

dockerfile 是用来制作镜像的源码文件,是构建容器过程中的指令。

docker能够读取dockerfile的指定进行自动构建容器,基于dockerfile制作镜像

每一个指令都会创建一个镜像层,即镜像都是多层叠加而成,因此,层越多,效率越低,创建镜像,层越少越好。因此能在一个指令完成的动作尽量通过一个指令定义。

白话文:名为Dockerfile 的文件,里面写了 Dockerfile的必需存在的几点,然后build一下 他就变成了镜像。

docker镜像制作的工作逻辑

首先需要有一个制作镜像的目录,该目录下有个文件,

名称必须为Dockerfile,Dockerfile有指定的格式

这个是在docker的convention,如果用过C语言的makefile,应该就会了解。都是一些编程语言的约定。固定的名字,docker程序代码中写好的。

自己命名的话: docker build -t test -f dockerfile .

#号开头为注释。指定默认用大写字母来表示,以区分指令和参数。

docker build 读取Dockerfile是按顺序依次Dockerfile里的配置,且第一条非注释指令必须是FROM 开头,表示基于哪个基础镜像来构建新镜像。可以根据已存在的任意镜像来制作新镜像。

Dockerfile的基本结构

Dockerfile 一般分为四部分:

基础镜像信息

维护者信息

镜像操作指令

容器启动时执行指令

接下来详细介绍

‘#’ 是 Dockerfile 中的注释。

Docker以从上到下的顺序运行Dockerfile的指令。为了指定基本映像,第一条指令必须是FROM。一个声明以#字符开头则被视为注释。可以在Docker文件中使用RUN,CMD,FROM,EXPOSE,ENV等指令。

vim Dockerfile

Dockerfile格式 案例:

执行顺序是从上到下,依次执行

Build镜像

docker build 是一条docker的命令,用于使用 Dockerfile 创建镜像。

Build方式需要写一个配置文件,然后利用当前是已存在的image,按照配置文件进行调整生成新的image。

参数格式:

docker build [OPTIONS] PATH |URL| -[flags]

Options:

-t, --tag list # 镜像名称

-f, --fire string # 指定 Dockerfile文件位置

# Options 其他参数 docker build --help

Options:

--add-host list Add a custom host-to-IP mapping (host:ip)

# 添加自定义主机到ip的映射(主机: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

# 容器的可选父cgroup

--compress Compress the build context using gzip

# 使用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 (0- 3,0,1)

--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)

# 允许执行的MEMs (0- 3,0,1)

--disable-content-trust Skip image verification (default true)

# 跳过图像验证(默认为真)

-f, --file string Name of the Dockerfile (Default is 'PATH/Dockerfile')

# Dockerfile的名称(默认为‘PATH/Dockerfile’)

--force-rm Always remove intermediate containers

# 总是移除中间容器

--iidfile string Write the image ID to the file

# 将图像ID写入文件

--isolation string Container isolation technology

# 容器隔离技术

--label list Set metadata for an image

# 设置图像的元数据

Dockerfile介绍 和 docker build常用命令介绍

-m, --memory bytes Memory limit

# 存储容量极限 / 内存限制

--memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap

# 交换限制等于内存加交换:'-1'以启用无限交换

--network string Set the networking mode for the RUN instructions during build (default "default")

# 在构建期间为运行指令设置连网模式(默认为“default”)

--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)

# 成功构建后删除中间容器(默认为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

# 名称和“Name:tag”格式的标记(可选)

--target string Set the target build stage to build.

# 设置要构建的目标构建阶段。

--ulimit ulimit Ulimit options (default [])

# Ulimit选项(默认[])

Docker 容器

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Pandas实例|药品发放汇总与excel表数据回填
下一篇:04Spark 运行架构
相关文章