This repository has been archived by the owner on Mar 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
|
||
|
||
|
||
|
||
Dockerfile指令 | ||
========================================= | ||
|
||
.. toctree:: | ||
:maxdepth: 2 | ||
|
||
dockerfile/CMD | ||
dockerfile/RUN |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
# CMD | ||
|
||
参考:[CMD](https://docs.docker.com/engine/reference/builder/#cmd) | ||
|
||
一个`Dockerfile`文件仅能执行一条`CMD`指令。如果存在多条`CMD`指令,仅最后一个`CMD`指令起作用 | ||
|
||
## 语法 | ||
|
||
`CMD`指令有`3`种书写格式: | ||
|
||
1. `CMD ["executable","param1","param2"]`(`exec`形式,推荐) | ||
2. `CMD ["param1","param2"]`(`ENTRYPOINT`指令的默认参数) | ||
3. `CMD command param1 param2`(`shell`形式) | ||
|
||
## 作用 | ||
|
||
`CMD`指令的主要目的是提供容器默认操作。可以通过`CMD`指定一个可执行文件,如果在`Dockerfile`中指定了`ENTRYPOINT`,那么`CMD`指定的可执行文件会被忽略 | ||
|
||
**注意:如果`CMD`指令用于提供`ENTRYPOINT`指令参数,那么两者必须按`JSON`数组格式编写** | ||
|
||
**注意:如果使用`exec`格式,其将会解析成`JSON`数组,所以必须使用双引号而不是单引号** | ||
|
||
调用`shell`命令: | ||
|
||
* `exec`形式的`CMD`指令不会调用命令行`shell`,所以如果要使用`shell`命令,必须显示调用`shell`,比如`CMD [ "sh", "-c", "echo $HOME" ]` | ||
* `shell`形式的`CMD`指令可以直接调用,比如`CMD echo $HOME` | ||
|
||
调用非`shell`命令,必须使用`exec`形式,且使用命令绝对路径,比如`CMD ["/usr/bin/wc","--help"]` | ||
|
||
**如果在运行`docker run`时指定了运行命令,将会覆盖`CMD`指令操作** | ||
|
||
## RUN vs. CMD | ||
|
||
* `RUN`用于在镜像构建时运行,并提交运行结果 | ||
* `CMD`在构建时不执行任何操作,但指定镜像的预期命令 | ||
|
||
## 示例一 | ||
|
||
## 示例二 | ||
|
||
## 示例三 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
# RUN | ||
|
||
`RUN`指令在当前镜像上执行操作,然后将提交结果为新的镜像,作用于`Dockerfile`的下一步 | ||
|
||
## 语法 | ||
|
||
`RUN`指令有`2`种格式: | ||
|
||
* `RUN <command>`(`shell`形式,命令运行在一个`shell`上。对`Linux`而言,默认是`/bin/sh -c`;对`Windows`而言,默认是`cmd /S /C`) | ||
* `RUN ["executable", "param1", "param2"]`(`exec`形式) | ||
|
||
注意一:使用`shell`形式,可以使用反斜线(`\, backslash`)将单行`RUN`指令扩展成多行,有助于配置和理解 | ||
|
||
``` | ||
RUN apt-get update && \ | ||
apt-get install -f \ | ||
apt-get install -y net-tools | ||
``` | ||
|
||
注意二:`exec`形式必须使用双引号而不是单引号 | ||
|
||
注意三:`exec`形式的`RUN`指令不会调用命令`shell`,必须显式调用`shell` | ||
|
||
``` | ||
RUN ["sh", "-c", "echo hello"] | ||
``` | ||
|
||
对于`Windows`系统而言,需要转义反斜杠,比如`RUN ["c:\\windows\\system32\\tasklist.exe"]` | ||
|
||
## 缓存 | ||
|
||
`RUN`指令的缓存不会在下一个生成期间自动失效,所以`RUN apt-get dist-upgrade -y`的缓存将在下次构建时使用 | ||
|
||
设置缓存内容失效,需要在构建时设置标识符`--no-cache`,比如`docker build --no-cache .` | ||
|
||
## 不同shell | ||
|
||
如果要使用不同`shell`,操作如下: | ||
|
||
* 使用绝对路径指定新的`shell` | ||
* 使用`SHELL`指令更新 | ||
|
||
``` | ||
RUN ["/bin/bash", "-c", "echo hello"] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ docker使用指南 | |
|
||
basic | ||
advanced | ||
dockerfile | ||
|
||
FAQ | ||
|
||
|