Building Docker image with Solr

There are two ways to build docker image:


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

Let’s take an example of creating a docker image with Solr from scratch:


Step1: Pull centos image which will act as 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

omi1 -d flag will run container in detach mode (as daemon thread).

-v flag mounts the volume.

-p flag is used to expose ports


Step3: Access docker container with exec command

docker exec -it solr1 bash

omi2 

Above command will open bash session for the solr1 container. Now we can change directory inside 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

omi3


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

omi4


Step3.4: Give execute permission to downloaded file & install jre

rpm –ivh $jre_downloaded

omi5


Step3.5: Download and unzip solr

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

omi6


Step3.6: Give execute permission to solr installation directory and change you directory to bin inside solr installation directory and run:

./solr start -p 8983

omi7


Step3.7: Test if the 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

omi8


Step5: Test your image by doing docker images

omi9 

Now you can save this image as a tar file. For that you will have to save this image as a tar file.

docker save –o <image name>

omi10 

To load image,

docker load -i <path to image tar file>

omi11


Building image with Dockerfile

Dockerfile is also referred to as docker build file. In Dockerfile we specify docker set of instructions. The instructions we give in Dockerfile are set of commands. An equivalent Dockerfile for building above image would be as,

omi12


  • The FROM instruction set the base image for subsequent instruction.
  • The MAINTAINER instruction allows you to set the author.
  • RUN command will execute the 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 to save your Dockerfile.

Write a comment
Cancel Reply
  • ken May 17, 2018, 8:55 am
    awesome post. but why would you include images with such poor resolution? Is the related code available anywhere?
    reply