Recently, someone asked me how to create a Docker base image for a Linux variant that they are creating. In this blog, I will cover what a Linux base image is and how to create new base images.
Linux Docker base image
Following is a sample Dockerfile to create a Python webserver Docker container image.
FROM ubuntu:14.04 # Update the sources list RUN apt-get update # Update RUN apt-get install -y python2.7 python-pip # Install app dependencies RUN pip install Flask==0.9.0 # Bundle app source COPY simpleapp.py /src/simpleapp.py EXPOSE 8000 CMD ["python", "/src/simpleapp.py", "-p 8000"]
In the above example, we have given “FROM ubuntu:14.04” in the first line. ubuntu:14.04 is the base image used here. 1 common question that I get asked is that does it mean that this container contains everything present in Ubunbu VM? That is not true. What ubuntu container image contains is the packages, libraries and tools associated with Ubuntu along with the root filesystem. To give a size comparison, Ubuntu container image is around 180mb while the Ubuntu VM size is around 1GB.
Docker hub contains base container images for all major distributions including Ubuntu, rhel, centos, debian etc. It is always better to take the official images as they would have the latest security patches. The official images are maintained by the distribution vendor who works closely with Docker.
Creating Linux Docker Container base image
Docker repository provides a script “mkimage.sh” that can be used to create base images for different Linux variants. When Docker is installed in Ubuntu 14.04, “mkimage.sh” is available in “/usr/share/docker-ce/contrib/”. Following output shows the “help” for “mkimage.sh”.
$ ./mkimage.sh --help usage: mkimage.sh [-d dir] [-t tag] [--compression algo| --no-compression] script [script-args] ie: mkimage.sh -t someuser/debian debootstrap --variant=minbase jessie mkimage.sh -t someuser/ubuntu debootstrap --include=ubuntu-minimal --components=main,universe trusty mkimage.sh -t someuser/busybox busybox-static mkimage.sh -t someuser/centos:5 rinse --distribution centos-5 mkimage.sh -t someuser/mageia:4 mageia-urpmi --version=4 mkimage.sh -t someuser/mageia:4 mageia-urpmi --version=4 --mirror=http://somemirror/ mkimage.sh -t someuser/solaris solaris
Each Linux distribution provides a helper script to create the base filesystem. For example, “debootstrap” is used for debian/ubuntu variants, “rinse” is used for centos variants. “mkimage.sh” uses the root filesystem created by helper script to import that to a Docker container. The helper script installs the necessary packages and sets up the root filesystem. If you are creating a new flavor of Linux and if it based on 1 of the major distributions, we can extend the helper script. Otherwise, we can create a new helper script based on the current examples. It will be good to contribute the new helper script back to “mkimage” in Docker repository.
Following is an example to create Debian Jessie base image with mkimage.sh:
sudo ./mkimage.sh -t smakam/debian:jessie debootstrap jessie
The above command will create a new container image “smakam/debian:jessie”. It pulls the necessary files from the repository.