It’s quite often that we all face a lot of challenges when getting on to the newer assignments and engagements while we work on any business streams. The challenges may be in r...
By: Omi Tewary | February 8, 2016
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
-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
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
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 & install jre
rpm –ivh $jre_downloaded
Step3.5: Download and unzip solr
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
Step3.7: Test if the solr is running locally,
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. For that you will have to 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 docker set of instructions.
The instructions we give in Dockerfile are set of commands. An equivalent Dockerfile for building above image would be as,
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.