Skip to content

Commit

Permalink
doc: Update docker.md dockerfile cheatsheet.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Oct 1, 2022
1 parent df6d94b commit f7f71e5
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Quick Reference
[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));-->
[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));-->
<!--rehype:class=home-card-->

Expand Down
3 changes: 2 additions & 1 deletion docs/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,5 @@ $ docker volume prune
----

- [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)_
78 changes: 66 additions & 12 deletions docs/dockerfile.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ FROM ruby:2.2.2
FROM golang:1.19-alpine3.16 AS build-env
```

### 变量
### 变量 ENV

```dockerfile
ENV <key>=<value> ...
Expand All @@ -55,17 +55,22 @@ ENV MY_NAME="John Doe" MY_DOG=Rex\ The\ Dog \
RUN bundle install
```

`WORKDIR` 指令为任何 `RUN``CMD``ENTRYPOINT``COPY``ADD` 指令设置工作目录。

```dockerfile
WORKDIR /myapp
```

`VOLUME` 指令创建一个具有指定名称的挂载点,并将其标记为保存来自本机主机或其他容器的外部挂载卷。

```dockerfile
VOLUME ["/data"]
# 安装点规范
```

```dockerfile
ADD file.xyz /file.xyz
# 复制
COPY --chown=user:group host_file.xyz /path/container_file.xyz
```
<!--rehype:className=wrap-text -->
Expand All @@ -75,21 +80,20 @@ COPY --chown=user:group host_file.xyz /path/container_file.xyz
```dockerfile
ONBUILD RUN bundle install
# 与另一个文件一起使用时
```

### 命令

```dockerfile
EXPOSE 5900
CMD ["bundle", "exec", "rails", "server"]
ONBUILD ADD . /app/src
ONBUILD RUN /usr/local/bin/python-build --dir /app/src
```
<!--rehype:className=wrap-text -->

指令将触发指令添加到镜像中,以便稍后执行,此时镜像用作另一个构建的基础。

### 在严格的 shell 中运行命令

```dockerfile
ENV my_var
SHELL ["/bin/bash", "-euo", "pipefail", "-c"]
# With strict mode:
# 使用严格模式:
RUN false # ails 像使用 && 一样构建
RUN echo "$myvar" # 由于拼写错误会抛出错误
RUN true | false # 将脱离管道
Expand All @@ -98,7 +102,21 @@ RUN true | false # 将脱离管道

使用 `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
ENTRYPOINT ["executable", "param1", "param2"]
Expand All @@ -114,7 +132,7 @@ ENTRYPOINT exec top -b

这将使用 shell 处理来替换 shell 变量,并将忽略任何 `CMD``docker run` 命令行参数。

### 元数据
### 元数据 LABEL

```dockerfile
LABEL version="1.0"
Expand All @@ -135,6 +153,41 @@ LABEL multi.label1="value1" \
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 -->

Expand All @@ -152,7 +205,7 @@ LABEL multi.label1="value1" \
`EXPOSE <port> [<port>/<protocol>...]` | 运行时侦听指定的网络端口

### 服务静态网站的最小 Docker 镜像
<!--rehype:wrap-class=col-span-3-->
<!--rehype:wrap-class=col-span-2-->

```dockerfile
FROM lipanski/docker-static-website:latest
Expand All @@ -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)_
- [Docker 备忘清单](./docker.md) _(github.io)_
- [Docker 备忘清单](./docker.md) _(github.io)_
- [Docker入门学习笔记](https://jaywcjlove.github.io/docker-tutorial) _(github.io)_

0 comments on commit f7f71e5

Please sign in to comment.