Optimize docker image for Sunbird LMS service

Overview

The current size of docker image of sunbird-lms service is approximately 1.16-1.17GB, But the compressed JAR of learnin-service having max size 250MB approx .  We have to find the approach to optimize the docker image .This can be done by removing unwanted files and maven dependencies  from learning-service repository and pom.xml respectively, but it will not have a big impact as of now files included into jar which are not java files weight below 1 MB and maven dependencies are added after only code review. So Better approach will be to explore docker image and its build process.


Approach 1:

Changing version of the dependency used in project to a same version number and removing unwanted or already add dependencies which are being inherited from another module.
After Implementing this approach no significant difference in the JAR size, but it is good practice to use same version of a dependency all over the project.


Approach 2:

This approach is to explore and understand how docker images are created and how we are creating the docker image of LMS .

The build steps and output used to create docker image on server are below :


we use the script "Build.sh" for creating the docker image, content of script are as follows:

Build.sh

#!/bin/sh
# Build script
# set -o errexit
e () {
    echo $( echo ${1} | jq ".${2}" | sed 's/\"//g')
}
m=$(./metadata.sh)

org=$(e "${m}" "org")
name=$(e "${m}" "name")
version=$(e "${m}" "version")
# docker build -f "./learner-actors/actors/Dockerfile.test" -t sunbird/actor-service:0.0.1-build .
# docker run --name actor-service-0.0.1-build sunbird/actor-service:0.0.1-build
# containerid=`docker ps -q -a -f name=actor-service-0.0.1-build`
# docker cp $containerid:/opt/learner-actors/actors/learner-actor/target/learner-actor-1.0-SNAPSHOT.jar ./learner-actors/actors/learner-actor-1.0-SNAPSHOT.jar
#docker rm $containerid
docker build -f ./Dockerfile -t ${org}/${name}:${version}-bronze .


The content of Dockerfile which is used in Build.sh

FROM openjdk:8-jdk-alpine
MAINTAINER "Manojv" "manojv@ilimi.in"
RUN apk update \
    && apk add unzip \
    && apk add curl \
    && adduser -u 1001 -h /home/sunbird/ -D sunbird \
    && mkdir -p /home/sunbird/learner
COPY ./service/target/actor-service.jar /home/sunbird/learner/
RUN chown -R sunbird:sunbird /home/sunbird
EXPOSE 8088
USER sunbird
WORKDIR /home/sunbird/learner/
CMD ["java", "-cp", "actor-service.jar", "-Dactor_hostname=actor-service", "-Dbind_hostname=0.0.0.0", "org.sunbird.middleware.Application"]

After running the following command:

sudo docker build -f Dockerfile -t org/sunb:mayank-bronze1 .

output were as follows :

Sending build context to Docker daemon  331.9MB
Step 1/9 : FROM openjdk:8-jre-alpine
 ---> 556cf01c8299
Step 2/9 : MAINTAINER "Manojv" "manojv@ilimi.in"
 ---> Using cache
 ---> 4d6b65b40c4b
Step 3/9 : RUN apk update     && apk add  unzip     && apk add curl     && adduser -u 1001 -h /home/sunbird/ -D sunbird     && mkdir -p /home/sunbird/learner
 ---> Using cache
 ---> 6eb982c6b85a
Step 4/9 : COPY ./service/target/learning-service-1.0-SNAPSHOT-dist.zip /home/sunbird/learner/
 ---> 6a60144252a8
Step 5/9 : RUN unzip /home/sunbird/learner/learning-service-1.0-SNAPSHOT-dist.zip -d /home/sunbird/learner/
 ---> Running in 929164e0e671
