|
|
|
@ -1,12 +1,14 @@
|
|
|
|
|
# Docker镜像学习
|
|
|
|
|
|
|
|
|
|
> [官网文档]()
|
|
|
|
|
|
|
|
|
|
## 镜像构建流程
|
|
|
|
|
|
|
|
|
|
![构建镜像](images/DockerBuild.png)
|
|
|
|
|
|
|
|
|
|
## 构建镜像的三种方法
|
|
|
|
|
|
|
|
|
|
+ 基于已有容器,使用 `docker commit` 命令构建
|
|
|
|
|
+ 基于现有容器,使用 `docker commit` 命令构建
|
|
|
|
|
|
|
|
|
|
``` shell
|
|
|
|
|
# 1 创建容器,执行相关操作,之后退出
|
|
|
|
@ -89,6 +91,82 @@
|
|
|
|
|
|
|
|
|
|
![build](images/build2.png)
|
|
|
|
|
|
|
|
|
|
## docker build 命令
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
# docker build 详解
|
|
|
|
|
# 语法
|
|
|
|
|
docker build [OPTIONS] PATH|URL|本地文本文件
|
|
|
|
|
|
|
|
|
|
# 参数可以是三种资源:Git存储库、tar压缩包和文本文件
|
|
|
|
|
git库示:
|
|
|
|
|
示例:docker build https://github.com/docker/rootfs.git#container:docker
|
|
|
|
|
|
|
|
|
|
Build Syntax Suffix Commit Used Build Context Used
|
|
|
|
|
myrepo.git refs/heads/master /
|
|
|
|
|
myrepo.git#mytag refs/tags/mytag /
|
|
|
|
|
myrepo.git#mybranch refs/heads/mybranch /
|
|
|
|
|
myrepo.git#pull/42/head refs/pull/42/head /
|
|
|
|
|
myrepo.git#:myfolder refs/heads/master /myfolder
|
|
|
|
|
myrepo.git#master:myfolder refs/heads/master /myfolder
|
|
|
|
|
myrepo.git#mytag:myfolder refs/tags/mytag /myfolder
|
|
|
|
|
myrepo.git#mybranch:myfolder refs/heads/mybranch /myfolder
|
|
|
|
|
|
|
|
|
|
tar包:
|
|
|
|
|
示例:docker build http://server/context.tar.gz
|
|
|
|
|
|
|
|
|
|
文件文件:
|
|
|
|
|
Powershell示例:Get-Content Dockerfile | docker build -
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Options
|
|
|
|
|
|
|
|
|
|
| 名称与速记 | 默认 | 描述 |
|
|
|
|
|
| ------------------------- | ------ | ------------------------------------------------------------ |
|
|
|
|
|
| `--add-host` | | 添加自定义主机到 IP 的映射 (host:ip) |
|
|
|
|
|
| `--build-arg` | | 设置构建时变量 |
|
|
|
|
|
| `--cache-from` | | 要考虑用作缓存源的图像 |
|
|
|
|
|
| `--cgroup-parent` | | 容器的可选父 cgroup |
|
|
|
|
|
| `--compress` | | 使用 gzip 压缩构建上下文 |
|
|
|
|
|
| `--cpu-period` | | 限制 CPU CFS(完全公平的调度程序)期限 |
|
|
|
|
|
| `--cpu-quota` | | 限制 CPU CFS(完全公平的调度程序)配额 |
|
|
|
|
|
| `--cpu-shares`,`-c` | | CPU 份额(相对权重) |
|
|
|
|
|
| `--cpuset-cpus` | | 允许执行的 CPU (0-3, 0,1) |
|
|
|
|
|
| `--cpuset-mems` | | 允许执行的 MEM (0-3, 0,1) |
|
|
|
|
|
| `--disable-content-trust` | `true` | 跳过镜像验证 |
|
|
|
|
|
| `--file`,`-f` | | Dockerfile 的名称(默认值为"PATH/Dockerfile") |
|
|
|
|
|
| `--force-rm` | | **始终删除中间容器** |
|
|
|
|
|
| `--iidfile` | | 将映像 ID 写入文件 |
|
|
|
|
|
| `--isolation` | | 容器隔离技术 |
|
|
|
|
|
| `--label` | | 设置图像的元数据 |
|
|
|
|
|
| `--memory`,`-m` | | 内存限制 |
|
|
|
|
|
| `--memory-swap` | | 交换限制等于内存加交换:"-1",用于启用无限制交换 |
|
|
|
|
|
| `--network` | | [**原料检修 1.25+**](https://docs.docker.com/engine/api/v1.25/) 在构建期间设置 RUN 指令的网络模式 |
|
|
|
|
|
| `--no-cache` | | 构建映像时不要使用缓存 |
|
|
|
|
|
| `--output`,`-o` | | [**1.40+ 原料药**](https://docs.docker.com/engine/api/v1.40/) 输出目标(格式:类型=本地,dest=路径) |
|
|
|
|
|
| `--platform` | | [**产品信息 1.38+**](https://docs.docker.com/engine/api/v1.38/) 如果服务器支持多平台,则设置平台 |
|
|
|
|
|
| `--progress` | `auto` | 设置进度输出类型(自动、普通、tty)。使用普通版显示容器输出 |
|
|
|
|
|
| `--pull` | | 始终尝试拉取较新版本的映像 |
|
|
|
|
|
| `--quiet`,`-q` | | 成功时禁止显示生成输出并打印图像 ID |
|
|
|
|
|
| `--rm` | `true` | **成功生成后删除中间容器** |
|
|
|
|
|
| `--secret` | | [**原料检修 1.39+**](https://docs.docker.com/engine/api/v1.39/) 要向构建版本公开的秘密文件(仅当启用 BuildKit 时):id=mysecret,src=/local/secret |
|
|
|
|
|
| `--security-opt` | | 安全选项 |
|
|
|
|
|
| `--shm-size` | | /dev/shm 的大小 |
|
|
|
|
|
| `--squash` | | [**实验性(守护进程)**](https://docs.docker.com/engine/reference/commandline/dockerd/#daemon-configuration-file)[**原料检修 1.25+**](https://docs.docker.com/engine/api/v1.25/) 将新构建的图层压缩为单个新图层 |
|
|
|
|
|
| `--ssh` | | [**原料检修 1.39+**](https://docs.docker.com/engine/api/v1.39/) 要向构建版本公开的 SSH 代理套接字或密钥(仅当启用了 BuildKit 时)(格式:default\|<id>[=<socket>\|<key>[,<key>]]) |
|
|
|
|
|
| `--stream` | | 流附加到服务器以协商生成上下文 |
|
|
|
|
|
| `--tag`,`-t` | | 名称和标签(可选)采用"名称:标签"格式 |
|
|
|
|
|
| `--target` | | 设置要生成的目标生成阶段。 |
|
|
|
|
|
| `--ulimit` | | 乌利米特选项 |
|
|
|
|
|
|
|
|
|
|
> **特别注意**:
|
|
|
|
|
>
|
|
|
|
|
> 不要将 根目录 用作生成上下文的 , 因为这会导致生成将硬盘驱动器的全部内容传输到 Docker 守护程序。
|
|
|
|
|
>
|
|
|
|
|
> 推荐作法:在空目录中放Dockerfile文件,把需要的文件全部复制到Dockerfile目录或子目录中。
|
|
|
|
|
|
|
|
|
|
## 构建环境
|
|
|
|
|
|
|
|
|
|
1. Dockerfile中所用的所有文件一定要和Dockerfile文件在同一级父目录下,可以为Dockerfile父目录的子目录
|
|
|
|
|