如何以较低优先级运行 docker build?

如何以较低优先级运行 docker build?

我经常需要重新构建 Docker 镜像,而且我在运行容器的主机内部进行此操作。有时我发现这会给 CPU 带来相当大的压力,因此我认为我可以docker build在下运行nice -n19,但是当其他进程需要运行时,这似乎对 docker 的产量没有影响。

我宁愿不使用 Docker Hub 存储库,因为这是私人的东西,我现在正试图节省每一分钱。我还知道我可以将另一台机器设置为构建/存储库——例如,我可以使用办公室里的一台机器——但我不知道该怎么做。

那么,问题是:为什么nice -n19 docker build ...看起来没有什么帮助?

(如果能向我提供有关如何设置我自己的私有构建/回购机器的文档,我将获得加分)

答案1

为什么 nice -n19 docker build ... 似乎没有什么帮助?

命令docker是 docker 守护进程的客户端,它生成在容器中运行的进程。当您为命令赋予较低优先级时docker,docker 守护进程本身的优先级不受影响,因此您的构建和执行将以其默认优先级运行。这就像为您的 Web 浏览器赋予较低优先级并不意味着 Web 服务器将以较低优先级为您的请求提供服务。

如何设置我自己的私有构建/回购机器

对于基本的独立构建机器,您只需执行docker export, scp, docker import。但是对于更严肃的构建系统,您可能需要运行私有docker注册表. 一些更有用的文档:

如果您运行自己的私有注册表,则可以从办公室的本地工作站进行构建,然后使用docker push并将docker pulldocker 镜像上传到您的私有注册表并将其获取到所需的任何位置。

答案2

不要使用nice,而要考虑使用其他选项CPU 管理docker API 为您提供:

docker build --help | grep cpu
      --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)

例如,我最近更新了我的部分构建,以使用如下命令保留其中一台构建机器上的 6 个 CPU 核心中的 1 个:

docker build --cpuset-cpus 0-4 .

这些选项似乎都没有得到很好的支持,因此您可能需要进行实验以了解哪种方法最适合您。我发布了一些我运行的快速测试,但可以通过运行更长的测试并使用主机上的 CPU 来更好地模拟共享来改进这些测试。

–cpu 周期=“100000” –cpu 配额=“150000”

docker run --name 7z --rm -it --cpu-period="100000" --cpu-quota="150000" localhost/7z bash -c 'time dd if=/dev/zero bs=50K count=1K | 7zr -si a out.7z'

7-Zip (a) [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=C,Utf16=off,HugeFiles=on,64 bits,12 CPUs Intel(R) Core(TM) i7-5820K CPU @ 3.30GHz (306F2),ASM,AES-NI)

Creating archive: out.7z

Items to compress: 1

  0M1024+0 records in
1024+0 records out
52428800 bytes (52 MB, 50 MiB) copied, 0.0942925 s, 556 MB/s
                  
Files read from disk: 1
Archive size: 7815 bytes (8 KiB)
Everything is Ok

real    0m2.326s
user    0m3.078s
sys     0m0.359s

–cpu 共享 500

docker run --name 7z --rm -it --cpu-shares 500 localhost/7z bash -c 'time dd if=/dev/zero bs=50K count=1K | 7zr -si a out.7z'

7-Zip (a) [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=C,Utf16=off,HugeFiles=on,64 bits,12 CPUs Intel(R) Core(TM) i7-5820K CPU @ 3.30GHz (306F2),ASM,AES-NI)

Creating archive: out.7z

Items to compress: 1

  0M1024+0 records in
1024+0 records out
52428800 bytes (52 MB, 50 MiB) copied, 0.08448 s, 621 MB/s
                  
Files read from disk: 1
Archive size: 7815 bytes (8 KiB)
Everything is Ok

real    0m1.982s
user    0m2.961s
sys     0m0.343s

好的

docker run --name 7z --rm -it localhost/7z bash -c 'time dd if=/dev/zero bs=50K 
count=1K | nice 7zr -si a out.7z'

7-Zip (a) [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=C,Utf16=off,HugeFiles=on,64 bits,12 CPUs Intel(R) Core(TM) i7-5820K CPU @ 3.30GHz (306F2),ASM,AES-NI)

Creating archive: out.7z

Items to compress: 1

  0M1024+0 records in
1024+0 records out
52428800 bytes (52 MB, 50 MiB) copied, 0.0928115 s, 565 MB/s
                  
Files read from disk: 1
Archive size: 7815 bytes (8 KiB)
Everything is Ok

real    0m2.026s
user    0m3.066s
sys     0m0.375s

–cpuset-cpus 0-1

docker run --name 7z --rm -it --cpuset-cpus 0-1 localhost/7z bash -c 'time dd if=/dev/zero bs=50K count=1K | 7zr -si a out.7z'

7-Zip (a) [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=C,Utf16=off,HugeFiles=on,64 bits,12 CPUs Intel(R) Core(TM) i7-5820K CPU @ 3.30GHz (306F2),ASM,AES-NI)

Creating archive: out.7z

Items to compress: 1

  0M1024+0 records in
1024+0 records out
52428800 bytes (52 MB, 50 MiB) copied, 0.0868617 s, 604 MB/s
                  
Files read from disk: 1
Archive size: 7815 bytes (8 KiB)
Everything is Ok

real    0m1.908s
user    0m2.432s
sys     0m0.295s

无(控制)

docker run --name 7z --rm -it localhost/7z bash -c 'time dd if=/dev/zero bs=50K count=1K | 7zr -si a out.7z'                                                                                               1 ↵

7-Zip (a) [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=C,Utf16=off,HugeFiles=on,64 bits,12 CPUs Intel(R) Core(TM) i7-5820K CPU @ 3.30GHz (306F2),ASM,AES-NI)

Creating archive: out.7z

Items to compress: 1

  0M1024+0 records in
1024+0 records out
52428800 bytes (52 MB, 50 MiB) copied, 0.0938064 s, 559 MB/s
                  
Files read from disk: 1
Archive size: 7815 bytes (8 KiB)
Everything is Ok

real    0m2.036s
user    0m3.021s
sys.    0m0.389s

答案3

Lie 的回答给出了 Docker 不会nice进行构建的原因。我想在下面为您提供一个实用的解决方法。

让 Docker 构建更温和的一种方法是确定 Dockerfile 中资源密集型的部分,然后只nice在 Dockerfile 中执行这些部分。这还将使您的构建在远程构建基础设施(例如 Jenkins CI)上更温和。

就我而言,我构建了一个相当繁重的 Apache mod_perl 容器,其中包含大量 CPAN 模块。构建所有这些模块是成本高昂的部分,因为它使用 GCC 编译大量代码。无论如何,我能够nice通过将 Dockerfile 中的以下行从

RUN cpanm --quiet --installdeps --local-lib-contained /perlmod .

RUN nice -n19 cpanm --quiet --installdeps --local-lib-contained /perlmod . 

尽管这有点不稳定并且有点黑客行为,但我不确定我是否会nice在 Dockerfile 中发布软件,但你说这无论如何都是用于私人的东西,所以它可能是一个合适的解决方案。

你可以将其推广到 Dockerfile 中的任何其他昂贵操作,

其他示例:

Python

RUN nice -n19 pip install some_module

Node.js

RUN nice -n19 npm install .

昂贵的压缩步骤

RUN nice -n19 xz some_file.bin

相关内容