How to create rhel docker base image

1. Create base image through redhat release version

Precondition:

1. host with docker running

2. Image version should the same as host. eg: if host is rhel7, the image also should be rhel7

 

Steps:

1 Configure rhel repo on /etc/yum.repo.d/define.repo

[root@dhcp-128-233 ~]# cat /etc/yum.repos.d/define.repo

[rhel-7.3]

name=RHEL_7.3

baseurl=http://download.eng.pek2.redhat.com//pub/rhel/rel-eng/RHEL-7.3-20161019.0/compose/Server/x86_64/os/

enabled=1

gpgcheck=0

 

2 Create a folder for our new root structure

$ export redhat_root='/redhat_image/rootfs'

$ mkdir -p $redhat_root

 

3 initialize rpm database

$ rpm --root $redhat_root --initdb

 

4 download and install the redhat-release package, it contains our repository sources

$ yum reinstall --downloadonly --downloaddir . redhat-release

$ rpm --root $redhat_root -ivh redhat-release*.rpm

$ rpm --root $redhat_root --import  $redhat_root/etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

 

5 install yum without docs and install only the english language files during the process

$ yum -y --installroot=$redhat_root --setopt=tsflags='nodocs' --setopt=override_install_langs=en_US.UTF-8 install yum

 

6 configure yum to avoid installing of docs and other language files than english generally

$ sed -i "/distroverpkg=redhat-release/a override_install_langs=en_US.UTF-8\ntsflags=nodocs" $redhat_root/etc/yum.conf

 

7 chroot to the environment and install some additional tools

$ cp /etc/resolv.conf $redhat_root/etc

$ chroot $redhat_root /bin/bash <<EOF

yum install -y procps-ng iputils

yum clean all

EOF

$ rm -f $redhat_root/etc/resolv.conf

 

8 Install docker on host

$ yum install -y docker-engine

$ systemctl restart docker

$ systemctl enable docker

 

9 Create docker image

[root@dhcp-128-233 yum.repos.d]# tar -C $redhat_root -c . | docker import - redhat73

sha256:dd116699a0a0532d0f8d3bb1fbfe8019aabf01fdbd75364bad63446acb88884b

[root@dhcp-128-233 yum.repos.d]# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

redhat73            latest              dd116699a0a0        8 seconds ago       139.8 MB

 

10 Run container with created image

[root@dhcp-128-233 yum.repos.d]# docker run -it redhat73 /bin/bash

bash-4.2#

 

2. Create base image through redhat installation iso

Precondition:

1. Download a iso file to host

2. host with docker running

3. Image version should the same as host. eg: if host is rhel7, the image also should be rhel7

 

Steps:

1  Mount iso file to local

# mkdir -p /mnt/rhel7-repo

# mount -o loop RHEL-7.3-20161019.0-Server-x86_64-dvd1.iso /mnt/rhel7-repo/

 

2 Create a new RPM root directory

# mkdir /rhel7-root

# export rpm_root=/rhel7-root

 

3 Initialize the new RPM root directory

# rpm --root ${rpm_root} --initdb

 

4 Install the redhat-release-server rpm package for RHEL 7.3

# rpm --root ${rpm_root} -ivh /mnt/rhel7-repo/Packages/redhat-release-server-7.3-7.el7.x86_64.rpm

 

5 Configure yum repositories as required

# mkdir ${rpm_root}/etc/yum.repos.d

# cat >${rpm_root}/etc/yum.repos.d/rhel73.repo<<EOF

[rhel73]

baseurl=file:///mnt/rhel7-repo

enabled=1

EOF

 

6 Import GPG keys

# rpm --root ${rpm_root} --import  /mnt/rhel7-repo/RPM-GPG-KEY-redhat-*

 

7 Install minimalistic RHEL OS

# yum -y --installroot=${rpm_root} install yum

 

8 Chroot to the new RHEL installation and perform any additional customizations

# chroot ${rpm_root} /bin/bash

bash-4.2# cat /etc/redhat-release

Red Hat Enterprise Linux Server release 7.3 (Maipo)

 

9 Convert this RHEL installation to a docker image

# tar -C ${rpm_root}/ -c . | docker import - rhel73_iso

sha256:23578d0af38c6791507fbb9fc16b37a8f740c6a14c4137cad1b600e94176e092

# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

rhel73_iso          latest              23578d0af38c        12 seconds ago      289.8 MB

 

10 Run a container with new image

# docker run -it rhel73_iso /bin/bash

bash-4.2#

# docker ps -a

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES

e035efa2afc2        rhel73_iso          "/bin/bash"         17 minutes ago      Exited (127) 14 minutes ago                       dreamy_shaw

猜你喜欢

转载自blog.csdn.net/qq_33744949/article/details/80283590