Creating a CentOS 6 Docker Image From Scratch

Now that CentOS 6.0 is long dead, and removed from the docker hub, how would you create your own image? It's easier than you think. Here's how!

Now that CentOS 6.0 is long dead, and removed from the docker hub, how would you create your own image? It's easier than you think. Here's how!

Recently, I needed to build some software on a CentOS 6.0 docker image, and couldn't find one anywhere to use. I needed to create what docker calls a base image, which is essentially a tarfile of a root directory of a Linux image. Here's how I recreated the image. I borrowed heavily from this blog post but changed it for CentOS 6.0 specifically, which has deprecated yum repos.

First, grab the CentOS 6.0 ISO from the vault, and spin up a VM with it.

Once you have your VM and are logged into it, create a folder to be our rpm buildroot for our image:

export centos_root='/centos_image/rootfs'
mkdir -p $centos_root

Then, make sure to remove the deprecated CentOS 6.0 repo, and change it to the vault repos

mv -v /etc/yum.repos.d/CentOS-Base.repo{,-backup}
touch /etc/yum.repos.d/CentOS-Base.repo

Make CentOS-Base.repo into the following:

[C6.0-base] 
name=CentOS-6.0 - Base 
baseurl=http://vault.centos.org/6.0/os/$basearch/ 
gpgcheck=1 
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 
enabled=1 

[C6.0-updates] 
name=CentOS-6.0 - Updates 
baseurl=http://vault.centos.org/6.0/updates/$basearch/ 
gpgcheck=1 
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 
enabled=1 

[C6.0-extras] 
name=CentOS-6.0 - Extras 
baseurl=http://vault.centos.org/6.0/extras/$basearch/ 
gpgcheck=1 
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 
enabled=1 
CentOS-Base.repo

Then, download all the RPMs we need:

yum clean all
yum makecache
# install download plugin for yum
yum install yum-plugin-downloadonly
# initialize rpm database
rpm --root $centos_root --initdb
# download and install the centos-release package, it contains our repository sources
yum reinstall --downloadonly --downloaddir . centos-release
rpm --root $centos_root -ivh --nodeps centos-release*.rpm
rpm --root $centos_root --import $centos_root/etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
# install yum without docs and install only the english language files during the process
yum -y --installroot=$centos_root install yum
# configure yum to avoid installing of docs and other language files than english generally
sed -i "/distroverpkg=centos-release/a override_install_langs=en_US.utf8\ntsflags=nodocs" $centos_root/etc/yum.conf
# chroot to the environment and install some additional tools
cp /etc/resolv.conf $centos_root/etc
# mount the device tree, as its required by some programms
mount -o bind /dev $centos_root/dev
chroot $centos_root /bin/bash <<EOF
yum install -y procps-ng iputils
yum clean all
EOF
rm -f $centos_root/etc/resolv.conf
umount $centos_root/dev

You're done! Now you can tar up the chroot, and it's importable into docker!

tar -cvzf centos-6.0-base.tar.gz -C $centos_root -c .
cat centos-6.0-base.tar.gz | docker import - taniumcentos:6.0