Create a CentOS 7 Docker Image From Scratch

Docker is an amazing tool and has revolutionized the software and IT industries extremely rapidly. The standard workflow for most users involves building on top of an image created by someone else, often the maintainers of the project or application you are installing.

However, there may be times when you want to build your own #Docker image, such as a strict compliance environment, or when you want to optimize the image layout or disk usage.

Below is one way of creating a docker image. There are many more, but this was one that I found straightforward and easy to understand. It doesn't create the smallest image possible, but it is a good place to start from.

Start with a Fedora computer, be it a VM or a physical development workstation

Install some virtualization packages: dnf install libvirt virt-install virt-manager virt-viewer libguestfs-tools-c lorax

Download a CentOS 7 Kickstart file to /tmp. I've shared one as a github gist or you can grab the original from the CentOS cloud SIG.

fedora :: /tmp » curl https://gist.githubusercontent.com/anthonyclarka2/052f3a24ce722dfadf33c2f2f4807ee7/raw/01c0d7ab7ff1cfaffe3d73c489b2b7f59db27758/centos7.ks -o /tmp/centos7.ks
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  3210  100  3210    0     0  21543      0 --:--:-- --:--:-- --:--:-- 21543

Download the CentOS 7 boot ISO image:

fedora :: /tmp » curl http://mirror.centos.org/centos/7/os/x86_64/images/boot.iso -o /tmp/boot.iso
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  507M  100  507M    0     0  15.8M      0  0:00:32  0:00:32 --:--:-- 15.3M

Now use those 2 files to create the base for the image:

fedora :: /tmp » sudo livemedia-creator --make-tar --iso=/tmp/boot.iso --ks=/tmp/centos7.ks --image-name=centos-7-docker.tar.xz
2019-02-03 12:04:14,644: livemedia-creator v29.18-1
2019-02-03 12:04:14,808: disk_img = /var/tmp/centos-7-docker.tar.xz
2019-02-03 12:04:14,809: Using disk size of 3002MiB
2019-02-03 12:04:14,809: install_log = /tmp/virt-install.log
2019-02-03 12:04:14,966: qemu vnc=127.0.0.1:0
2019-02-03 12:04:14,966: Running qemu
2019-02-03 12:04:15,091: Processing logs from ('127.0.0.1', 52022)
2019-02-03 12:34:08,774: Installation finished without errors.
2019-02-03 12:34:08,775: Shutting down log processing
2019-02-03 12:34:08,775: unmounting the iso
2019-02-03 12:34:08,847: Partition mounted on /var/tmp/tmpmgcijjqi size=3145728000
2019-02-03 12:35:44,889: Disk Image install successful
2019-02-03 12:35:44,890: SUMMARY
2019-02-03 12:35:44,890: -------
2019-02-03 12:35:44,890: Logs are in /tmp
2019-02-03 12:35:44,890: Disk image is at /var/tmp/centos-7-docker.tar.xz
2019-02-03 12:35:44,890: Results are in /var/tmp

Note that you need to run the command “livemedia-creator” as root. You're using it to create a tar file of an entire Linux system's disk. It's compressed by the “xz” utility by default, which is the same compression scheme used by Docker and other container libraries.

That will take 30+ minutes. Open a new terminal on that workstation or VM and follow the log. Please be patient, as it often takes a few minutes for the log to start generating output because the VM takes time to boot. I have placed the logs from one run up on my github gists: virt-install.log, livemedia.log, and program.log.

fedora :: /tmp » tail -f virt-install.log

The resulting file, placed in /var/tmp/centos-7-docker.tar.xz can now be used in a very basic Dockerfile to create an image:

FROM scratch

ADD centos-7-docker.tar.xz /

LABEL name="CentOS 7 basic image" build-date="2019-02-01" maintainer="somebody@example.com"

The resulting output from ```docker build -t centos7basic:0.1 .” is about 200MB:

fedora :: projects/docker/centos7 » docker build -t centos7basic:0.1 .
Sending build context to Docker daemon  43.52MB
Step 1/3 : FROM scratch
 --->
Step 2/3 : ADD centos-7-docker.tar.xz /
 ---> 38b7b9cd8813
Step 3/3 : LABEL name="CentOS 7 basic image" build-date="2019-02-01" maintainer="somebody@example.com"
 ---> Running in 31be1218ab5c
Removing intermediate container 31be1218ab5c
 ---> 043e2edf19e0
Successfully built 043e2edf19e0
Successfully tagged centos7basic:0.1

fedora :: projects/docker/centos7 » docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos7basic       0.1                 043e2edf19e0        9 seconds ago       204MB

To look at the contents of the tar.xz file use the syntax “tar tJvf centos-7-docker.tar.xz”

More to come once I write it, probably in a new post!