4

I'm trying to set some resource limits into a docker container. I'm able to add the below values to a docker-compose.yml file for docker resource limits;

      resources:
        limits:
          cpus: '2'
          memory: 4GB
        reservations:
          cpus: '1'
          memory: 4GB

How would I pass them in via gitlab pipeline for the container being built but set them as variables?

I was able to override the java heap size by adding;

java_xmx=${JAVA_XMX_OVERRIDE}

and the value

JAVA_XMX_OVERRIDE: "-Xmx2048m"

How would I do the same with resource limits?

Thanks

CPdev
  • 375
  • 2
  • 5
  • 20

2 Answers2

3

You can use variables in docker compose which you can propagate with the starting command.

compose.yaml:

version: '3.9'

services:
  httpd:
    container_name: httpd-test
    image: httpd
    deploy:
      resources:
        limits:
          memory: ${MEMORY}

Start container:

$ MEMORY=60M docker-compose up -d
$ docker stats
CONTAINER ID   NAME              CPU %     MEM USAGE / LIMIT     MEM %     NET I/O           BLOCK I/O     PIDS
ace0d6d638e1   httpd-test        0.00%     26.86MiB / 60MiB      44.77%    4.3kB / 0B        0B / 0B       82

You should be able to define an environment variable in your gitlab pipeline:

variables:
  MEMORY: 60M
Slava Kuravsky
  • 2,702
  • 10
  • 16
  • Can you pass them in without modifying the docker-compose file? I.e set them as extra vars when the container is being deployed? – CPdev Jan 16 '23 at 13:55
  • 1
    You can change limitations of a running container via `docker update -m 50M container_name` – Slava Kuravsky Jan 16 '23 at 14:24
  • But then when the container is deployed via the pipeline in the future, those changes will be over-written? – CPdev Jan 18 '23 at 08:53
  • If you can't modify your docker compose file, then the only way I see is to use `docker update` command in the pipeline after `docker-compose up` – Slava Kuravsky Jan 18 '23 at 11:49
1

I've ended up adding a docker-compose file template in the pipeline, in the template I modified the resource limits with ansible

- name: Compose Docker | Get text block for service   set_fact:
      service_content: "{{ lookup('template', 'templates/docker-compose-service.yml.j2') }}"   tags:
    - compose_docker
CPdev
  • 375
  • 2
  • 5
  • 20