Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Creating minimal CentOS docker image from scratch
# Create a folder for our new root structure
$ export centos_root='/centos_image/rootfs'
$ mkdir -p $centos_root
# 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 centos-release*.rpm
$ rpm --root $centos_root --import $centos_root/etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
# install yum without docs and install only the english language files during the process
$ yum -y --installroot=$centos_root --setopt=tsflags='nodocs' --setopt=override_install_langs=en_US.utf8 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
$ chroot $centos_root /bin/bash <<EOF
yum install -y procps-ng iputils
yum clean all
EOF
$ rm -f $centos_root/etc/resolv.conf
# install and enable docker
$ yum install -y docker
...
$ systemctl start docker
# create docker image
$ tar -C $centos_root -c . | docker import - centos
sha256:28843acfa85226385d2db0196db72465729db38088ea98751d4a5c3d4c85ccd
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 28843acfa852 11 seconds ago 287.7 MB
# run docker image
$ docker run --rm centos cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
Source: https://www.sidorenko.io/post/2016/07/creating-container-base-image-of-centos/
@dedalqq

This comment has been minimized.

Copy link

commented Oct 1, 2018

[root@8a986baef9eb /]# rpm --root $centos_root -ivh centos-release*.rpm
warning: centos-release-7-5.1804.4.el7.centos.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
error: Failed dependencies:
        /bin/sh is needed by centos-release-7-5.1804.4.el7.centos.x86_64
        coreutils is needed by centos-release-7-5.1804.4.el7.centos.x86_64
        grep is needed by centos-release-7-5.1804.4.el7.centos.x86_64

need add --nodeps

rpm --nodeps --root $centos_root -ivh centos-release*.rpm
@gbolo

This comment has been minimized.

Copy link

commented Feb 8, 2019

Very nice! smallest centos image I have ever seen!

@jmturner

This comment has been minimized.

Copy link

commented Apr 17, 2019

You need to add --nodeps to the rpm --root $centos_root -ivh centos-release*.rpm line. Otherwise, it asks for a shell, coreutils, and grep.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.