Docker image from existing directory tree, chroot or nspawn-container

There is a simple way of building a docker image from scratch using existing chroot or nspawn-container.

Here is what needs to be done:

1. Create a Dockerfile

It should add all the directories from the chroot/nspawn-container into the image. As a base for the Dockerfile use the scratch. Here are the contents of the Docker file:


FROM scratch
ADD bin /bin
ADD boot /boot
ADD cgroup /cgroup
ADD dev /dev
ADD etc /etc
ADD home /home
ADD icons /icons
ADD lib /lib
ADD lib64 /lib64
ADD media /media
ADD mnt /mnt
ADD PackageVersion.txt /PackageVersion.txt
ADD proc /proc
ADD root /root
ADD run /run
ADD sbin /sbin
ADD selinux /selinux
ADD srv /srv
ADD sys /sys
ADD tmp /tmp
ADD usr /usr
ADD var /var

2. Keep in mind to change any permissions if needed by your image, since all the directories added as above will be owned by root.

For example:


RUN chown -R mysql:mysql /var/run/mysql 

3. Build the image


cd /my/chroot/dir
sudo docker build -t my_chroot_image ./   

And YES, its that simple!

Categories