Docker is growing with amazing features, and it becomes a platform to deploy services even in embedded linux enviroment. The container image creation is an important part of process.
Docker has done great job to manage the container image to make it to be in layer fashion, this is especially important for storage efficiency.
The following post shows what I did to create a scratch hello image and a minimal linux image based on alpine using Dockerfile.
Dockerfile is the way docker has defined to create docker container image. It is kind of like GNU Makefile to build binary application from C source code. It has a set of simple instructions. For details, check Dockerfile reference and best Dockerfile practice
(There is another way to create a tar file and use “docker import “ to create a container name. But here I only use Dockerfile to create.)
Inside Dockerfile, each line docker build will create a layer. Layer is marked with checksum,it is used for file systme overlay to avoid duplication of same content across containers. More details, check here.
hello image from scratch
Objective: create a minimum container image to show “Hello from Docker by WENWEI WENG”.
How: Docker has a minimal image “scratch”, which is empty. It can be used as a starting point for building containers. Using the “scratch” image as a base to tell the build process that I want the next command in the Dockerfile to be the first filesystem layer in my image. From there, I add a static linked executable “hello” binary.
hello.c file:
it is good to notice that to print hello, printf() is not used, instead use syscall(), because printf() requires libc, which it needs to be avoided in this case. it is needed to build and linked statically like below.
Build hello.c
Dockerfile:
Build container image
Build a minimal linux image with a file named “test-alpine-weng.txt”
Objective: a minimal linux image with a file named “test-alpine-weng.txt”
How: starting with “alpine” image as base, add file “test-alpine-weng.txt””.