Bitbucket Pipeline keeps on showing not enough memory
Asked by Jc Briones:

Hi, can anyone help me how to configure my self.hosted linux docker. It has 64GBs of RAM and 12c/24t CPU and it keeps on telling me that the build is running out of memory (*Service 'docker' exceeded memory limit.*) when I ran the docker build. Here is a sample of my configuration:

\`\`\`yml  
definitions:  
services:  
docker:  
memory: 1024  
docker-build:  
memory: 5120  
type: docker  
steps:  
- step: \&docker-build  
name: Docker Build  
runs-on:  
- self.hosted  
- linux  
services:  
- docker-build  
caches:  
- docker  
size: 8x  
script:  
- IMAGE_NAME=$BITBUCKET_REPO_SLUG-$BITBUCKET_BRANCH  
- docker build . --file Dockerfile --tag ${IMAGE_NAME}  
- docker save ${IMAGE_NAME} --output "${IMAGE_NAME}.tar"  
artifacts:  
- "\*.tar"  
pipelines:  
pull-requests:  
'\*\*':  
- step: \*docker-build

\`\`\`


Answers:
author: Patrik S
body: Hello [@Jc Briones](/t5/user/viewprofilepage/user-id/5170113) ,

Thank you for reaching out to Atlassian Community!

Just to bring some background on how memory is allocated in the bitbucket pipelines, regular steps have 4096 MB of memory in total, and large build steps (which you can define using size: 2x) have 8192 MB in total. When using self-hosted runners, you can go up to 8x which means the step will be given 32GB of memory to work with. Also, service containers (like the docker service ) get 1024 MB memory by default but can be configured to use between 128 MB and the step maximum.

So when you define a step as **size: 8x,** this means it will be given 32GB of memory that can be distributed between the **services containers** and the **build container**. The build container is the container where your script actually runs, and it needs to have at least 1024MB of memory allocated to it. The service containers are the container spun up for any service defined in your step and they receive 1024MB as default.

With that in mind and analyzing the step you have shared, we can see that the step is configured as **size: 8x** , hence it will be provided with **32768MB** of memory, of which **just 5120MB is being reserved for the docker service** , leaving **27648 MB** for the build container that runs your script commands. Since you are running a docker build command, the most memory-expensive task is being executed on the **docker service container** , which has just **5120MB** of available memory, and looks like this memory is not sufficient for the image build to complete, and hence the error you are getting.

As your step is essentially just building a docker image, which implies most of the load will be in the docker service container, I would suggest allocating a greater portion of the memory to the docker service (remembering you need to leave at least 1024MB for the build container). You could try allocating 10GB (10240MB) - double what it has currently and check if the error persists. The YML file would look like below :

```
definitions:
  services:
    docker-build:
       memory: 10240
       type: docker
  steps:
    - step: &docker-build
       name: Docker Build
       runs-on:
        - self.hosted
        - linux
       services:
        - docker-build
       caches:
        - docker
       size: 8x
       script:
         - IMAGE_NAME=$BITBUCKET_REPO_SLUG-$BITBUCKET_BRANCH
         - docker build . --file Dockerfile --tag ${IMAGE_NAME}
         - docker save ${IMAGE_NAME} --output "${IMAGE_NAME}.tar"
```

For further details on how memory is allocated in Pipelines, you can also refer to our [Databases and service containers](https://support.atlassian.com/bitbucket-cloud/docs/databases-and-service-containers/) documentation.

Thank you, [@Jc Briones](/t5/user/viewprofilepage/user-id/5170113) !

Patrik S

comments: author: Jc Briones
body: Thank you Patrick! I actually did set it to 8gb and it did work. Didn't know that my build takes more than 5gb of ram.


Tags: bitbucket-cloud

cloud

server