Create docker image from scratch

Create docker image from scratch

What is this about


Docker is a Platform-as-a-Service (PaaS) solution to run all kinds of applications in isolated containers.
More information is available at

Docker images

Docker stores application data in so called images. These images provide the full runtime environment to execute the applications in containers.

Why create docker image from scratch

The downside of the officially available Docker Hub images is, that you do not know what is in the images and there is no guarantee that these images are safe.

It is not difficult to create an image from scratch.

Preconditions to start

  1. Running Linux environment
  2. Working dockerd, containerd and docker client tools
  3. BusyBox installed []

Check docker installation

Enter docker info at the command prompt to check if your docker installation is working.

Create the smallest bootstrap

To run docker build on the later Dockerfile you will need a very small bootstrap installation to continue with your image installation.

One possibility is to use BusyBox.

In my environment I created a chroot directory with these commands...

mkdir -p chroot/bin ; cp -a /bin/busybox chroot/bin/
mkdir chroot/lib ; cp -a /lib/ /lib/ chroot/lib/
cd chroot/bin
ln -s busybox sh
ln -s busybox mkdir
ln -s busybox tar
cd ../..

BusyBox needs some libraries. You can lookup the libraries with command ldd $(which busybox).

The commands I will need for installation in the Dockerfile are provided by BusyBox and so I create the needed symbolic links to sh, mkdir and tar.

Another tool I will use during image creation is wget.

mkdir -p chroot/usr/bin ; cp -a /usr/bin/wget chroot/usr/bin/
cd chroot/usr/bin
ln -s ../../bin/busybox find
cd ../../..

And a symbolic link to busybox for find. This is useful for debugging purposes.

Dockerfile image build instructions

The Dockerfile starts with the special

FROM scratch

and next COPY chroot /.

In the first RUN command you have to add the chroot library directories to LD_LIBRARY_PATH and the executable directories to PATH environment variables.

RUN export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/chroot/lib" ; \
    export PATH="$PATH:/chroot/bin:/chroot/usr/bin" ; \

In the next step you can download tar-files with wget and mkdir a directory for extraction.

    wget "$release_url" && \
    mkdir extract && \
    tar xzf "$release_tarball" -C extract ; \

Now you can modify the installation in extract and do whatever is needed.
Create a new tar with the modifications and afterwards extract it to the root-/ of the build-container.

Delete the tarballs and the extract-directories and do all the cleanup you need to do.

Docker references


Run docker build

Start the docker build with the command like

docker build -t "whatever-$release_arch-$release_version:`date +%Y%m%d-%H%M%S`" .

It is a good idea to provide a tag and versioned subtag for the image.

If all commands are successful your image is finished.
Lookup the installed images with docker image ls.

More information to commands at Docker reference.

Reuse image

Now you can reuse this image in new Dockerfile instructions at From ....

Push image to repositories

You can publish your new container image.
Use the command docker push for this.
Just one step before, you need to specify the repository tag for the image, which is done with docker tag image-tag:subtag [repository/]username/repository-tag:subtag.
Maybe you need to docker login repository.
Now you can docker push [repository/]username/repository-tag:subtag.

Do you need help?

If you would like to use own high quality images, please contact me and request an offer for your requirements.