Skip to content

Today I learned

Learn something new every day

Menu
  • Home
  • About
  • Hello World! and Why I Started This Blog
Menu

How to Write Container Image FROM Scratch

Posted on 14/02/202216/02/2022 by Sagi Rosenthal

Today I learned how to write container images from scratch. When designing a product one must not just think about how to code it, but also how to deploy it. The containerization concept is a good choice in terms of security and also in being easily built and deploy. Building containers from scratch enhance the first, at a cost of the latter.

FROM scratch
COPY ./my_executable_file /
RUN ["/my_executable_file"]

That’s it. I know you probably thought it will be a hundred-line file…

A little bit background – Docker images are basically a .tar file with some metadata, Usually when we use FROM ubuntu or FROM alpine we start with some files in our tree. but starting from scratch is like a new empty directory. although ubuntu and alpine are small and considered secure images, it is still recommended to avoid them when possible.

The downside is that your image will lack any other operation you might want. need a directory for logs? mkdir is not possible. want to start a shell in runtime? no /bin/sh for you. but, you can use a builder image for that. it can also help in stripping your executable:

FROM alpine as builder
COPY ./my_executable_file /tmp
RUN strip /tmp/my_executable_file
RUN mkdir /tmp/logs

FROM scratch
#writing copy in one line is more efficient
COPY --from=builder ["/tmp/my_executable_file","/tmp/logs", "/"]
RUN ["/my_executable_file"]

Read more:
https://hub.docker.com/_/scratch

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

CAPTCHA


Recent Posts

  • How to Sell Stuff Online
  • How to Make Your Kubernetes Dev Cycle Faster
  • How To Be A Valuable Team Member
  • Don’t Make, Go Generate It
  • Why You Should Monkey Patch Your Next Unit-Test

Archives

  • July 2023
  • April 2023
  • January 2023
  • November 2022
  • August 2022
  • July 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022

Categories

  • Go
  • Learning
  • Software Utilities
Join the updates channel
© 2026 Today I learned | Powered by Minimalist Blog WordPress Theme