# Scratch linux container possible?

**URL:** <https://forums.swift.org/t/scratch-linux-container-possible/84722>\
**Category:** Embedded\
**Tags:** linux, static, static-sdk, embedded-linux\
**Created:** [February 13, 2026, 3:10am UTC](https://forums.swift.org/t/scratch-linux-container-possible/84722 "2026-02-13T03:10:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![caco](https://avatars.discourse-cdn.com/v4/letter/c/74df32/32.png) [@caco](https://forums.swift.org/u/caco)\
**Post date:** [February 13, 2026, 3:10am UTC](https://forums.swift.org/t/scratch-linux-container-possible/84722/1 "2026-02-13T03:10:40Z")

</div>

Would it be possible to produce a statically linked linux binary using the embedded subsystem? I’ve gone through the documentation and got it to compile for macOS, but for linux I’m a little out of my depth. My idea is to invoke POSIX system calls from Swift and package the static binary into a bare (from-scratch) container (x86\_64). A simple hello-world would be a great starter. Greetings!

---

<div class="post-metadata">

**Author:** ![0xTim](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/0xtim/32/27968_2.png) [@0xTim](https://forums.swift.org/u/0xTim)\
**Post date:** [February 13, 2026, 11:47am UTC](https://forums.swift.org/t/scratch-linux-container-possible/84722/2 "2026-02-13T11:47:02Z")

</div>

It’s possible but requires lots of moving parts. I essentially had to do the same for the chiselled containers, which are built on top of scratch. You need to pull in all the dependencies that the Swift runtime requires and compile it as a static executable

> [@Swift Chiselled Containers](https://forums.swift.org/t/swift-chiselled-containers/65993):
>
> Hi all, I've been investigating [Ubuntu Chiselled Containers](https://ubuntu.com/blog/chiselled-containers-perfect-gift-cloud-applications) and seeing if they can be used with Swift applications. Chiselled containers are based off the Distroless concept and provide very stripped down environments for your applications (no shell, no package manager etc) which provides smaller container images and a smaller attack surface. It turns out, they're very easy to build. I've put a demo repo [on GitHub](https://github.com/0xTim/swift-chiselled/tree/main) but the container sizes built are: Chiselled: 13.2MB Non-chiselled: 83.7MB A…

---

<div class="post-metadata">

**Author:** ![caco](https://avatars.discourse-cdn.com/v4/letter/c/74df32/32.png) [@caco](https://forums.swift.org/u/caco)\
**Post date:** [February 13, 2026, 7:15pm UTC](https://forums.swift.org/t/scratch-linux-container-possible/84722/3 "2026-02-13T19:15:36Z")

</div>

Thanks, but that’s still too bloated for what I have in mind — my target being an equivalent to what zig does with cross compiling to linux with musl, producing static binaries of ~100KB for a minimal http server.  
The furthest I got with Swift Embedded is a 'hello-world' binary that is static in Swift’s sense but still linked to the system glibc, which makes sense if the target triple includes gnu. Example `ldd main` output:

```swift
’libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fffff5dc000)
/lib64/ld-linux-x86-64.so.2 (0x00007ffffffcb000)'

```

ls output:

```swift
’18 KB Thu Feb 12 16:55:57 2026  main
1.8 MB Fri Feb 13 15:19:57 2026  libc.so.6
210 KB Fri Feb 13 15:20:03 2026  ld-linux-x86-64.so.2’

```

Deploying it into a bare container (with the correct library paths) yields ~0.96 MB compressed (container image inspect shows` "size": 1009266`)  
I'm sure Swift Embedded can be used with a patched musl, but how?

---

<div class="post-metadata">

**Author:** ![Cyberbeni](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cyberbeni/32/46241_2.png) [@Cyberbeni](https://forums.swift.org/u/Cyberbeni)\
**Post date:** [February 13, 2026, 9:58pm UTC](https://forums.swift.org/t/scratch-linux-container-possible/84722/4 "2026-02-13T21:58:08Z")

</div>

I think the problem is that you are targeting Linux. You need to target none and then implement the syscalls you want to use manually, like this guy: [https://www.youtube.com/watch?v=IbibjkI1kIs](https://www.youtube.com/watch?v=IbibjkI1kIs)

A led blinking embedded project would probably be a good second half for the starting point: [GitHub - swiftlang/swift-embedded-examples: A collection of example projects using Embedded Swift](https://github.com/swiftlang/swift-embedded-examples)

edit: you could also try targeting none and then manually including Musl headers and linking Musl.

---

<div class="post-metadata">

**Author:** ![caco](https://avatars.discourse-cdn.com/v4/letter/c/74df32/32.png) [@caco](https://forums.swift.org/u/caco)\
**Post date:** [February 14, 2026, 9:37am UTC](https://forums.swift.org/t/scratch-linux-container-possible/84722/5 "2026-02-14T09:37:54Z")

</div>

Thanks, that pointed me in the right direction. Manually writing the sys calls in C with inline assembly totally works. Going back to my original proposition, I just had preliminary success linking with musl (and I've just read the edit xd):

```swift
146 KB Sat Feb 14 06:15:22 2026  bin

```

file bin:

```swift
bin: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, with debug_info, not stripped

```

it works as the sole file on a bare container! Here are the steps to reproduce:  
project.swift:

```swift
@_extern(c, "write")
func c_write(_ fd: Int32, _ buf: UnsafeRawPointer, _ count: Int) -> Int

print("Hello, world!")
let message = "hello from write()\n"
let _ = c_write(1, message, message.utf8.count)

```

```swift
swiftc -enable-experimental-feature Embedded -enable-experimental-feature Extern -wmo -Osize -target x86_64-unknown-none-elf Sources/project/project.swift -c -o main.o
ld.lld -static -m elf_x86_64 -o bin ./musl-sdk/crt1.o ./musl-sdk/crti.o ./musl-sdk/libc.a main.o ./musl-sdk/crtn.o --entry=_start --gc-sections

```

You need the musl-sdk files from manually compiled musl or from alpine linux musl-dev package for the proper target arch, and of course a development snapshot of swift.

The binary can be made smaller with strip and without debug\_info; also, I haven't tested bridging-headers in order to have consistent structs and preprocessor macros.  
WIP

---

<div class="post-metadata">

**Author:** ![Max\_Desiatov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/max_desiatov/32/20654_2.png) [@Max\_Desiatov](https://forums.swift.org/u/Max_Desiatov)\
**Post date:** [February 14, 2026, 11:00am UTC](https://forums.swift.org/t/scratch-linux-container-possible/84722/6 "2026-02-14T11:00:16Z")

</div>

> [@caco](#):
>
> I'm sure Swift Embedded can be used with a patched musl, but how?

You'd have to create a separate Swift SDK from scratch built against Musl with Embedded Swift, similarly to how we have two Swift SDKs against WASI-libc (a fork of Musl) built with Embedded and non-Embedded Swift.
