version: "3.8"
services:
  GPU:
    image: registry.51nca.com:8950/python:3.8mysql
    volumes: 
      - "datavol:/data:rw"
      - type: volume
        source: mydata
        target: /data
        volume:
          nocopy: true
      - type: bind
        source: ./static
        target: /opt/app/static
    deploy:
        mode: global
        #replicas: 1
        #endpoint_mode: dnsrr  #vip为该服务分配了一个虚拟IP/dnsrr:DNS轮询
        #labels:
        placement: #约束,部署在
          constraints:
            - node.role == manager|worker
            #- node.hostname == $
          #constraints:
            #- node.role == manager |worker
            #constraints: [node.role == worker ] #manager
            #constraints: [node.hostname == node1 ]
            #constraints: [node.labels.isgpu == true]
            #constraints: [node.labels.mysql == true ]
        resources: #资源限制
          limits: #最大使用
            cpus: "0.50"  #0.5个cpu
            memory: "512M" #内存
          reservations: #预留多少cpu
            cpus: "0.25"
            memory: "128M"
            devices: #GPU
              - driver: nvidia
                count: 1  #限制数量
                device_ids: ["0", "3"] #指定设备id 和count二选一
                capabilities: [gpu, utility] #必须字段  ["gpu"] ["tpu"]
        restart_policy: #重启策略
            condition: on-failure  #可选 none,on-failure在容器非正常退出时(退出状态非0),才会重启容器 或者 any(默认值:any)
            delay: 5s  #设置多久之后重启
            max_attempts: 3  #尝试重新启动容器的次数
            window: 120s  #设置容器重启超时时间
    volumes: 
      - "/nas:/nas:rw"
    environment:
      - TZ=Asia/Shanghai
    working_dir: /worker
    command: ['python3','main.py']
    #command: /bin/bash -c "while true; do sleep 1; done"
    networks:
    - net1116
networks:
  net1116:
    name: net212
    external: true
volumes:
  datavol:
    external: true
    name: nfsredis
volumes:
  datavol:
    driver_opts:
      type: "nfs"
      o: "addr=10.3.60.40,nolock,soft,rw"
      device: ":/NFSDir/db/redis7"

limits定义了对应容器最大可以消耗的资源上限


limits:    
    cpu: "100m"
    memory: "1000Mi"

#cpu 等于1一般等同于1CPU 核心,1个VCPU或者一个超线程
#100m 则叫做100毫核,100m就表示0.1个核,所以这里也可以用0.1代替

memory 等于500Mi,(备注:
1Mi=1024*1024;
1M=1000*1000;

查看命令

docker stats libre #查看容器的cpu 内存使用情况
docker inspect libre | grep Cpu #查看Cpu的限制
docker inspect libre | grep Cpu #查看内存限制