gpt4 book ai didi

puppet - 如何将项目添加到 .dockerignore?

转载 作者:IT老高 更新时间:2023-10-28 12:37:13 26 4
gpt4 key购买 nike

我找不到很多 .dockerignore 文件应该是什么样子的示例。

使用 puppet 在 docker 容器上安装一些包会导致图像从 600MB 爆炸到 3GB。我正在尝试使用 .dockerignore 文件将大小保持在最小值

$ 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

$ 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 将 puppet 模块克隆到 /modules 这会破坏构建缓存

我尝试了以下 .dockerignore 文件,但没有成功。

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

这是 .dockerignore 文件的正确语法吗?
有没有其他方法可以防止这些容器变得如此之大?

其他信息:

http://kartar.net/2013/12/building-puppet-apps-inside-docker/
http://danielmartins.ninja/posts/a-week-of-docker.html

最佳答案

.dockerignore 文件类似于 .gitignore 语法。以下是一些示例规则:

# Ignore a file or directory in the context root named "modules"
modules

# Ignore any files or directories within the subdirectory named "modules"
# in the context root
modules/*

# Ignore any files or directories in the context root beginning with "modules"
modules*

# Ignore any files or directories one level down from the context root named
# "modules"
*/modules

# Ignore any files or directories at any level, including the context root,
# named modules
**/modules

# Ignore every file in the entire build context (see next rule for how this
# could be used)
*

# Re-include the file or directory named "src" that may have been previously
# excluded. Note that you cannot re-include files in subdirectories that have
# been previously excluded at a higher level
!src

请注意,“构建上下文”是您在构建命令末尾传递的目录,通常是一个 . 来指示当前目录。此目录是从 docker 客户端打包的,不包括您使用 .dockerignore 忽略的任何文件,并发送到 docker 守护程序以执行构建。即使守护程序与您的客户端位于同一主机上,构建也只能在此上下文中运行,而不是直接从文件夹中运行。

构建只有一个 .dockerignore,它必须位于构建上下文的根目录中。如果它位于您的主目录中(假设您从子目录构建),它将不起作用,并且它不会从构建上下文的子目录中起作用。

要测试当前构建上下文中的内容并验证 .dockerignore 文件的行为是否正确,您可以复制/粘贴以下内容(假设您没有名为 test 的图像-context,如果你这样做,它将被覆盖然后删除):

# create an image that includes the entire build context
docker build -t test-context -f - . <<EOF
FROM busybox
COPY . /context
WORKDIR /context
CMD find .
EOF

# run the image which executes the find command
docker container run --rm test-context

# cleanup the built image
docker image rm test-context

关于puppet - 如何将项目添加到 .dockerignore?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25490911/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com