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