doc: Update docker.md dockerfile cheatsheet.

This commit is contained in:
jaywcjlove 2022-10-01 21:20:19 +08:00
parent df6d94b300
commit f7f71e5070
3 changed files with 69 additions and 14 deletions

View File

@ -12,7 +12,7 @@ Quick Reference
[JavaScript](./docs/javascript.md)<!--rehype:style=background: rgb(203 183 31/var(\-\-bg\-opacity));--> [JavaScript](./docs/javascript.md)<!--rehype:style=background: rgb(203 183 31/var(\-\-bg\-opacity));-->
[JSON](./docs/json.md)<!--rehype:style=background: rgb(57 59 60/var(\-\-bg\-opacity));--> [JSON](./docs/json.md)<!--rehype:style=background: rgb(57 59 60/var(\-\-bg\-opacity));-->
[TOML](./docs/toml.md)<!--rehype:style=background: rgb(132 132 132/var(\-\-bg\-opacity));--> [TOML](./docs/toml.md)<!--rehype:style=background: rgb(132 132 132/var(\-\-bg\-opacity));-->
[Markdown](./docs/markdown.md)<!--rehype:style=background: rgb(78 57 104/var(\-\-bg\-opacity));--> [Markdown](./docs/markdown.md)<!--rehype:style=background: rgb(103 61 156/var(\-\-bg\-opacity));-->
[TypeScript](./docs/typescript.md)<!--rehype:style=background: rgb(49 120 198/var(\-\-bg\-opacity));--> [TypeScript](./docs/typescript.md)<!--rehype:style=background: rgb(49 120 198/var(\-\-bg\-opacity));-->
<!--rehype:class=home-card--> <!--rehype:class=home-card-->

View File

