设为首页 收藏本站
查看: 1069|回复: 0

[经验分享] docker build

[复制链接]

尚未签到

发表于 2018-1-12 16:06:20 | 显示全部楼层 |阅读模式
  命令:docker build
  

[iyunv@iZ943kh74qgZ ~]# docker build --help  

  
Usage:    docker build [OPTIONS] PATH
| URL | -  

  
Build an image from a Dockerfile
  

  
Options:
--add-host list              Add a custom host-to-IP mapping (host:ip)--build-arg list             Set build-time variables--cache-from stringSlice     Images to consider as cache sources--cgroup-parent string       Optional parent cgroup for the container--compress                   Compress the build context using gzip  --cpu-period int             Limit the CPU CFS (Completely Fair Scheduler) period
  --cpu-quota int              Limit the CPU CFS (Completely Fair Scheduler) quota
  -c, --cpu-shares int             CPU shares (relative weight)
  --cpuset-cpus string         CPUs in which to allow execution (0-3, 0,1)
  --cpuset-mems string         MEMs in which to allow execution (0-3, 0,1)
  --disable-content-trust      Skip image verification (default true)
  -f, --file string                Name of the Dockerfile (Default is 'PATH/Dockerfile')
  --force-rm                   Always remove intermediate containers
  --help                       Print usage
  --isolation string           Container isolation technology
  --label list                 Set metadata for an image
  -m, --memory bytes               Memory limit
  --memory-swap bytes          Swap limit equal to memory plus swap: '-1' to enable unlimited swap
  --network string             Set the networking mode for the RUN instructions during build (default "default")
  --no-cache                   Do not use cache when building the image
  --pull                       Always attempt to pull a newer version of the image

  -q, --quiet                      Suppress the build output and print image>  --rm                         Remove intermediate containers after a successful build (default true)
  --security-opt stringSlice   Security options

  --shm-size bytes            >  -t, --tag list                   Name and optionally a tag in the 'name:tag' format
  --target string              Set the target build stage to build.
  --ulimit ulimit              Ulimit options (default [])
  

  使用这个命令来搭配Dockerfile 创建一个image
  PATH or URL  在这2项中的文件被当作资源上下文. 创建image过程中,所有的文件都可能会被标记,比如说执行 ADD (link is external) 项. 当一个Dockerfile只有 URL STDIN (docker build - < Dockerfile), 那么就没有上下文了.
  如果 URL 中指定了一个git repo,那么这个git repo也会被使用。 这个git repo会被当作子目录 (git clone -recursive). A fresh git clone occurs in a temporary directory on your local host, and then this is sent to the Docker daemon as the context. 反正如果使用git你必须处理好git的凭证和假如需要的VPN设置.
  .dockerignore 在 PATH 项的根目录,这提供一种指定忽略的方式. 符合忽略规则的文件或目录将被忽略。
  从官网文档来看https://docs.docker.com/engine/reference/builder/#usage
  首先建议一个新的目录,这个文件下面是你需要创建这个镜像时所用到的文件,还有一个叫“Dockerfile”的文件,而这个文件就是我们构建镜像文档文。我们要写指令就是放在这个文件里。
  我们可以用-f 参数来指定其它地方的dockerfile
  

$ docker build -f /path/to/a/Dockerfile .  

  有一个比较搞笑的插曲,当我学习的时候,一直在纠结为什么docker build 不成功,一直报错。原来我错误的认为,这个Dockerfile是一个文件夹,里面放指令文件。还折腾了好几个小时,不断各种百度。
  我们可以使用-t来指定repository 和tag
  

$ docker build -t shykes/myapp .  

  其它 docker build 的用法
  直接用 Git repo 进行构建
  或许你已经注意到了,docker build 还支持从 URL 构建,比如可以直接从 Git repo 中构建:
  

$ docker build https://github.com/twang2218/gitlab-ce-zh.git#:8.14  
docker build https://github.com/twang2218/gitlab-ce-zh.git\#:8.14
  
Sending build context to Docker daemon 2.048 kB
  
Step 1 : FROM gitlab/gitlab-ce:8.14.0-ce.0
  
8.14.0-ce.0: Pulling from gitlab/gitlab-ce
  
aed15891ba52: Already exists
  
773ae8583d14: Already exists
  
...
  

  这行命令指定了构建所需的 Git repo,并且指定默认的 master 分支,构建目录为 /8.14/,然后 Docker 就会自己去 git clone 这个项目、切换到指定分支、并进入到指定目录后开始构建。
  用给定的 tar 压缩包构建
  

$ docker build http://server/context.tar.gz  

  如果所给出的 URL 不是个 Git repo,而是个 tar 压缩包,那么 Docker 引擎会下载这个包,并自动解压缩,以其作为上下文,开始构建。
  从标准输入中读取 Dockerfile 进行构建
  

docker build - < Dockerfile  

  

cat Dockerfile | docker build -  

  如果标准输入传入的是文本文件,则将其视为 Dockerfile,并开始构建。这种形式由于直接从标准输入中读取 Dockerfile 的内容,它没有上下文,因此不可以像其他方法那样可以将本地文件 COPY 进镜像之类的事情。
  从标准输入中读取上下文压缩包进行构建
  

$ docker build - < context.tar.gz  

  如果发现标准输入的文件格式是 gzip、bzip2 以及 xz 的话,将会使其为上下文压缩包,直接将其展开,将里面视为上下文,并开始构建。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-434318-1-1.html 上篇帖子: [GIT] Git 工作流程(Git flow, Github flow flow, Git lab flow) 下篇帖子: Git branch 分支与合并分支
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表