<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/small-and-secure-docker-images-for-rust-scratch-vs-alpine-vs-debian-31lvdlshh" -->

---
title: Small and secure Docker images for Rust: scratch vs...
description: A practical comparison of three Docker base image strategies for Rust binaries: scratch (3.44 MB), alpine (11.4 MB), and debian:13-slim (77.5 MB). Walks...
canonical: https://daily.dev/posts/small-and-secure-docker-images-for-rust-scratch-vs-alpine-vs-debian-31lvdlshh
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Small and secure Docker images for Rust: scratch vs alpine vs debian | daily.dev
og:description: A practical comparison of three Docker base image strategies for Rust binaries: scratch (3.44 MB), alpine (11.4 MB), and debian:13-slim (77.5 MB). Walks...
og:url: https://daily.dev/posts/small-and-secure-docker-images-for-rust-scratch-vs-alpine-vs-debian-31lvdlshh
og:image: https://api.daily.dev/og/posts/31lVdLsHh.png
og:image:alt: Small and secure Docker images for Rust: scratch vs alpine vs debian
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Small and secure Docker images for Rust: scratch vs alpine vs debian

**[Sylvain Kerkour](https://daily.dev/sources/kerkour)** · 7 min read · 0 upvotes · 0 comments

## Summary

A practical comparison of three Docker base image strategies for Rust binaries: scratch (3.44 MB), alpine (11.4 MB), and debian:13-slim (77.5 MB). Walks through building a small CLI tool that calls an HTTPS API, using rustls instead of openssl to enable static linking, and swapping in the mimalloc allocator to avoid MUSL's slower malloc. Provides full Dockerfiles for each approach, including certificate, timezone, and unprivileged-user setup. Concludes that scratch is preferred for security and size, with alpine as a fallback when external tools like chromium or ffmpeg are needed.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://kerkour.com/rust-docker>

## Questions this post answers

### How small can a Docker image be for a Rust binary using FROM scratch versus alpine or debian-slim?

A Rust CLI tool built FROM scratch comes out to about 3.44 MB, the same binary on an alpine base image is about 11.4 MB, and on debian:13-slim it grows to about 77.5 MB. The scratch image requires manually copying certificates, passwd/group files, and timezone data since the base has nothing pre-installed.

_Compare base image tradeoffs like this on daily.dev before picking a Docker strategy for Rust services._

### Why is MUSL's malloc bad for Rust performance and how do I fix it?

MUSL libc's default memory allocator is not optimized for speed and causes high memory fragmentation, especially under high throughput, which matters because static linking with musl targets is required for scratch and alpine Docker images. The fix is switching Rust's global allocator to mimalloc with two lines of code: declaring a static MiMalloc instance as the #[global_allocator].

_Developers optimizing Rust containers for performance can track allocator tricks like this on daily.dev._

### Should I use openssl or rustls for statically linking a Rust binary in a minimal Docker image?

Rustls is preferable to openssl when statically linking Rust binaries for minimal Docker images, because statically linking openssl is difficult while rustls works cleanly with musl targets. This choice enables building HTTPS-capable command-line tools that run in scratch-based images without dynamic library dependencies.

_daily.dev helps developers weighing TLS library choices for lean Rust deployments stay informed._

## Similar posts on daily.dev

- [Everything you need to know to secure your Rust projects against the wave of cyberattacks](https://daily.dev/posts/everything-you-need-to-know-to-secure-your-rust-projects-against-the-wave-of-cyberattacks-1h8ntzkrh) · Sylvain Kerkour · 0 upvotes · 0 comments
- [Building smaller Docker images faster](https://daily.dev/posts/building-smaller-docker-images-faster-pkye16mw7) · Lobsters · 3 upvotes · 0 comments
- [Why To Use A Multi-Stage Dockerfile](https://daily.dev/posts/why-to-use-a-multi-stage-dockerfile-ripe33vzt) · RUBYLAND · 0 upvotes · 0 comments

---

Tags: [#alpine-linux](https://daily.dev/tags/alpine-linux), [#docker](https://daily.dev/tags/docker), [#rust](https://daily.dev/tags/rust)

[View this post on daily.dev](https://daily.dev/posts/small-and-secure-docker-images-for-rust-scratch-vs-alpine-vs-debian-31lvdlshh)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Small and secure Docker images for Rust: scratch vs alpine vs debian","url":"https://daily.dev/posts/small-and-secure-docker-images-for-rust-scratch-vs-alpine-vs-debian-31lvdlshh","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/small-and-secure-docker-images-for-rust-scratch-vs-alpine-vs-debian-31lvdlshh"},"datePublished":"2026-09-23T17:53:44.082Z","dateModified":"2026-11-12T02:13:58.427Z","description":"A practical comparison of three Docker base image strategies for Rust binaries: scratch (3.44 MB), alpine (11.4 MB), and debian:13-slim (77.5 MB). Walks...","image":"https://media.daily.dev/image/upload/s--qPvKM23u--/f_auto/v1722860399/public/Placeholder%2009","thumbnailUrl":"https://media.daily.dev/image/upload/s--qPvKM23u--/f_auto/v1722860399/public/Placeholder%2009","isAccessibleForFree":true,"articleSection":"Sylvain Kerkour","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Sylvain Kerkour","logo":"https://media.daily.dev/image/upload/s--3ngWt4Yp--/f_auto,q_auto/v1786438308/logos/kerkour?_a=BAMAMicg0","url":"https://daily.dev/sources/kerkour"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/small-and-secure-docker-images-for-rust-scratch-vs-alpine-vs-debian-31lvdlshh","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"alpine-linux,docker,rust","timeRequired":"PT7M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Sylvain Kerkour","item":"https://daily.dev/sources/kerkour"},{"@type":"ListItem","position":3,"name":"Small and secure Docker images for Rust: scratch vs alpine vs debian"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/small-and-secure-docker-images-for-rust-scratch-vs-alpine-vs-debian-31lvdlshh#faq","mainEntity":[{"@type":"Question","name":"How small can a Docker image be for a Rust binary using FROM scratch versus alpine or debian-slim?","acceptedAnswer":{"@type":"Answer","text":"A Rust CLI tool built FROM scratch comes out to about 3.44 MB, the same binary on an alpine base image is about 11.4 MB, and on debian:13-slim it grows to about 77.5 MB. The scratch image requires manually copying certificates, passwd/group files, and timezone data since the base has nothing pre-installed. Compare base image tradeoffs like this on daily.dev before picking a Docker strategy for Rust services."}},{"@type":"Question","name":"Why is MUSL's malloc bad for Rust performance and how do I fix it?","acceptedAnswer":{"@type":"Answer","text":"MUSL libc's default memory allocator is not optimized for speed and causes high memory fragmentation, especially under high throughput, which matters because static linking with musl targets is required for scratch and alpine Docker images. The fix is switching Rust's global allocator to mimalloc with two lines of code: declaring a static MiMalloc instance as the #[global_allocator]. Developers optimizing Rust containers for performance can track allocator tricks like this on daily.dev."}},{"@type":"Question","name":"Should I use openssl or rustls for statically linking a Rust binary in a minimal Docker image?","acceptedAnswer":{"@type":"Answer","text":"Rustls is preferable to openssl when statically linking Rust binaries for minimal Docker images, because statically linking openssl is difficult while rustls works cleanly with musl targets. This choice enables building HTTPS-capable command-line tools that run in scratch-based images without dynamic library dependencies. daily.dev helps developers weighing TLS library choices for lean Rust deployments stay informed."}}]}
```

