来自 docker-compose 的 docker 容器 运行 的内存资源

Memory resources for a docker container running from docker-compose

我 运行 Sonarqube 在 Docker 撰写,我的文件如下所示:

version: "3"


services:
  sonarqube:
    image: sonarqube
    ports:
     - "9000:9000"
     - "5432:5432"
    links:
      - db:db
    environment:
     - SONARQUBE_JDBC_URL=jdbc:postgresql://db:5432/sonar
     - SONARQUBE_JDBC_USERNAME=postgres
     - SONARQUBE_JDBC_PASSWORD=sonar
    volumes:
     - ..../Work/tools/_SonarQube_home/conf:/opt/sonarqube/conf
#     - sonarqube_data:/opt/sonarqube_new/data
     - ...../Work/tools/_SonarQube_home/data:/opt/sonarqube/data
     - ....../Work/tools/_SonarQube_home/extensions:/opt/sonarqube/extensions
     - ..../Work/tools/_SonarQube_home/bundled-plugins:/opt/sonarqube/lib/bundled-plugins

  db:
    image: postgres
    environment:
     - POSTGRES_USER=postgres
     - POSTGRES_PASSWORD=sonar
     - POSTGRES_DB=sonar
    volumes:
     - .../Work/tools/_PostgreSQL_data:/var/lib/postgresql
     # This needs explicit mapping due to https://github.com/docker-library/postgres/blob/4e48e3228a30763913ece952c611e5e9b95c8759/Dockerfile.template#L52
     - ..../Work/tools/_PostgreSQL_data/data:/var/lib/postgresql/data

一切正常,太棒了。有一刻我看到 Sonarqube 实例开始动作缓慢,因此我检查了 docker stats。它看起来像这样:

| CPU   | Mem Usage/ Limit   |
|-------| --------------------
| 5.39% | 1.6GiB / 1.952GiB  |

如何为服务器定义更多 RAM 资源,比如 4 GB?以前它是 mem_limit 但现在在版本 3 上它不存在。 什么是好的解决方案?

谢谢!

如果您要部署到 Swarm,那么您可以在 Compose 文件中使用 resources 关键字。 (它在文件参考 https://docs.docker.com/compose/compose-file/ 的资源下进行了描述)

所以你可以做这样的事情是 Swarm:

version: "3.7"
services:
  redis:
    image: redis:alpine
    deploy:
      resources:
        limits:
          cpus: '0.50'
          memory: 50M
        reservations:
          cpus: '0.25'
          memory: 20M

如果您使用的是 Compose,则可以选择返回到 Compose 文件版本 2.0,如 Docker 的 Compose 文件参考中所述。

Looking for options to set resources on non swarm mode containers?

The options described here are specific to the deploy key and swarm mode. If you want to set resource constraints on non swarm deployments, use Compose file format version 2 CPU, memory, and other resource options. If you have further questions, refer to the discussion on the GitHub issue docker/compose/4513.

我不熟悉 Sonarqube 内存问题,但您可能想看看这个 https://docs.sonarqube.org/display/SONARqube71/Java+Process+Memory

在 Compose 文件版本 3 中,资源限制 moved to under a deploy: {resources: ...} 密钥,但也仅记录在 Swarm 模式下工作。因此,要实际设置它们,您需要切换到最兼容的版本 2 Compose 文件。

version: '2'
services:
  sonarqube:
    mem_limit: 4g

The default 应该是容器能够使用无限量的内存。如果您 运行 在 Docker 位于 Linux VM 内的环境中(任何基于 Docker 工具箱或 Docker Machine, Docker for Mac) 它受限于 VM 的内存大小。