Creating a Docker Image

I delved into learning about Docker and (later on) Kubernetes since I was using them to scale AARU Robot- my startup project which is an AI based conversational engine. In this article I’ll share my learnings about creating a Docker Image through AARU Robot’s Image creation.

So I had to create two images: Workload and Web application.

Step 1: Creating a jar file for Workload and Web App

A jar file contains all the class files (and in my case dependencies too) to run the programs.

Script for building Workload jar file:

cd $TSI_HOME/tsi_workload
git pull
mvn install
mvn package

Script for Web App jar file:

cd $TSI_HOME/tsi_web_app
git pull
mvn install
mvn package

Step 2: Creating Dockerfile

Docker can automatically build images using a Dockerfile. A Dockerfile is a core file which contains instructions to build a Docker Image.

Here, is the Dockerfile for creating an image of the Workload:

FROM openjdk:8-jdk-alpine
ENV DB_SERVER=localhost
ENV DB_SERVER_PORT=23017
RUN mkdir /opt/tsi-workload
COPY tsi-workload-1.0.0.jar /opt/tsi-workload/tsi-workload-1.0.0.jar
WORKDIR /opt/tsi-workload
CMD ["java", "-jar", "-Dserver.port=5000", "./tsi-workload-1.0.0.jar"]

Here, is the Dockerfile for creating an image of the Web App:

FROM openjdk:8-jdk-alpine
ENV DB_SERVER=localhost
ENV DB_SERVER_PORT=23017
RUN mkdir /opt/tsi-web-app
COPY tsi-web-app-1.0.0.jar /opt/tsi-web-app/tsi-web-app-1.0.0.jar
WORKDIR /opt/tsi-web-app
CMD ["java", "-jar", "-Dserver.port=3000", "./tsi-web-app-1.0.0.jar"]

So let’s understand all the Docker Commands used here:

Before diving in, note that a “container” is an instance of an image. From docker.com, a container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings. Container images become containers at runtime and in the case of Docker containers – images become containers when they run on Docker Engine.

  1. FROM: A valid Dockerfile must start with a FROM instruction. It specifies the underlying OS architecture that will be used when Docker builds the image. You have to use some form of a base image (like Ubuntu, CentOS, etc) to get started with building an image. I’ve used ALPINE which is a minimal linux image with a size of only 5MB.
  2. ENV: ENV is used to declare the environment variables in a Dockerfile.
  3. RUN: RUN to use to execute any shell command (in the example above, we’ve made a directory)
  4. COPY: Pretty self explanatory. In the example above, we’ve copied the jar file into the new directory that we created
  5. WORKDIR: The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile.
  6. CMD: There can be only one CMD command in a Dockerfile. If there are more than one CMD command then the last CMD command will be executed. From the Docker documentation, the main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as well. If CMD is used to provide default arguments for the ENTRYPOINT instruction, both the CMD and ENTRYPOINT instructions should be specified with the JSON array format.

Step 3: Writing the Dockerfile build script

Using the docker build command, I’ve built the tsi-workload and tsi-web-app image.

sudo docker build . -t tsi-workload
sudo docker build . -t tsi-web-app

Step 4: Checking if Images are created

To check if the image is created, run the following command in terminal

sudo docker images

********************

Leave a Reply

PHP JS HTML CSS BASH PYTHON CODE

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close