skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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
    

  2. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search