Home > Enterprise >  Set Docker resource limits via gitlab CICD as variables
Set Docker resource limits via gitlab CICD as variables

Time:01-20

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

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

You can pass the resource limits as environment variables in the GitLab pipeline and then reference them in the docker-compose.yml file.

For example, you can define the environment variables in the pipeline:

resources:
  variables:
    CPU_LIMIT: 2
    MEMORY_LIMIT: 4GB
    CPU_RESERVATION: 1
    MEMORY_RESERVATION: 4GB

Then, in your docker-compose.yml file, you can reference the environment variables in the resources block:

services:
  myservice:
    image: myimage
    resources:
      limits:
        cpus: ${CPU_LIMIT}
        memory: ${MEMORY_LIMIT}
      reservations:
        cpus: ${CPU_RESERVATION}
        memory: ${MEMORY_RESERVATION}

This way, when the pipeline runs, it will substitute the environment variable values in the docker-compose.yml file, and the container will be built with the desired resource limits.

You can also add a step in your pipeline to check if the variable is set or not and if not, then set the default value.

It's important to note that the limits passed via environment variable need to be in their string format, and the values for memory limits need to be in the format of 4GB and not 4096m.

  • Related