Container 'Build' exceeded memory limit.
Asked by Alfonso Ricardo Carranza:

I got the following issue:  

definitions:  
services:  
docker:  
memory: 1024  
docker-with-large-memory:  
memory: 7168  
type: docker  
------------  
steps:  
env: \&env  
...  
build: \&build  
- step:  
size: 2x  
services: \[docker-with-large-memory\]  

Always got "Container 'Build' exceeded memory limit."  
On logs:  
The build failed because the process exited too early. This probably means the system ran out of memory or someone called \`kill -9\` on the process.  
Any suggestion?


Answers:
author: Patrik S
body: Hello [@Alfonso Ricardo Carranza](/t5/user/viewprofilepage/user-id/4846757),

Welcome to Atlassian Community!

From the error message you have reported, it seems that you have exceeded the memory available to be used in the build container, and thus the build failed to complete.

Taking a deeper look at the build setup you have shared with us I've noticed that you are currently allocating 7168MB to the docker container service, leaving just 1024MB of memory to the 'Build' container where the scripts of your step are executed. 1024MB seems not to be enough to execute your current build, and this seems to be the reason why your build is failing.

Just to give you a background on how memory is allocated in the build container, regular steps have 4096 MB of memory in total, large build steps (which you can define using size: 2x) have 8192 MB in total. 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 (3072/7128 MB), which is your case.

Currently, on your yml file, you have set the step to use **size:2x**, and the docker service to use 7168MB, as per the following sections I've extracted from your yml file :

```
definitions:
   services:
     docker:
       memory: 1024
     docker-with-large-memory:
       memory: 7168
       type: docker
------------
steps:
env: &env
...
build: &build
- step:
   size: 2x
   services: [docker-with-large-memory]
```

Since this step is configured as **size:2x** , this means that it will be provided with 8192 MB of memory, of which **7168MB are being reserved for the docker service**, leaving just 1024MB for the Build container that runs your script commands. Since you're currently receiving an error, this memory seems to not be sufficient for your build to complete.

That being said, I would suggest you to decrease the memory being allocated to the docker service, so you leave more memory to the build container, by using the following definition on your YML file:  

```
definitions:
 services:
   docker-with-large-memory:
     memory: 5120
     type: docker
```

This will allocate 5120MB to the docker service (2GB less than it's currently allocating). You can start with 5120MB, and if it still fails with the same message, you can decrease it further.

After adjusting your YML file, you can try running the build again and check if the issue is fixed.

You can also refer to the following documentation for more details about docker memory limits on the Pipelines :

* <https://support.atlassian.com/bitbucket-cloud/docs/run-docker-commands-in-bitbucket-pipelines/#Docker-memory-limits>

Additionally, in case you need more than 8192 MB of memory, you have the option to use self-hosted runners, so you can run the build in your own infrastructure and allocate much more memory :

* <https://support.atlassian.com/bitbucket-cloud/docs/runners/>

Hope that helps. Let us know in case you run into any issues or have any question related to this topic.

Thank you, [@Alfonso Ricardo Carranza](/t5/user/viewprofilepage/user-id/4846757) .

Kind regards,

Patrik S

comments: author: Alfonso Ricardo Carranza
body: Thanks for your answer Patrick.

But I still have one doubt about it.

If I declare:

```
 services:
   docker-with-large-memory:
     memory: 5120
     type: docker
```

and call from "Build step" to use docker-with-large-memory:

```
build: &build
- step:
   size: 2x
   services: [docker-with-large-memory]
```

Build step,won't use 5120 has memory? and why do you say that it will only use 1024?  
Or maybe you refer to x2 (total size of memory 8192 between build and deploy )


author: Patrik S
body: Hey Alfonso,

Each step in the pipeline runs in separate container, which we refer as 'Build container'. Regular steps are given 4096 MB of available memory, but in case you use **size:2x**, the step will be given twice that number, meaning you would have 8192 MB in total.

The step memory can then be divided between the 'Build container' and the services that you use on that step. You can use multiple services in a given step, such as docker service or a database service. Those services run in a different container, but the memory allocated for those services is subtracted from the step available memory.

So for your example :

```
services:
   docker-with-large-memory:
     memory: 5120
     type: docker
```

```
build: &build
- step:
   name : my_step
   size: 2x
   services: [docker-with-large-memory]
```

Your step is set as size:2x, meaning that will have 8192 MB of memory available to be allocated between the build container and the services being used. In your case, you are allocating 5120MB of memory to the docker service, where your docker commands are executed, such as docker run or docker build. These 5120MB are reserved from the 8192 MB available for that step, meaning that just 3072MB (8192MB - 5120MB) will be left for the build container (where your script commands are executed).

Since the error you were facing was due to exceeded memory limit in 'Build container' , that's the reason why I suggested you reducing the memory allocated to the docker service, so it will make more memory available to the build container. In case you don't run docker commands in your build step, you can remove the service statement from that step, meaning that the total memory of that step will be available for the 'Build container'.

Let me know if this clarifies your questions.

Thank you, [@Alfonso Ricardo Carranza](/t5/user/viewprofilepage/user-id/4846757) .

Kind regards,

Patrik S


author: Alfonso Ricardo Carranza
body: Hello [@Patrik S](/t5/user/viewprofilepage/user-id/4603309) ,  
I got another question, what if I got the following context?

```
services:
   docker:
     memory: 1024
```

And this step (WHICH FAILS):

```
- step: &build-test-sonarcloud
   name : Build, test and analyze on SonarCloud
   size: 2x
```

Also got these ones:

```
env: &env
...
```

And

```
build: &build
- step:
   size: 2x
```

The branch executes in the following order:

```
-env(SUCCESS)
-build(SUCCESS)
-build-test-sonarcloud(FAIL)
```

***Container 'docker' exceeded memory limit.***

Any idea why ? i follow your previous concept definition


author: Amani Attia
body: have you resolved that issue ? plz


author: Patrik S
body: Hello [@Amani Attia](/t5/user/viewprofilepage/user-id/5100268)

I would like to ask if you could please create a new question for your issue, providing details on the errors you see.

We generally encourage users to create a new question for their issue instead of posting on someone else's question, because 1) the root cause and resolution may be different for each case 2) a question can become cluttered and difficult to follow if we try to troubleshoot multiple users' issues in it. Please feel free to let me know if you have any questions.

Thank you, [@Amani Attia](/t5/user/viewprofilepage/user-id/5100268) .

Patrik S


Tags: atl-gruland

bitbucket-cloud

cloud