Home > Back-end >  What is the smallest image that can be used to leave docker-compose running indefinitely?
What is the smallest image that can be used to leave docker-compose running indefinitely?

Time:04-03

One of my unit tests kills a running Docker container. For the unit test to work, I need to start up a container with docker-compose run, such that it has something to kill.

What is the smallest and simplest image that can be used with a docker-compose.yml to make this happen?

Note that a Dockerfile isn't required.

CodePudding user response:

If you're looking for literally the very smallest image, and it literally needs to do nothing other than not exit, k8s.gcr.io/pause should be close.

This image is part of Kubernetes and you can read its source. The Makefile compiles a small C program into a static binary, and then builds that into a FROM scratch image. That means it doesn't even spend the megabyte for the BusyBox static binary, so it doesn't contain unnecessary fluff like a shell, tail, Alpine's apk package manager, or anything else at all.

~% docker images
REPOSITORY         TAG       IMAGE ID       CREATED        SIZE
k8s.gcr.io/pause   3.7       221177c6082a   3 weeks ago    711kB
busybox            latest    2fb6fc2d97e1   3 weeks ago    1.24MB
alpine             latest    76c8fb57b6fc   4 days ago     5.57MB

The one thing this image does other than sleep forever is to notice if any child processes exit and wait(2) for them. Since it expects to be process ID 1, this cleans up any orphaned processes that get reparented to the init process (euphemistically, "reaping zombies"). Kubernetes uses this as the root for a multi-container pod where the pods share various namespaces; also see What work does the process in container "gcr.io/google_containers/pause:0.8.0" do? or Ian Lewis's The Almighty Pause Container (which includes the entire C source code as part of a blog post).

You could probably build an even smaller image if you rewrote the binary in assembly so you didn't need the standard boilerplate to start and stop a C process, but it's probably not worth the effort.

CodePudding user response:

You just need a docker-compose file for that

version: '3.9'
services:
  myservice:
    image: alpine:latest
    command: tail -f /dev/null

You can probably find other images to run if you prefer something else. nginx:alpine springs to mind. That doesn't need a command, so you'd save a line in the docker-compose file.

  • Related