How to build a docker image from scratch using its root file system?
We will be demonstrating the custom docker image build by using the linux debian Distribution’s rootfs.
Our workstation environment is Debian Buster, which will be used to build a debian docker Image from a same distro linux rootfs.
Lets get our environment upto speed and download the necessary build tool for our project.
We need the command debootstrap
which gives the necessary build tools to go ahead.
$ wget http://ftp.debian.org/debian/pool/main/d/debootstrap/debootstrap_1.0.116_all.deb
from your debian/ubuntu apt repository you can install as shown below:
# apt-get install debootstrap
Install the debootstrap package on the Host workstation.
$ sudo dpkg --install debootstrap_1.0.116_all.deb
To Neatly organize our build directory we can create a directory where we can download the rootfs.
$ sudo mkdir /tmp/debian-build
Downloading the debian rootfile system
Now we begin our process by looking up the Debian latest rootfs and running it against the debootstrap as shown below.
$ sudo debootstrap stable [BUILD-DIR] http://deb.debian.org/debian/
And also you can really explicit and filter some specifics as follows and choose a specific variant of debian OS as shown below.
$ sudo debootstrap --variant=minbase --components=main,contrib --include=dirmngr,apt-transport-https --arch=amd64 buster debian-buster http://deb.debian.org/debian/
Now you can also append the –verbose flag to print the background information.
Once you have successfully downloaded the rootfs, you can then verify the directory, which is /tmp/debian-build build directory in our case.
$ ls /tmp/debian-build
chroot-ing to the build directory
Now we need to chroot into the [BUILD-DIR] as follows
$ sudo chroot /tmp/debian-build
Now please note how the PS! prompt changes are you are chrooted to the latest build directory.
root@node03:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@node03:/# pwd /
Now we are chroot-ed into our Debian build directory and our main goal is to remove as many unneeded packages as possible to downsize the image.
We now need to follow some best practices to implement and configure the build.
Firstly we create a file /etc/apt/apt.conf.d/10-assume_yes, as shown below which auto assumes the apt commands.
And run the below commands to remove the packages.
# echo "APT::Get::Assume-Yes \"true\";" | tee /etc/apt/apt.conf.d/10-assume_yes # apt-get remove --purge -y $(apt-mark showauto) && rm -rf /var/lib/apt/lists/* # apt-get autoremove -y # apt-get clean # apt-get remove --allow-remove-essential e2fsprogs e2fslibs nano pinentry-curses whiptail kmod iptables iproute2 dmidecode # apt-get clean
We should now exit of the chroot environment by typing exit
With this you will be left with the total size of around 160MB.
root@node03:~# du -sh /tmp/debian-build 164M /tmp/debian-build
Once we are satisfied with the rootfs contents we proceed to the next steps to accomplish our goal of building a docker image.
Processing the rootfs directory into a tar file.
# sudo tar --verbose --create --file archive-name.tar --directory [BUILD-DIR] .
In our case the [BUILD-DIR] is debian-build directory.
# sudo tar --verbose --create --file debian-vamshi.tar --directory debian-build .
Creating a Docker image from the tar file
# cat archive.tar | sudo docker import - [Docker-image-name]
# cat debian-vamshi.tar | sudo docker import - debian-vamshi
Once the import operation is successfully completed we are left with a pure debian docker image to be used up.
By Successfully Stripping down the debian Image, I finally ended up with 178MB size of of debian image.
$ docker images docker-repo/debian-vamshi latest f57963009dd8 About a minute ago 178MB
Thus the process of debian image stripdown and creation of a docker image.
The same process applies to the Ubuntu linux distro, which will be discussed in another post.