Archive:  /home/sunbird/learner/learning-service-1.0-SNAPSHOT-dist.zip
   creating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/
   creating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/learning-service-1.0-SNAPSHOT.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.jclouds.jclouds-compute-2.1.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/net.jcip.jcip-annotations-1.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/com.sun.mail.javax.mail-1.6.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/com.microsoft.azure.azure-storage-5.4.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.jclouds.provider.digitalocean2-2.1.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.slf4j.slf4j-log4j12-1.7.10.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.htrace.htrace-core-3.1.0-incubating.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/com.google.protobuf.protobuf-java-2.5.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.sunbird.elasticsearch-util-1.0-SNAPSHOT.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.jboss.resteasy.resteasy-jaxrs-services-3.1.0.Final.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/com.sun.jersey.jersey-json-1.9.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.slf4j.slf4j-api-1.7.12.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/com.google.inject.guice-4.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.velocity.velocity-tools-2.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.struts.struts-tiles-1.3.8.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/joda-time.joda-time-2.8.1.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.jclouds.provider.profitbricks-2.1.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.lucene.lucene-spatial3d-6.5.1.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.jclouds.provider.aws-sqs-2.1.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.jclouds.provider.skalicloud-sdg-my-2.1.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.jclouds.provider.rackspace-cloudservers-us-2.1.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/io.netty.netty-3.10.4.Final.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/sslext.sslext-1.2-0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.jclouds.api.cloudstack-2.1.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/ch.qos.logback.logback-classic-1.1.3.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/com.typesafe.ssl-config-core_2.11-0.2.4.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.jclouds.driver.jclouds-okhttp-2.1.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/com.typesafe.akka.akka-protobuf_2.11-2.5.16.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.jclouds.provider.elastichosts-lon-p-2.1.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/com.jcraft.jzlib-1.1.3.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/com.typesafe.akka.akka-actor_2.11-2.5.16.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/com.typesafe.config-1.3.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.jclouds.api.openstack-trove-2.1.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.scala-lang.modules.scala-xml_2.11-1.0.1.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.jclouds.provider.packet-2.1.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.jclouds.driver.jclouds-sshj-2.1.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.hadoop.hadoop-auth-2.7.3.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/dom4j.dom4j-1.1.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.jclouds.api.sqs-2.1.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.jclouds.api.rackspace-cloudloadbalancers-2.1.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/com.typesafe.play.play-functional_2.11-2.4.6.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/com.fasterxml.jackson.jaxrs.jackson-jaxrs-base-2.8.6.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.scala-lang.modules.scala-java8-compat_2.11-0.7.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.lucene.lucene-suggest-6.5.1.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/com.lmax.disruptor-3.2.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/commons-net.commons-net-3.1.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.lucene.lucene-memory-6.5.1.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/com.typesafe.netty.netty-http-pipelining-1.1.4.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/ch.qos.logback.logback-core-1.1.3.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.jclouds.api.byon-2.1.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.hadoop.hadoop-azure-2.7.3.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.jclouds.provider.rackspace-clouddatabases-us-2.1.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.jclouds.api.docker-2.1.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.codehaus.jackson.jackson-jaxrs-1.8.3.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.slf4j.jul-to-slf4j-1.7.12.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/antlr.antlr-2.7.2.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.jclouds.api.elasticstack-2.1.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.jclouds.api.rackspace-cloudidentity-2.1.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.tukaani.xz-1.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/io.netty.netty-transport-4.1.11.Final.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/com.mashape.unirest.unirest-java-1.4.9.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/com.typesafe.play.play-exceptions-2.4.6.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/com.fasterxml.jackson.datatype.jackson-datatype-jdk8-2.5.4.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/com.google.inject.extensions.guice-multibindings-3.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/com.fasterxml.jackson.module.jackson-module-paranamer-2.7.8.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.logging.log4j.log4j-api-2.8.2.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.keycloak.keycloak-common-3.2.0.Final.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.glassfish.hk2.osgi-resource-locator-1.0.1.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.elasticsearch.plugin.reindex-client-5.4.3.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.httpcomponents.httpasyncclient-4.1.1.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.lucene.lucene-queryparser-6.5.1.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.jclouds.provider.rackspace-cloudblockstorage-uk-2.1.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.elasticsearch.client.rest-5.4.3.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/com.typesafe.akka.akka-stream_2.11-2.5.16.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.jclouds.provider.google-cloud-storage-2.1.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.mortbay.jetty.jetty-6.1.26.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/io.netty.netty-buffer-4.1.11.Final.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.jclouds.jclouds-all-2.1.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/javax.servlet.servlet-api-2.5.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/net.java.dev.jets3t.jets3t-0.9.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/javax.mail.javax.mail-api-1.5.1.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.xerial.snappy.snappy-java-1.0.4.1.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/com.typesafe.akka.akka-remote_2.11-2.5.16.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.jboss.spec.javax.ws.rs.jboss-jaxrs-api_2.0_spec-1.0.1.Beta1.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.elasticsearch.plugin.transport-netty3-client-5.4.3.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/com.fasterxml.jackson.core.jackson-databind-2.9.5.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.jclouds.api.atmos-2.1.0.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/org.apache.lucene.lucene-join-6.5.1.jar  
  inflating: /home/sunbird/learner/learning-service-1.0-SNAPSHOT/lib/com.fasterxml.jackson.datat