Create Docker Image: CentOS

You can create custom Docker image to provide exactly functionality that you need. Later you will can to ship this images or use it with DOCERFILEs.

In this example I will show you how to create yours custom CentOS base image. When I says base I mean image with bash, yum and rpm (with all dependencies), but you can build are much smaller image (without that stuff).

Overview

All operations that described on this post I will make with CentOS 6.8 and Docker 1.7.1.

The image creation can been separate to few parts: preparation and image build.

Preparation

At first lest create temporary directory. This is will be your installation root directory.

mkdir /tmp/centos6  
export image_root=/tmp/centos6  

Now we need initialize that root directory.

rpm --root ${image_root} --initdb  

The next step is install some basic CentOS packages.

alias INSTALL="rpm --root ${image_root} -ivh"  
INSTALL http://mirror.centos.org/centos/6.8/os/x86_64/Packages/setup-2.8.14-20.el6_4.1.noarch.rpm \ http://mirror.centos.org/centos/6.8/os/x86_64/Packages/filesystem-2.4.30-3.el6.x86_64.rpm \ http://mirror.centos.org/centos/6.8/os/x86_64/Packages/basesystem-10.0-4.el6.noarch.rpm \ http://mirror.centos.org/centos/6.8/os/x86_64/Packages/centos-release-6-8.el6.centos.12.3.x86_64.rpm  

I create this image for 'general purpose' so now I install yum packet manager and net-tools. But you can leave this step or install what you want (e.g. nginx).

[ ! ] Note: that if you does not install yum or rpm you will not to able provide additional packages with DOCKERFILE.

yum --installroot ${image_root} install yum net-tools  

Image creation

Image creation is pretty simple just run.

tar -C ${image_root}/ -c . | docker import - centos6:latest  

Where centos6:letest is image name and image tag.

Test

You can define yours new image with fallow command.

docker images  
docker inspect  

Also you can log into your image. Remember that if you does not install bash then you will not to able to log into.

docker run --rm -it -v ~/.bash_history:/root/.bash_history centos6:latest /bin/bash  

As you can see I also provide (map) my local .bash_history file with the command above.

Publish image

You can share yours image with Docker Hub or with local Docker registry.

docker tag %IMAGE_ID% %IMG_ANNOTATE%  
docker login --username=yourhubusername --email=youremail@company.com  
docker push %IMG_ANNOTATE%  

You can find image_id with docker image command.

Docker builder script

For my every day need I use docker-builder script.

docker-builder.sh --build_name=centos6 --packages="yum nginx" --tag=latest