Pages

Sunday, April 22, 2018

Containerization - Creating Base Images

Base images are very important for creating containers. We download the base images like centos , ubuntu from Docker hub and use them to create our application images. What if there is a way to create our own Base image and not download from docker hub.

Iam creating a base image of Centos 6.9 on my Centos 7.4 Instance. Lets see how we can create the base image of Centos 6.9

1. Create a Base Image location
export centos_base='/work/docker-class/centos_image/rootfs'
mkdir -p $centos_base

2. Initialize RPM database using the command,
rpm --root $centos_base --initdb

3. Re install the package in the centos_base image that we created. We will be installing the centos-release package which contain our repository sources
yum reinstall --downloadonly --downloaddir . centos-release

rpm --root $centos_base -ivh http://mirror.centos.org/centos/6/os/x86_64/Packages/centos-release-6-9.el6.12.3.x86_64.rpm

Check the location “http://mirror.centos.org/centos/6/os/x86_64/Packages/” for the centos-release package.

rpm --root $centos_base --import  $centos_base/etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7


4. Install yum without docs and install only the english language files during the process as
yum -y --installroot=$centos_base --setopt=tsflags='nodocs'  --setopt=override_install_langs=en_US.utf8 install yum

Once this command ran successfully we can see the directory structure created in the $centos_base location

[root@sample docker-class]$   cd centos_image/rootfs/
[root@sample rootfs]$  ll
total 24
dr-xr-xr-x.  2 root root 4096 Oct  7 22:03 bin
dr-xr-xr-x.  2 root root  6 Sep 23 2011 boot
drwxr-xr-x.  2 root root 18 Oct  7 22:02 dev
drwxr-xr-x. 32 root root 4096 Oct  7 22:03 etc
drwxr-xr-x.  2 root root  6 Sep 23 2011 home
dr-xr-xr-x.  5 root root 53 Oct  7 22:02 lib
dr-xr-xr-x.  5 root root 8192 Oct  7 22:03 lib64
drwxr-xr-x.  2 root root  6 Sep 23 2011 media
drwxr-xr-x.  2 root root  6 Sep 23 2011 mnt
drwxr-xr-x.  2 root root  6 Sep 23 2011 opt
dr-xr-xr-x.  2 root root  6 Sep 23 2011 proc
dr-xr-x---.  2 root root  6 Sep 23 2011 root
dr-xr-xr-x.  2 root root 253 Oct  7 22:02 sbin
drwxr-xr-x.  2 root root  6 Sep 23 2011 selinux
drwxr-xr-x.  2 root root  6 Sep 23 2011 srv
drwxr-xr-x.  2 root root  6 Sep 23 2011 sys
drwxrwxrwt.  2 root root  6 Sep 23 2011 tmp
drwxr-xr-x. 13 root root  155 Oct 7 22:01 usr
drwxr-xr-x. 17 root root  197 Oct 7 22:01 var

5. Configure yum to avoid installing of docs and other language files than english
Add the below lines
distroverpkg=centos-release
override_install_langs=en_US.utf8
ntsflags=nodocs

To $centos_base/etc/yum.conf

6. Copy the resolv.conf to the new centos_base
cp /etc/resolv.conf $centos_base/etc

7. Chroot to the environment and install some additional packages as,
Chroot $centos_base /bin/bash << EOF
Yum install -y procps-ng iputils
Yum clean all
EOF

8. Clean the resolve .conf
Rm -f $centos_base/etc/resolv.conf

9. The last step is to convert the $fcento_base to a docker image file
[root@sample docker-class]$  tar -C $centos_base -c . | docker import - centos-baseimage
Sha256:380de41af927bc89b3f7f5e654b9c84930651cbbfeebac4c3219d0bc0bdd4fea

10. Check the docker images available in the machine now,
[root@sample docker-class]$  docker images
REPOSITORY                             TAG IMAGE ID    CREATED SIZE
centos-baseimage                   latest 380de41af927     37 seconds ago 400.3 MB

We can see that the centos-baseimage is available with our newly created $centos_base image.

11. Start a container using the base image as,
[root@sample docker-class]$  docker run -it centos-baseimage /bin/bash
bash-4.1# pwd
/

We can see that we can run a start from the centos base image that we created.

No comments :

Post a Comment