# Docker image for Erlang VM with minimal memory/disk usage

**URL:** <https://erlangforums.com/t/docker-image-for-erlang-vm-with-minimal-memory-disk-usage/3610>\
**Category:** Questions / Help\
**Tags:** docker\
**Created:** [May 8, 2024, 11:36am UTC](https://erlangforums.com/t/docker-image-for-erlang-vm-with-minimal-memory-disk-usage/3610 "2024-05-08T11:36:04Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![zabrane](https://erlangforums.com/letter_avatar_proxy/v4/letter/z/7ba0ec/32.png) [@zabrane](https://erlangforums.com/u/zabrane)\
**Post date:** [May 8, 2024, 11:36am UTC](https://erlangforums.com/t/docker-image-for-erlang-vm-with-minimal-memory-disk-usage/3610/1 "2024-05-08T11:36:04Z")

</div>

Hi guys,

Is there any Docker image I can use to build the smallest Erlang VM in term of memory usage and disk space?

Currently, I’m shipping my containers based on Ubuntu 22.04 LTS. They are pretty large and consume +200MB of RAM during startup (not even under load).

My task in the next weeks is to deploy more containers, and I can’t afford wasting memory like that.

Disclaimer: newbie here. I know only basics Docker.

Many thanks

---

<div class="post-metadata">

**Author:** ![garazdawi](https://erlangforums.com/user_avatar/erlangforums.com/garazdawi/32/175_2.png) [@garazdawi](https://erlangforums.com/u/garazdawi)\
**Post date:** [May 8, 2024, 11:58am UTC](https://erlangforums.com/t/docker-image-for-erlang-vm-with-minimal-memory-disk-usage/3610/2 "2024-05-08T11:58:57Z")

</div>

If I just start an Erlang shell it uses about 37 MB of memory (as shown by `erlang:memory(total)`) and then another 13 MB in caches and pre-allocated memory (`docker stats` shows 50 MB of memory usage). To get that value down a bit I can start erl like this:

```erlang
docker run -it erlang:26 erl +S 1 +SDio 1 +P 1024 +Q 1024 -mode minimal

```

That brings the memory usage down to 11 MB with 9 MB in caches etc, but it puts some constraints on the system that you might not want.

To decrease the amount of diskspace used, you should go for a multistage docker build where you only copy over your release. Like what is done here: [GitHub - erlang/docker-erlang-example: HowTo Erlang in Docker](https://github.com/erlang/docker-erlang-example) and specifically in this [Dockerfile](https://github.com/erlang/docker-erlang-example/blob/master/Dockerfile.very_small_image).

---

<div class="post-metadata">

**Author:** ![zabrane](https://erlangforums.com/letter_avatar_proxy/v4/letter/z/7ba0ec/32.png) [@zabrane](https://erlangforums.com/u/zabrane)\
**Post date:** [May 8, 2024, 12:16pm UTC](https://erlangforums.com/t/docker-image-for-erlang-vm-with-minimal-memory-disk-usage/3610/3 "2024-05-08T12:16:01Z")

</div>

@garazdawi thanks a lot. Let me give it go

---

<div class="post-metadata">

**Author:** ![SirWerto](https://erlangforums.com/user_avatar/erlangforums.com/sirwerto/32/2936_2.png) [@SirWerto](https://erlangforums.com/u/SirWerto)\
**Post date:** [May 9, 2024, 9:14am UTC](https://erlangforums.com/t/docker-image-for-erlang-vm-with-minimal-memory-disk-usage/3610/4 "2024-05-09T09:14:02Z")

</div>

Jade Allen talks about it in this presentation [Code as infrastructure in the BEAM ecosystem - Jade Allen | Code BEAM Europe 2023](https://erlangforums.com/t/code-as-infrastructure-in-the-beam-ecosystem-jade-allen-code-beam-europe-2023/3574) 🙂

---

<div class="post-metadata">

**Author:** ![zabrane](https://erlangforums.com/letter_avatar_proxy/v4/letter/z/7ba0ec/32.png) [@zabrane](https://erlangforums.com/u/zabrane)\
**Post date:** [May 25, 2024, 11:21pm UTC](https://erlangforums.com/t/docker-image-for-erlang-vm-with-minimal-memory-disk-usage/3610/5 "2024-05-25T23:21:19Z")

</div>

@garazdawi Is this still the case, or has the problem already been solved?

> <https://github.com/erlang/otp/issues/7292#issuecomment-1569825141>
>
> \*\*Describe the bug\*\*
> Application down few seconds after the run release in AWS …eks
> 
> \*\*To Reproduce\*\*
> Unfortunately i have no idea how to reproduce.
> 
> \*\*Affected versions\*\*
> 26.0
> 
> \*\*Additional context\*\*
> AWS EKS
> erlang 26.0
> alpine 3.18.0
> 
> Application logs
> \`\`\`
> hend=0x00007f4f6f34c7f0
> stop=0x00007f4f6f34c670
> htop=0x00007f4f6f34c678
> heap=0x00007f4f6f349600
> beam/erl\_gc.c, line 735: \<0.3141.0\>: Overrun stack and heap
> \`\`\`

I came across this after reading Elixir question:  
[Using Alpine and musl instead of GNU libc affect performance?](https://elixirforum.com/t/using-alpine-and-musl-instead-of-gnu-libc-affect-performance/57670)

---

<div class="post-metadata">

**Author:** ![eproxus](https://erlangforums.com/user_avatar/erlangforums.com/eproxus/32/452_2.png) [@eproxus](https://erlangforums.com/u/eproxus)\
**Post date:** [May 26, 2024, 7:24am UTC](https://erlangforums.com/t/docker-image-for-erlang-vm-with-minimal-memory-disk-usage/3610/6 "2024-05-26T07:24:08Z")

</div>

@garazdawi `-mode minimal` seems undocumented. Is there any available information on what it does?

---

<div class="post-metadata">

**Author:** ![garazdawi](https://erlangforums.com/user_avatar/erlangforums.com/garazdawi/32/175_2.png) [@garazdawi](https://erlangforums.com/u/garazdawi)\
**Post date:** [May 26, 2024, 6:01pm UTC](https://erlangforums.com/t/docker-image-for-erlang-vm-with-minimal-memory-disk-usage/3610/7 "2024-05-26T18:01:47Z")

</div>

If you read the last comment of that issue you can see that it had nothing to do with using musl and yes it has been fixed.

---

<div class="post-metadata">

**Author:** ![garazdawi](https://erlangforums.com/user_avatar/erlangforums.com/garazdawi/32/175_2.png) [@garazdawi](https://erlangforums.com/u/garazdawi)\
**Post date:** [May 26, 2024, 6:04pm UTC](https://erlangforums.com/t/docker-image-for-erlang-vm-with-minimal-memory-disk-usage/3610/8 "2024-05-26T18:04:02Z")

</div>

Using `-mode minimal` disables some kernel services mostly related to distribution and timers. I thought it was documented by apparently not. It has minimal effect on memory usage, but it does shave a few KBs off as a couple of processes are not started.

---

<div class="post-metadata">

**Author:** ![zabrane](https://erlangforums.com/letter_avatar_proxy/v4/letter/z/7ba0ec/32.png) [@zabrane](https://erlangforums.com/u/zabrane)\
**Post date:** [September 16, 2024, 5:16pm UTC](https://erlangforums.com/t/docker-image-for-erlang-vm-with-minimal-memory-disk-usage/3610/9 "2024-09-16T17:16:14Z")

</div>

@garazdawi when trying to [compress the modules](https://github.com/erlang/docker-erlang-example/blob/master/Dockerfile.very_small_image#L156-L159) in the **lib** directory (except the NIF ones), my release doesn’t boot anymore. Got this error:

```erlang
({load_failed,[lists,logger_config,logger_filters,logger,kernel,heart,file_io_server,file_server,file,error_logger,erl_parse,erl_lint,erl_eval,code,proc_lib,code_server,application_master,application,error_handler,logger_simple_h,logger_server,gen_server,gen_event,gen,filename,ets,logger_backend,supervisor,application_controller]})

```

Any idea?  
Should I set a special flag when running the release containing `.ez` modules?

---

<div class="post-metadata">

**Author:** ![nhpip](https://erlangforums.com/letter_avatar_proxy/v4/letter/n/3bc359/32.png) [@nhpip](https://erlangforums.com/u/nhpip)\
**Post date:** [September 17, 2024, 2:08pm UTC](https://erlangforums.com/t/docker-image-for-erlang-vm-with-minimal-memory-disk-usage/3610/10 "2024-09-17T14:08:24Z")

</div>

We just did a similar exercise with an Elixir app. We used [Burrito](https://hexdocs.pm/burrito/readme.html) to create a single executable that contains `erts` and the beam files (Erlang/Elixir/the app). The Docker image size went from 200MB down to 20MB.

We did a 2 phase build.

1. Create a [standard Dockerfile](https://gist.github.com/nhpip/a6b8f29d371b837772c9c77ad3f76dc2) that creates a executable and an Elixir (`mix`) release.
2. A [minimized Dockerfile](https://gist.github.com/nhpip/908153065bc6fc9d0f6e7b6d0173262f) that just takes the executable and libraries required and produces a small image.

You could probably use a `rebar3` deployable tarball instead of `Burrito` and `mix` release.

---

<div class="post-metadata">

**Author:** ![zabrane](https://erlangforums.com/letter_avatar_proxy/v4/letter/z/7ba0ec/32.png) [@zabrane](https://erlangforums.com/u/zabrane)\
**Post date:** [September 17, 2024, 3:53pm UTC](https://erlangforums.com/t/docker-image-for-erlang-vm-with-minimal-memory-disk-usage/3610/11 "2024-09-17T15:53:14Z")

</div>

Thanks a lot @nhpip. We use `rebar3` of course for our Erlang releases and everything is working perfectly. Our images aren’t that small (~80MiB because we ship a code + many NIFs), but it’s OK.

My issue is with the `.ez` modules per @garazdawi suggestion.

---

<div class="post-metadata">

**Author:** ![zabrane](https://erlangforums.com/letter_avatar_proxy/v4/letter/z/7ba0ec/32.png) [@zabrane](https://erlangforums.com/u/zabrane)\
**Post date:** [September 24, 2024, 5:45am UTC](https://erlangforums.com/t/docker-image-for-erlang-vm-with-minimal-memory-disk-usage/3610/12 "2024-09-24T05:45:52Z")

</div>

@garazdawi could you please help with the `.ez` issue? Thanks
