Dockerfile & docker 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格式 案例:
在这里插入图片描述
执行顺序是从上到下,依次执行

指令 描述
FROM 构建新景象是基于那个镜像
MAINTAINER 镜像维护者姓名或邮箱地址
LABEL 给镜像添加信息。
RUN 构建镜像时运行的Shell命令
ADD 将本地文件添加到容器中
COPY 拷贝文件或目录到景象中
ENV 设置环境变量
USER 为RUN、CMD 和 ENTRYPOINT 执行命令指定运行用户
ARG 由外部启动时必须传入的参数,在容器启动时用–build-arg传递参数
EXPOSE 声明容器运行服务端口
HEALTHCHECK 容器中服务健康检查
VOLUME 用于指定持久化目录
WORKDIR 为 RUN、CMD、ENTRYPOINT、COPY 和 ADD 设置工作目录
ENTRYPOINT 运行容器时执行,如果有多个 ENTRYPOINT 命令,最后一个生效
CMD 运行容器时执行,如果有多个CMD指令,最后一个生效

Build镜像

docker build 是一条docker的命令,用于使用 Dockerfile 创建镜像。
Build方式需要写一个配置文件,然后利用当前是已存在的image,按照配置文件进行调整生成新的image。

参数格式:

扫描二维码关注公众号,回复: 10079538 查看本文章
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
								# 设置图像的元数据
-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选项(默认[])
  1. 看当前环境

    docker images
    

    看一下当前的image,确定新的image是基于哪个image,记下它的大版本号(REPOSITORY)和小版本号(TAG)。

  2. 写配置文件Dockerfile

    vim Dockerfile
    

    写入以下内容,我这里是安装了一个新 python 包 pyecharts

    FROM 基础大版本号:基础小版本号
    RUN ["pip", "install", "pyecharts"]
    
  3. BUILD新的Docker image

    docker build -t 新的大版本号: 新的小版本号 .
    

此时docker会从当前目录”.”中读取Dockerfile,并按其设置建立新的image

  1. 查看是否BUILD成功
    docker images
    

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

这个文章对于 Dockerfile 写的特别详细

发布了91 篇原创文章 · 获赞 174 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/weixin_44685869/article/details/104700269