如何将项目添加到.dockerignore? [英] How do you add items to .dockerignore?

查看:268
本文介绍了如何将项目添加到.dockerignore?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到.dockerignore文件的外观的许多示例。

I'm not able to find many examples of what a .dockerignore file should look like.

使用木偶在Docker容器上安装一些软件包会导致映像从600MB爆炸到3GB。我正在尝试使用 .dockerignore 文件将大小保持为最小

Using puppet to install a few packages on a docker container causes the image to explode from 600MB to 3GB. I'm trying to use a .dockerignore file to keep the size to a minumum

$ cat Dockerfile  
FROM centos:centos6

#Work around selinux problem on cent images
RUN yum install -y --enablerepo=centosplus libselinux-devel

RUN yum install -y wget git tar openssh-server; yum -y clean all

Add Puppetfile / 
RUN librarian-puppet install
RUN puppet apply --modulepath=/modules -e "class { 'buildslave': jenkins_slave => true,}"
RUN librarian-puppet clean

如果我运行 docker images --tree 我可以看到映像立即增长了几GB

If I run docker images --tree I can see that the image instantlly grows by several GB

$ docker images --tree
                ├─e289570b5555 Virtual Size: 387.7 MB
                │ └─a7646acf90d0 Virtual Size: 442.5 MB
                │   └─d7bc6e1fbe43 Virtual Size: 442.5 MB
                │     └─772e6b204e3b Virtual Size: 627.5 MB
                │       └─599a7b5226f4 Virtual Size: 627.5 MB
                │         └─9fbffccda8bd Virtual Size: 2.943 GB
                │           └─ee46af013f6b Virtual Size: 2.943 GB
                │             └─3e4fe065fd07 Virtual Size: 2.943 GB
                │               └─de9ec3eba39e Virtual Size: 2.943 GB
                │                 └─31cba2716a12 Virtual Size: 2.943 GB
                │                   └─52cbc742d3c4 Virtual Size: 2.943 GB
                │                     └─9a857380258c Virtual Size: 2.943 GB
                │                       └─c6d87a343807 Virtual Size: 2.964 GB
                │                         └─f664124e0080 Virtual Size: 2.964 GB
                │                           └─e6cc212038b9 Virtual Size: 2.964 GB Tags: foo/jenkins-centos6-buildslave:latest

我相信图像变大的原因是因为 librarian-puppet 将木偶模块克隆为 / modules 会破坏构建缓存

I believe the reason that the image grows so large, is because librarian-puppet clones a puppet module to /modules which breaks the build cache

我尝试了以下 .dockerignore 没有运气的文件。

I've tried the following .dockerignore files with no luck.

$ cat .dockerignore
/modules
/modules/
/modules/*

这是的正确语法吗.dockerig否文件?

是否有其他方法可以防止这些容器变大?

Is this the correct syntax for a .dockerignore file?
Are there any other ways to prevent these containers from growing so large?

其他信息:

http://kartar.net/2013/12/building-puppet-apps-inside-docker/

http://danielmartins.ninja/posts/a- week-of-docker.html

推荐答案

.dockerignore 是为了防止在执行 docker build 时将文件添加到发送到docker守护进程的初始构建上下文中,它不会创建用于排除文件的全局规则

.dockerignore is to prevent files from being added to the initial build context that is sent to the docker daemon when you do docker build, it doesn't create a global rule for excluding files from being created in all images generated by a Dockerfile.

需要注意的是,每个 RUN 语句都会生成一个新映像,该映像的父映像是其上方的Dockerfile语句生成的映像。尝试将 RUN 语句折叠为一个语句以减小图像大小:

It's important to note that each RUN statement will generate a new image, with the parent of that image being the image generated by the Dockerfile statement above it. Try collapsing your RUN statements into a single one to reduce image size:

RUN librarian-puppet install &&\
 puppet apply --modulepath=/modules -e "class { 'buildslave': jenkins_slave => true,}" &&\
 librarian-puppet clean

这篇关于如何将项目添加到.dockerignore?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