Skip to content

Instantly share code, notes, and snippets.

@gmrandazzo
Last active November 21, 2023 22:36
Show Gist options
  • Save gmrandazzo/77bd332d70af0ee2c09d3f0512b37296 to your computer and use it in GitHub Desktop.
Save gmrandazzo/77bd332d70af0ee2c09d3f0512b37296 to your computer and use it in GitHub Desktop.
CentOS7 Docker Image from Debian 12
#!/usr/bin/env bash
#
# Tested with altarch - armv7l
# If you want to build for other arch please change the
# mirror.centos.org/altarch/ with the appropriate architecture
#
if [ "$(id -u)" -ne 0 ]; then echo "Please run as root." >&2; exit 1; fi
apt-get install rpm dnf -y
export centos_root='/opt/centos_image/rootfs'
mkdir -p $centos_root
mkdir /etc/yum.repos.d
cat << 'EOF' > /etc/yum.repos.d/CentOS-Base.repo
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client. You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#
[base]
name=CentOS-$releasever - Base
# mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
baseurl=http://mirror.centos.org/altarch/$releasever/os/$basearch/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-AltArch-Arm32
#released updates
[updates]
name=CentOS-$releasever - Updates
# mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
baseurl=http://mirror.centos.org/altarch/$releasever/updates/$basearch/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-AltArch-Arm32
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
# mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
baseurl=http://mirror.centos.org/altarch/$releasever/extras/$basearch/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-AltArch-Arm32
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
# mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
baseurl=http://mirror.centos.org/altarch/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-AltArch-Arm32
EOF
rpm --root $centos_root --initdb
mkdir tmprpm
cd tmprpm
dnf install -y --releasever 7 --downloadonly --downloaddir . centos-release
rpm --root $centos_root -ivh *.rpm
rpm --root $centos_root --import $centos_root/etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
dnf -y --releasever 7 --installroot=$centos_root --setopt=tsflags='nodocs' --nogpgcheck --setopt=override_install_langs=en_US.utf8 install yum
sed -i "/distroverpkg=centos-release/a override_install_langs=en_US.utf8\ntsflags=nodocs" $centos_root/etc/yum.conf
mount -obind /dev/ $centos_root/dev/
mount -t proc none $centos_root/proc/
chroot $centos_root /bin/bash <<EOF
sed -e '/mirrorlist/ s/^#*/#/' -i /etc/yum.repos.d/*.repo
sed -e 's/#baseurl/baseurl/' -i /etc/yum.repos.d/CentOS-Base.repo
yum install -y procps-ng iputils
yum clean all
rm /etc/yum.repos.d/CentOS-armhfp-kernel.repo
EOF
rm -f $centos_root/etc/resolv.conf
umount $centos_root/proc/
umount $centos_root/dev/
echo "If you run docker as root please run this:"
echo "tar -C $centos_root -c . | docker import - centos7"
echo "instead if you run docker as non root user please run this:"
echo "tar -C $centos_root -c . | su <non root docker user> -c 'docker import - centos7'"
echo "Finally clean up /opt/centos_image/ with rm -rf /opt/centos_image"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment