So what is the most minimal starter image we can work from?
First of all, minimal doesn't necessarily mean storage space. I'm account for number of lines of code written in configuration files as well. The equation might be written as:
[ docker container size ] = [ lines of code ] + [ number of files/directories ]
So what's the answer? After some internet searching, that answer came pretty quickly, and quite reasonably - from "scratch"!
The first line of `Dockerfile` is literally:
FROM scratch
Now the container should do something to be counted as a valid one. And the most minimal container function I can think of is one that echos out a specified string. Looks like I'm going to need the `/bin/echo` binary for that.
I could have grabbed the binary from my currently running linux host (i.e. `/bin/echo`) but that seems less than ideal. I want a canonized set of binaries, not just the ones I happen to by running locally. If not for the sake of a standard, at least something anyone can use by following these instructions.
So looking around the already-provided images in the Docker library, I found `ubuntu`. Works for me!
To extract the binaries (along with the rest of the filesystem) I took these steps:
tim@timverly.com:~/docker/playground$ sudo docker run ubuntu /bin/echo 'done!'
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
203137e8afd5: Pull complete
2ff1bbbe9310: Pull complete
933ae2486129: Pull complete
a3ed95caeb02: Pull complete
Digest: sha256:1bea66e185d3464fec1abda32ffaf2a11de69833cfcf81bd2b9a5be147776814
Status: Downloaded newer image for ubuntu:latest
done!
tim@timverly.com:~/docker/playground$
Then to see what I've got I like to run the following:
tim@timverly.com:~/docker/playground$ echo ''; sudo docker images -a; echo ''; sudo docker ps -a; echo '';
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 97434d46f197 2 weeks ago 188 MB
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7ee11f791fc7 ubuntu "/bin/echo done!" About a minute ago Exited (0) About a minute ago jolly_torvalds
tim@timverly.com:~/docker/playground$
Jolly indeed.
Alright, now I just need to extract the filesystem from the `ubuntu` image. Looking at the above output I see the ID for the container that was created after running the `docker run` command is "7ee11f791fc7". Just plug that into the `docker export` command, pipe the output of that to `tar` and we're golden.
tim@timverly.com:~/docker/playground$ mkdir -p tmp/ubuntu
tim@timverly.com:~/docker/playground$ cd tmp/ubuntu
tim@timverly.com:~/docker/playground/tmp/ubuntu$ sudo docker export 7ee11f791fc7 | sudo tar -vx
.dockerenv
.dockerinit
bin/
bin/bash
bin/bunzip2
bin/bzcat
[a ton of files listed]
var/spool/cron/
var/spool/cron/crontabs/
var/spool/mail
var/spool/plymouth/
var/spool/rsyslog/
var/tmp/
tim@timverly.com:~/docker/playground/tmp/ubuntu$
NOTE: You need the `sudo` in `sudo tar -vx` in order to create the `/dev/` files without problems.
So just a recap: I've found the `scratch` image to build my container upon, found the `ubuntu` image to grab canonical binaries from, and exported the `ubuntu` filesystem to my current working directory.
Now the real fun begins...
A typical linux binary, even one as simple as `bin/echo`, is linked to library files on your computer. Those library files will need to be added to our container as well so `echo` can run. Otherwise you get an error saying something like:
no such file or directory
docker: Error response from daemon: Container command not found or does not exist.
Oh it exists Mr. Daemon, it's the library files that don't.
(All seriousness aside, I'd like to see a `mattd` daemon process running on my linux system that kills off other evil resource-hoarding processes, Bourne style.)
Now if we were lazy we could just grab everything from `lib/` and `lib64/` from the `ubuntu` filesystem and be done with it. But where's the fun in that?
So how can we find out which linux library files `bin/echo` needs, and only those files?
In comes the tool of the day: ldd
Just pass the binary file you're interested in to `ldd` and it spits out all the shared library info you need:
tim@timverly.com:~/docker/playground/tmp/ubuntu$ ldd bin/echo
linux-vdso.so.1 => (0x00007ffd1c956000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fbce1149000)
/lib64/ld-linux-x86-64.so.2 (0x00007fbce150e000)
tim@timverly.com:~/docker/playground/tmp/ubuntu$
To read this, you're looking for full filepaths. So the first line of output can be ignored since it only contains a filename, and no path.
The files we are interested then are:
/lib/x86_64-linux-gnu/libc.so.6
/lib64/ld-linux-x86-64.so.2
To keep things neat and tidy, I like to make a specialized folder in the build context (i.e. the directory `Dockerfile` is in) instead of referencing files within the exported `ubuntu` filesystem.
So let's make a directory for the build files needed and copy them over.
tim@timverly.com:~/docker/playground/tmp/ubuntu$ cd ../..
tim@timverly.com:~/docker/playground$
tim@timverly.com:~/docker/playground$ sudo mkdir build-fs/
tim@timverly.com:~/docker/playground$ sudo mkdir -p build-fs/bin/ build-fs/lib/x86_64-linux-gnu/ build-fs/lib64/
tim@timverly.com:~/docker/playground$ sudo cp tmp/ubuntu/bin/echo build-fs/bin/echo
tim@timverly.com:~/docker/playground$ sudo cp tmp/ubuntu/lib/x86_64-linux-gnu/libc.so.6 build-fs/lib/x86_64-linux-gnu/libc.so.6
tim@timverly.com:~/docker/playground$ sudo cp tmp/ubuntu/lib64/ld-linux-x86-64.so.2 build-fs/lib64/ld-linux-x86-64.so.2
Oh, btw, it's best to add the `tmp` directory to your `.dockerignore` file, so that it isn't passed to the build command - it'll speed up the build process.
tim@timverly.com:~/docker/playground$ cat .dockerignore
*tmp*
tim@timverly.com:~/docker/playground$
With that information in hand, we can write the Dockerfile, which ends up being:
FROM scratch
ADD build-fs/* /
CMD ["/bin/echo","success"]
Or so we wish. Actually, docker is kind of dumb, and the end result is actually:
.dockerenv
.dockerinit
dev/
dev/console
dev/pts/
dev/shm/
echo
etc/
etc/hostname
etc/hosts
etc/mtab
etc/resolv.conf
ld-linux-x86-64.so.2
proc/
sys/
x86_64-linux-gnu/
x86_64-linux-gnu/libc.so.6
See how `echo` and the library files aren't in the `/bin/` and `/lib/` directories respectively?
So we have to be a bit more detailed in the `Dockerfile`:
tim@timverly.com:~/docker/playground$ cat Dockerfile
FROM scratch
ADD build-fs/bin/echo /bin/echo
ADD build-fs/lib/x86_64-linux-gnu/libc.so.6 /lib/x86_64-linux-gnu/libc.so.6
ADD build-fs/lib64/ld-linux-x86-64.so.2 /lib64/ld-linux-x86-64.so.2
CMD ["/bin/echo","Success!"]
tim@timverly.com:~/docker/playground$
Now build this mofo:
tim@timverly.com:~/docker/playground$ sudo docker build -t fingers_crossed .
Sending build context to Docker daemon 2.03 MB
Step 1 : FROM scratch
--->
Step 2 : ADD build-fs/bin/echo /bin/echo
---> 0d900a7e3ca8
Removing intermediate container 23bff5e69acf
Step 3 : ADD build-fs/lib/x86_64-linux-gnu/libc.so.6 /lib/x86_64-linux-gnu/libc.so.6
---> 2225fbd1a6cc
Removing intermediate container fcc8005b7a35
Step 4 : ADD build-fs/lib64/ld-linux-x86-64.so.2 /lib64/ld-linux-x86-64.so.2
---> 2e88df258ab7
Removing intermediate container 97474b7bc693
Step 5 : CMD /bin/echo Success!
---> Running in b280e7933316
---> 80bebff6d80c
Removing intermediate container b280e7933316
Successfully built 80bebff6d80c
tim@timverly.com:~/docker/playground$
... and now run said mofo:
tim@timverly.com:~/docker/playground$ sudo docker run fingers_crossed
Success!
tim@timverly.com:~/docker/playground$
And there ya go!
p.s. There's actually a quicker way to build images and run them with `docker import`, but it's almost 3am and I need to go to sleep.