Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Latest commit

 

History

History
46 lines (24 loc) · 2.41 KB

README.md

File metadata and controls

46 lines (24 loc) · 2.41 KB

FROM scratch

Demonstration of creating a base image that contains a statically-linked program.

The background

In classic Linux distro, some libraries are available only for dynamic linking (ex: GLib). Even the glibc requires a dynamically loaded library for some functions (read here).

Indeed, on getaddrinfo() calls, static and dynamic flavours of glibc always dynamically load at least libnss_files.so.2 which depends on libc.so itself. So you cannot make a static self-sufficient executable that uses getaddrinfo() API with the standard glibc.

In order to create an empty Docker image that contains just an executable, you will need statically linked program that will not require or load any dynamic libraries, since the image will not have any shared libraries.

The best choice is to use musl-libc and Alpine Linux, where all the hard work has already be done.

The Docker image

The Docker image uses a multi-stage build.

  • The first stage prepares the build environment (compiler and libraries).
  • The second one creates a bare image with only the program and its data.

Running the demo

From the Docker registry

docker run --rm -ti rene2/fromscratch

Nota: This image is made by the automated build system on Docker Hub's infrastructure.

With a fresh image

The demo.sh script builds and inspects the image (jq is required to filter the inspect output, or comment line 8).

Then, it runs the unique executable (a.out) in the container.

demo.png

To go further

Some explanations.