Program Tip

Docker Compose 버전 3에서 메모리 및 CPU 제한을 지정하는 방법

programtip 2020. 11. 5. 18:54
반응형

Docker Compose 버전 3에서 메모리 및 CPU 제한을 지정하는 방법


버전 3에 지정된 서비스에 대해 CPU 및 메모리를 지정할 수 없습니다.

버전 2에서는 서비스 아래의 "mem_limit"및 "cpu_shares"매개 변수와 잘 작동합니다. 그러나 버전 3을 사용하는 동안 실패하고, 내가 떼 모드를 사용하지 않는 한 배포 섹션 아래에 두는 것은 가치가없는 것 같습니다.

누군가 도울 수 있습니까?

version: "3"
services:
  node:
    build:
     context: .
      dockerfile: ./docker-build/Dockerfile.node
    restart: always
    environment:
      - VIRTUAL_HOST=localhost
    volumes:
      - logs:/app/out/
    expose:
      - 8083
    command: ["npm","start"]
    cap_drop:
      - NET_ADMIN
      - SYS_ADMIN

deploy:
  resources:
    limits:
      cpus: '0.001'
      memory: 50M
    reservations:
      cpus: '0.0001'
      memory: 20M

더보기 : https://docs.docker.com/compose/compose-file/#resources

특정 경우 :

version: "3"
services:
  node:
    image: USER/Your-Pre-Built-Image
    environment:
      - VIRTUAL_HOST=localhost
    volumes:
      - logs:/app/out/
    command: ["npm","start"]
    cap_drop:
      - NET_ADMIN
      - SYS_ADMIN
    deploy:
      resources:
        limits:
          cpus: '0.001'
          memory: 50M
        reservations:
          cpus: '0.0001'
          memory: 20M

volumes:
  - logs

networks:
  default:
    driver: overlay

노트 :

  • 노출은 필요하지 않으며 스택 네트워크에서 기본적으로 노출됩니다.
  • 이미지는 미리 빌드해야합니다. v3 내에서 빌드 할 수 없습니다.
  • "다시 시작"도 더 이상 사용되지 않습니다. 실패시 작업으로 배포에서 다시 시작을 사용할 수 있습니다.
  • 독립형 하나의 노드 "swarm"을 사용할 수 있습니다. v3 대부분의 개선 사항 (전부는 아님)은 떼를위한 것입니다.

Also Note: Networks in Swarm mode do not bridge. If you would like to connect internally only, you have to attach to the network. You can 1) specify an external network within an other compose file, or have to create the network with --attachable parameter (docker network create -d overlay My-Network --attachable) Otherwise you have to publish the port like this:

ports:
  - 80:80

Docker Compose does not support the deploy key. It's only respected when you use your version 3 YAML file in a Docker Stack.

This message is printed when you add the deploy key to you docker-compose.yml file and then run docker-compose up -d

WARNING: Some services (database) use the 'deploy' key, which will be ignored. Compose does not support 'deploy' configuration - use docker stack deploy to deploy to a swarm.

The documentation (https://docs.docker.com/compose/compose-file/#deploy) says:

Specify configuration related to the deployment and running of services. This only takes effect when deploying to a swarm with docker stack deploy, and is ignored by docker-compose up and docker-compose run.


I know the topic is a bit old and seems stale, but anyway I was able to use these options:

    deploy:
      resources:
        limits:
          cpus: '0.001'
          memory: 50M

when using 3.7 version of docker-compose

What helped in my case, was using this command:

docker-compose --compatibility up

--compatibility flag stands for (taken from the documentation):

If set, Compose will attempt to convert deploy keys in v3 files to their non-Swarm equivalent

Think it's great, that I don't have to revert my docker-compose file back to v2.

참고URL : https://stackoverflow.com/questions/42345235/how-to-specify-memory-cpu-limit-in-docker-compose-version-3

반응형