Preparing RHEL 7.5 base image for docker container from scratch

Whenever we start working with a container, to start you need a base image for any specific Operating system as per your requirement. You can get base images for many OS distributions except RHEL OS as it needs licensing. So in case, you want to prepare a base image of RHEL operating system you can follow below steps which are pretty straight forward: 

Prerequisites:

Mount RHEL 7.5 ISO on /mnt and create a .repo file:

[root@localhost yum.repos.d]# cat /etc/yum.repos.d/local.repo
[localrepo]
name=Local Repository
baseurl=file:///mnt
gpgcheck=0
enabled=1


OPTIONAL: HTTP/HTTPS repository is needed when you need to install any packages inside a container, or while preparing containers.

[root@localhost yum.repos.d]# cat httplocal.repo
[rhel-core]
name=RHEL 7.5 Core
baseurl=http://web1.repo.local/linux/RedHat/7.5Server/en/os/x86_64/
gpgcheck=0
enabled=1

OPTIONAL: Set proxy if needed in your network.


Steps to create a base image:

[root@localhost ~]# export img_root=/rhel7-root

[root@localhost ~]# rpm --root ${img_root} --initdb

[root@localhost ~]# rpm --root ${img_root} -ivh /mnt/Packages/redhat-release-server-7.5-8.el7.x86_64.rpm
warning: /mnt/Packages/redhat-release-server-7.5-8.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:redhat-release-server-7.5-8.el7  ################################# [100%]

[root@localhost ~]# rm -f ${img_root}/etc/yum.repos.d/*.repo

[root@localhost ~]# mkdir /rhel7-root/etc/yum.repos.d/

[root@localhost ~]# cp /etc/yum.repos.d/httplocal.repo ${img_root}/etc/yum.repos.d/httplocal.repo

[root@localhost ~]# rpm --root ${img_root} --import  /mnt/RPM-GPG-KEY-redhat-*

[root@localhost ~]# yum -y --installroot=${img_root} install yum

[root@localhost ~]# yum -y --installroot=${img_root} install net-tools iputils

Note: "net-tools and iputils" packages are optional, but in case if you won't install these packages, you will not be able to communicate to container as networking will be disabled. 

Once packages are installed check if chroot to the image location is working fine.

[root@localhost ~]# chroot ${img_root} /bin/bash
bash-4.2# ls

bash-4.2# exit
exit


[root@localhost ~]# rm -rf “${img_root}”/var/lib/rpm/__db*

[root@localhost ~]# rm -rf “${img_root}”/var/cache/yum

[root@localhost ~]# yum --installroot=${img_root} history sync

[root@localhost ~]# yum --installroot=${img_root} clean all


Create a tar file for a image filesystem and import it as an working base docker image:

[root@localhost ~]# tar -C ${img_root}/ -c . | docker import - rhel75base
sha256:4fe7ebbf140cabf573f7ce2c69a71f165c1a8d91d257d95e3ef073b9ca7f4213

Start a container and check if image is working fine and you are able to see virtual ethernet interface:

[root@localhost ~]# docker run -it rhel75base bash
bash-4.2# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)
        RX packets 6  bytes 508 (508.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


Note: Once in the container, you might need to set up proxy to reach httplocal.repo in case of further package installation.

Comments

Post a Comment

Popular posts from this blog

Recover or restore initramfs file in RHEL or CentOS 7

Space reclamation / UNMAP on RHEL or CentOS 7

How to recover /boot partition on RHEL or CentOS 7?