Slimmest golang static container

Tanvir Shahid
2 min readAug 17, 2021
Slimmest golang application container

If you read this post then by now you know what is distroless container and how you can take advantage of it to inject your golang project into a super slimed docker container. Now, in this post we will see how we can make the container more slimmer.

TBH, it is quite simple if you don’t need cgo and SSL (https can be done from nginx proxy) in your golang project. The biggest advantage of Go is, it’s statically typed compiled programming language and that’s why we can use static distroless container to package our application if you really don’t need any 3rd party package that depends on cgo. However, the -race parameter to compile a Go app has circular dependency with cgo (read here). So, if we compromise that -race parameter then our container size will be super slimmer than usual. You probably can use -race parameter to compile your app for staging and local development environment, test as much as possible, tackle all the race condition there. And finally build a production container without -race using distroless static container to take the benefits on memory usage of your app.

Now, let’s imagine a golang app and it has following directory structure along with a Dockerfile in it’s root directory:

.
├── bin
│ └── testapp
├── configs
│ └── env.json
├── controllers
│ └── index.go
│ └── profile.go
│ └── signin.go
├──…

--

--

Tanvir Shahid

🦾 Startup mechanic, 💻 Coder, 🧔🏻Techaholic, ₿ Crypto enthusiast, 🍣 Sushi, System architect @ https://retail-ai.jp | https://jp.linkedin.com/in/tanvir-shahid