How to create a full-fledged docker image

Last week we saw how to get docker installed on your Debian-based system. If you haven’t installed docker by reading this article, take some minutes and follow the steps of Install Docker on Debian 6 (Squeeze) on a VPS. Don’t be confused about the VPS, the instructions are the same for your local machine.

If you’re using Arch Linux simply run sudo pacman -S docker and start the service with

systemctl start docker

In the next section we’ll create a new Debian-based docker image which is no more than a tarball of a root file system.

Create the root file system

It’s fairly simple to install Debian’s base packages on your current machine and it doesn’t matter if you’re currently not on a Debian host system. There’s a tool called debootstrap which does all the work for you.

You can download it via your package manager.

# Arch Linux
yaourt -S debootstrap
# Debian/Ubuntu
aptitude install debootstrap

As soon as the utility is available on your system you just need to tell it the suite, target and the mirror so basically, in that order, the name of the Debian version, the directory where to install the packages and the URL from which the packages are downloaded. Essentially the mirror is optional but we’ll specify it anyway here.

$ mkdir wheezy
$ sudo debootstrap wheezy ./wheezy http://http.debian.net/debian/

This step can take a moment depending on your internet connection. You may have noticed that debootstrap requires root privileges to create a file system that belongs root.

Amongst the required suite and target Debian’s bootstrap utility also accepts options from which some are listed below.

SYNOPSIS
     debootstrap [OPTION...]  SUITE TARGET [MIRROR [SCRIPT]]

OPTIONS
     --include=alpha,beta
            Comma  separated  list  of  packages  which will be added to download and
            extract lists.

     --exclude=alpha,beta
            Comma separated list of packages which will be removed from download and
            extract lists. WARNING: you can and probably will exclude essential
            packages, be careful using this option.

     --variant=minbase|buildd|fakechroot|scratchbox
            Name of the bootstrap script variant to use. Currently, the variants
            supported are  minbase,  which  only  includes  essential  packages and
            apt; buildd, which installs the build-essential packages into TARGET;
            and fakechroot, which installs the packages without root privileges.
            Finally there is variant scratchbox, which is for creating targets for
            scratchbox usage. The default, with no --variant=X argument, is to create
            a base Debian installation in TARGET.

     --make-tarball=FILE
            Instead  of  bootstrapping,  make  a  tarball (written to FILE) of the
            downloaded packages.  The resulting tarball may be passed to a later
            --unpack-tarball.

Assuming you want to install additional packages such as zsh or git you can do so by specifying them with the --include option.

Create the tarball

Docker supports both non-compressed but also compressed images so a .tar, .tar.gz, .tgz, .bzip, .tar.xz or .txz image will be totally fine. Here we create a non-compressed tarball. In a subsequent step we’ll fix the tarball’s owner so that further work with docker can be done by non-root users.

$ sudo tar -C wheezy -cf wheezy.tar .
$ sudo chown you:you wheezy.tar

According debootstrap’s documentation one should be able to use its option –make-tarball to create the tarball during the bootstrap process by executing …

debootstrap --make-tarball=wheezy.tar wheezy ./wheezy http://http.debian.net/debian/

… but when doing so the image is some sort of corrupt and incomplete.

To share your image with the community you could upload the image and make it available for other docker users.

Coming soon

Next week you’ll be taught some basics to properly work with docker containers. Stay tuned.

2 Comments

  1. Vishal

    thanks,

    this is very helpful.pls, keep on posting some more tutorials for beginners..

  2. Ian

    The “Install Docker on Debian 6 (Squeeze) on a VPS” link is broken – should be http://slopjong.de/2014/09/03/install-docker-on-a-debian-based-vps/

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>