简体   繁体   English

如何限制 Docker 构建命令的 CPU 使用率?

[英]How to limit CPU usage of Docker build command?

My Dockerfile executes RUN npm run build command.我的 Dockerfile 执行RUN npm run build命令。 When I execute docker build command on t2.micro EC2 instance, instance freezes and does not respond (I cannot even connect with ssh).当我在 t2.micro EC2 实例上执行docker build命令时,实例冻结并且没有响应(我什至无法连接 ssh)。 When I monitor the CPU usage, I can see that CPU usage reached to maximum level.当我监控 CPU 使用率时,我可以看到 CPU 使用率达到了最高水平。 Therefore, I have tried to limit memory usage with --max_semi_space_size=1 and --max_old_space_size=198 arguments but it did not work.因此,我试图通过--max_semi_space_size=1--max_old_space_size=198 arguments 来限制 memory 的使用,但它没有用。

If I can set the CPU usage limit for the docker build command I think it might work.如果我可以为 docker 构建命令设置 CPU 使用限制,我认为它可能会起作用。 Can someone help me?有人能帮我吗?

I'm aware that if I increase vCPU amount of the EC2 instance then I can build my application without any trouble but since it is a demo project I'm trying to deploy it on free tier.我知道如果我增加 EC2 实例的 vCPU 数量,那么我可以毫无问题地构建我的应用程序,但由于它是一个演示项目,我正在尝试将它部署在免费层上。

Dockerfile: Dockerfile:

FROM node:16-alpine
COPY . .
# Install dependencies
RUN node \
--max_semi_space_size=1 \
--max_old_space_size=198 \
$(which npm) ci
# Build the app
RUN node \
--max_semi_space_size=1 \
--max_old_space_size=198 \
$(which npm) run build
EXPOSE 3000
# Start the app
CMD [ "npx", "serve", "build" ]

in your system, run sudo docker info .在您的系统中,运行sudo docker info if you get WARNING: no swap limit support, you need to manually edit grub configuration first by running sudo nano /etc/default/grub and adding the following line: GRUB_CMDLINE_LINUX="cdgroup_enable=memory swapaccount=1" and saving.如果您收到警告:不支持交换限制,您需要首先通过运行sudo nano /etc/default/grub并添加以下行来手动编辑 grub 配置: GRUB_CMDLINE_LINUX="cdgroup_enable=memory swapaccount=1"并保存。

limiting cpu can then be done.然后可以限制cpu。 for commands on limiting memory and cpu you can check this article.有关限制 memory 和 cpu 的命令,您可以查看这篇文章。 limit cpu and memory usage in container限制 cpu 和 memory 在容器中的使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM