Building Docker image with Solr

There are two ways to build a Docker image:

  1. Running an image, modifying and committing it. This requires access to the live container.
  2. Using Dockerfile to build it.

Let's take an example of creating a Docker image with Solr from scratch.

Step1: Pull centos image which will act as the base image.

docker pull centos:latest

Step2: Run container, map ports and mount base machine directory to container

docker run -d --name solr1 -v /home/omit/project/:/home/omit/project/ -p 0.0.0.0:2224:22 -p 0.0.0.0:8983:8983 centos:latest

-d flag is used to run container in detach mode (as daemon thread)
-v flag is used to mount the volume
-p flag is used to expose ports

Step3:Access Docker container with exec command

docker exec -it solr1 bash

Above command will open a bash session for the Solr1 container. Now, we can change the directory inside the container to /home/project and install Solr.

So, for inside container, we need to install following things for Solr:

  • -wget for downloading
  • -install unzip utility
  • -jre required by Solr

Step3.1: Install wget

Yum install –y wget

Step3.2 Download and install unzip utility

wget http://mirror.centos.org/centos/7/os/x86_64/Packages/unzip-6.0-13.el7.x86_64.rpm
rpm –ivh $downloaded_file

Step3.3: Download jre

wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u51-b16/jre-8u51-linux-x64.rpm" -P /home/omit/project

Step3.4:Give 'Execute' permission to downloaded file and install jre

rpm –ivh $jre_downloaded

Step3.5:Download and unzip Solr

wget http://www.us.apache.org/dist/lucene/solr/5.4.0/solr-5.4.0.tgz

Step3.6: Give execute permission to Solr installation directory and change your directory to bin inside Solr installation directory and run:

./solr start -p 8983

Step3.7: Test if Solr is running locally,
http://localhost:8983

Step4:Exit container and commit the changes

docker commit -m "solr installed" -a "Omi Tewary" $container_ID solr/digital:1.0

Step5:Test your image by doing Docker images

Now you can save this image as a tar file.

docker save –o <image name>

To load image,

docker load -i <path to image tar file>

Building image with Dockerfile

Dockerfile is also referred to as Docker build file. In Dockerfile we specify the Docker set of instructions.

The instructions we give in Dockerfile are a set of commands. An equivalent Dockerfile for building above image would be as follows:

The FROM instruction set the base image for subsequent instruction.
The MAINTAINER instruction allows you to set the author.
RUN command will execute command on the bash of container.
WORKDIR specify the working directory of container.
VOLUME is used for mounting base machine directory to the container.
EXPOSE is used for exposing container ports.
Apart from this there are other instructions also available. Refer docker documentation for that.

After writing your docker file, execute the following command from the same directory where you have save your Dockerfile.

docker build -t <image_name>.