This article covers how to build a RHEL docker image from scratch and use it in docker. In order to build this image you either should have access to RHN or have DVD to install the base system from. I'll be installing it from a DVD.

We'll be using yum and rpm to initialize the image's root directory. Build RHEL Image

First install yum and rpm

$ sudo apt-get install yum rpm

Next step is to initialize the RPM database so that we can install all of the software we need in the image: we will need to create the directory for the database because RPM expects it to exist, and then use the RPM "rebuilddb" command:

$ rhel6_root=/vdisks/chroot/rhel6 $ mkdir -p ${rhel6_root}/var/lib/rpm $ sudo rpm --root $rhel6_root --initdb

In order for YUM to manage and install software into our image it needs to know which RHEL version to install - for this the package redhat-release to be installed in the image.

$ sudo rpm --root ${rhel6_root} -ivh /mnt/cdrom/Packages/redhat-release*

Next configure YUM repository

$ cat ${rhel6_root}/etc/yum.repos.d/rhel-dvd.repo [rhel-dvd] name=Red Hat Enterprise Linux $releasever - $basearch - DVD baseurl=file:///mnt/cdrom

Now install yum and rpm in the image

$ sudo yum -y --installroot=${rhel6_root} install yum rpm

At this point you have a small environment which you can chroot into and modify it the way you like

$ sudo chroot ${rhel6_root} /bin/bash

You will drop to a bash shell in the newly created system, I won't do any further changes to this system except modifying the banner

# echo 'RHEL 6 Docker image' > /etc/motd # exit

Import Docker Image

Now let's import this new system into docker:

$ sudo tar -C rhel6/ -c . | docker import - aossama/rhel6 f013651179cf1192375f66f20f0f61246aaa60c462a60ff03a3d524c58123ae8

If the import goes well, you will see the ID of the newly imported image

First check the images list:

$ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE aossama/rhel6 latest f013651179cf 19 seconds ago 268.4 MB

And start the new image

$ docker run --rm --name=rhel6 aossama/rhel6 cat /etc/redhat-release Red Hat Enterprise Linux Server release 6.5 (Santiago)

$ docker run --rm --name=rhel6 aossama/rhel6 cat /etc/motd RHEL 6 Docker image

And voilà, you just built a RHEL6 docker image, imported it, and ready to dockerize it.