1.Dockerfile

Posted leleyao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1.Dockerfile相关的知识,希望对你有一定的参考价值。

1.docker build

  docker build 这个动作有一个context 上下文的概念

docker build -f /path/to/a/Dockerfile .
这个动作 通过 -f 参数 指出我们的 Dockerfile 位置,同时指定我们当前的工作目录为构建上下文。如果以当前的目录非常大(构建启动过程会变慢,还会浪费多余的mem)
docker build -t shykes/myapp:1.0.2 -t shykes/myapp:latest .
构建时 可以给指定的 镜像打上相应的tag
下面的例子

[[email protected] ~]# docker build -t weaveworks/scope:1.11.2 -t weaveworks/scope:latest -f /tmp/Dockerfile /tmp/
Sending build context to Docker daemon 119.8kB               传送安全上下文
Step 1/3 : FROM weaveworks/scope:1.11.2
1.11.2: Pulling from weaveworks/scope
Digest: sha256:4cdc0bac4e83f61f1675cd8fbe0709ff0df473b81977650ed3a26772ff3c627a
Status: Downloaded newer image for weaveworks/scope:1.11.2
---> d165af1c2e55
Step 2/3 : ENV http_proxy="proxy3.bj.petrochina:8080"
---> Running in e0c36494eb6b
Removing intermediate container e0c36494eb6b
---> 045d14958d3c
Step 3/3 : ENV https_proxy="proxy3.bj.petrochina:8080"
---> Running in bd51305a33de
Removing intermediate container bd51305a33de
---> a3130b788869
Successfully built a3130b788869
Successfully tagged weaveworks/scope:1.11.2
Successfully tagged weaveworks/scope:latest

docker  18.09 为我们开启了 docker的新流程 非线性构建

[[email protected] ~]# export DOCKER_BUILDKIT=1
[[email protected] ~]# docker build -t weaveworks/scope:1.11.2 -t weaveworks/scope:latest -f /tmp/Dockerfile /tmp/
[+] Building 7.1s (5/5) FINISHED
=> [internal] load .dockerignore 0.7s
=> => transferring context: 2B 0.0s
=> [internal] load build definition from Dockerfile 0.5s
=> => transferring dockerfile: 155B 0.0s
=> [internal] load metadata for docker.io/weaveworks/scope:1.11.2 4.8s
=> [1/1] FROM docker.io/weaveworks/scope:[email protected]:4cdc0bac4e83f61f1675cd8fbe0709ff0df473b81977650ed3a26772ff3c627a 0.6s
=> => resolve docker.io/weaveworks/scope:[email protected]:4cdc0bac4e83f61f1675cd8fbe0709ff0df473b81977650ed3a26772ff3c627a 0.0s
=> exporting to image 0.5s
=> => exporting layers 0.0s
=> => writing image sha256:99f9e42918a0338f08a377a1b933e331c57275326471803f591944ed414b67df 0.0s
=> => naming to docker.io/weaveworks/scope:1.11.2 0.0s
=> => naming to docker.io/weaveworks/scope:latest 0.0s

2.Parser directives

没太搞懂的概念,dockefile可以指定指令的定制器。官方指令器必须出现在dockerfile的首行,且不能换行。目前只支持syntax和escape(转义字符)

3.ENV

FROM busybox
ENV foo /bar
WORKDIR $foo   # WORKDIR /bar
ADD . $foo       # ADD . /bar
COPY \$foo /quux # COPY $foo /quux
ENV abc=hello
ENV abc=bye def=$abc
ENV ghi=$abc
will result in def having a value of hello, not bye. However, ghi will have a value of byebecause it is not part of the same instruction that set abc to bye.
ENV 中有同步定义的概念 变量赋值不分先后顺序,他们都从上一层镜像中取值,而上层镜像和本层是independence的

 4.FROM

语法格式
FROM <image>[:<tag>] [AS <name>]
FROM <image>[@<digest>] [AS <name>]

 5.RUN 和 CMD

 

以上是关于1.Dockerfile的主要内容,如果未能解决你的问题,请参考以下文章