@ -295,4 +295,5 @@ $ docker volume prune
---- ----
- [Dockerfile 备忘清单](./dockerfile.md) _(github.io)_ - [Dockerfile 备忘清单](./dockerfile.md) _(github.io)_
- [Docker 官方入门教程](https://docs.docker.com/get-started/) _(docker.com)_ - [Docker 官方入门教程](https://docs.docker.com/get-started/) _(docker.com)_
- [Docker入门学习笔记](https://jaywcjlove.github.io/docker-tutorial) _(github.io)_

View File

@ -32,7 +32,7 @@ FROM ruby:2.2.2
FROM golang:1.19-alpine3.16 AS build-env FROM golang:1.19-alpine3.16 AS build-env
``` ```
### 变量 ### 变量 ENV
```dockerfile ```dockerfile
ENV <key>=<value> ... ENV <key>=<value> ...
@ -55,10 +55,14 @@ ENV MY_NAME="John Doe" MY_DOG=Rex\ The\ Dog \
RUN bundle install RUN bundle install
``` ```
`WORKDIR` 指令为任何 `RUN``CMD``ENTRYPOINT``COPY``ADD` 指令设置工作目录。
```dockerfile ```dockerfile
WORKDIR /myapp WORKDIR /myapp
``` ```
`VOLUME` 指令创建一个具有指定名称的挂载点,并将其标记为保存来自本机主机或其他容器的外部挂载卷。
```dockerfile ```dockerfile
VOLUME ["/data"] VOLUME ["/data"]
# 安装点规范 # 安装点规范
@ -66,6 +70,7 @@ VOLUME ["/data"]
```dockerfile ```dockerfile
ADD file.xyz /file.xyz ADD file.xyz /file.xyz
# 复制
COPY --chown=user:group host_file.xyz /path/container_file.xyz COPY --chown=user:group host_file.xyz /path/container_file.xyz
``` ```
<!--rehype:className=wrap-text --> <!--rehype:className=wrap-text -->
@ -75,21 +80,20 @@ COPY --chown=user:group host_file.xyz /path/container_file.xyz
```dockerfile ```dockerfile
ONBUILD RUN bundle install ONBUILD RUN bundle install
# 与另一个文件一起使用时 # 与另一个文件一起使用时
```
### 命令 ONBUILD ADD . /app/src
ONBUILD RUN /usr/local/bin/python-build --dir /app/src
```dockerfile
EXPOSE 5900
CMD ["bundle", "exec", "rails", "server"]
``` ```
<!--rehype:className=wrap-text -->
指令将触发指令添加到镜像中,以便稍后执行,此时镜像用作另一个构建的基础。
### 在严格的 shell 中运行命令 ### 在严格的 shell 中运行命令
```dockerfile ```dockerfile
ENV my_var ENV my_var
SHELL ["/bin/bash", "-euo", "pipefail", "-c"] SHELL ["/bin/bash", "-euo", "pipefail", "-c"]
# With strict mode: # 使用严格模式:
RUN false # ails 像使用 && 一样构建 RUN false # ails 像使用 && 一样构建
RUN echo "$myvar" # 由于拼写错误会抛出错误 RUN echo "$myvar" # 由于拼写错误会抛出错误
RUN true | false # 将脱离管道 RUN true | false # 将脱离管道
@ -98,7 +102,21 @@ RUN true | false # 将脱离管道
使用 `shell` 将为 shell 命令打开严格模式。 使用 `shell` 将为 shell 命令打开严格模式。
### 入口点 ### 命令 CMD
<!--rehype:wrap-class=col-span-2-->
:- | -
:- | -
`CMD ["executable","param1","param2"]` | (exec 形式,这是首选形式)
`CMD ["param1","param2"]` | (作为 ENTRYPOINT 的默认参数)
`CMD command param1 param2` | (shell形式)
```dockerfile
EXPOSE 5900
CMD ["bundle", "exec", "rails", "server"]
```
### 入口点 ENTRYPOINT
```dockerfile ```dockerfile
ENTRYPOINT ["executable", "param1", "param2"] ENTRYPOINT ["executable", "param1", "param2"]
@ -114,7 +132,7 @@ ENTRYPOINT exec top -b
这将使用 shell 处理来替换 shell 变量,并将忽略任何 `CMD``docker run` 命令行参数。 这将使用 shell 处理来替换 shell 变量,并将忽略任何 `CMD``docker run` 命令行参数。
### 元数据 ### 元数据 LABEL
```dockerfile ```dockerfile
LABEL version="1.0" LABEL version="1.0"
@ -135,6 +153,41 @@ LABEL multi.label1="value1" \
other="value3" other="value3"
``` ```
### ARG
```dockerfile
ARG <name>[=<default value>]
```
指令定义了一个变量,在构建时通过 `docker build` 命令使用 --build-arg `<varname>=<value>` 标志将其传递给构建器。
```dockerfile
FROM busybox
# user1 默认值为 someuser
ARG user1=someuser
ARG buildno=1
```
### .dockerignore 文件
```ignore
# 注释说明
*/temp*
*/*/temp*
temp?
```
----
:- | -
:- | -
`# comment` | 忽略
`*/temp*` | 在根的任何直接子目录中<br />排除名称以 `temp` 开头的文件和目录
`*/*/temp*` | 从根以下两级的任何子目录中<br />排除以 `temp` 开头的文件和目录
`temp?` | 排除根目录中名称为<br /> `temp` 的单字符扩展名的文件和目录
如果此文件存在,排除与其中的模式匹配的文件和目录,有利于避免 `ADD``COPY` 将敏感文件添加到镜像中。匹配是使用 Go 的 [filepath.Match](https://golang.org/pkg/path/filepath#Match) 规则完成的。
### 主要命令 ### 主要命令
<!--rehype:wrap-class=col-span-2 --> <!--rehype:wrap-class=col-span-2 -->
@ -152,7 +205,7 @@ LABEL multi.label1="value1" \
`EXPOSE <port> [<port>/<protocol>...]` | 运行时侦听指定的网络端口 `EXPOSE <port> [<port>/<protocol>...]` | 运行时侦听指定的网络端口
### 服务静态网站的最小 Docker 镜像 ### 服务静态网站的最小 Docker 镜像
<!--rehype:wrap-class=col-span-3--> <!--rehype:wrap-class=col-span-2-->
```dockerfile ```dockerfile
FROM lipanski/docker-static-website:latest FROM lipanski/docker-static-website:latest
@ -175,4 +228,5 @@ CMD ["/busybox", "httpd", "-f", "-v", "-p", "3000", "-c", "httpd.conf"]
## 也可以看看 ## 也可以看看
- [Dockerfile reference](https://docs.docker.com/engine/reference/builder/) _(docker.com)_ - [Dockerfile reference](https://docs.docker.com/engine/reference/builder/) _(docker.com)_
- [Docker 备忘清单](./docker.md) _(github.io)_ - [Docker 备忘清单](./docker.md) _(github.io)_
- [Docker入门学习笔记](https://jaywcjlove.github.io/docker-tutorial) _(github.io)_