vincentcox / StaCoAn

StaCoAn is a crossplatform tool which aids developers, bugbounty hunters and ethical hackers performing static code analysis on mobile applications.
MIT License
827 stars 129 forks source link

Suggestions for container size improvements #70

Closed Kevin-De-Koninck closed 6 years ago

Kevin-De-Koninck commented 6 years ago

I did a rewrite of your old Docker container (https://github.com/vincentcox/StaCoAn/pull/47) to reduce the size from 1.44GB to 264MB. At that time, I did not know Docker well enough and now I think that I can further optimize the container's image size by combining RUN commands.

Maybe some packages can be installed virtual that are only needed during build steps (info). Don't know this for sure since I don't know the Dockerfile that well. (First look suggests pip3 is one of those packages)

I'll try this tonight, it will be good to see if there is a noticeable difference.

Kevin-De-Koninck commented 6 years ago

Before:

kevin@Kevins-MacBook-Pro:docker  [master]  -> docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
stacoan               latest              6a04f8627491        23 seconds ago      278MB
anapsix/alpine-java   latest              430fab324907        2 months ago        127MB

kevin@Kevins-MacBook-Pro:docker  [master]  -> docker history stacoan
IMAGE               CREATED              CREATED BY                                      SIZE                COMMENT
6a04f8627491        About a minute ago   /bin/sh -c #(nop)  ENTRYPOINT ["/bin/bash" "…   0B                  
fb33226e3041        About a minute ago   /bin/sh -c #(nop)  EXPOSE 8080                  0B                  
4fa88e94d86a        About a minute ago   /bin/sh -c #(nop)  EXPOSE 8000                  0B                  
0ea6a6e42984        About a minute ago   /bin/sh -c dos2unix /stacoan.sh                 410B                
ba27ed30f113        About a minute ago   /bin/sh -c #(nop) COPY file:f52baae16d9f2b54…   410B                
da3853ea2759        About a minute ago   /bin/sh -c pip3 install -r requirements.txt …   242kB               
dec2d64ea27c        About a minute ago   /bin/sh -c #(nop) WORKDIR /StaCoAn/src          0B                  
a57e7cbb7d9d        About a minute ago   /bin/sh -c git clone https://github.com/vinc…   77MB                
367c47802f4b        About a minute ago   /bin/sh -c apk add --no-cache git               14.5MB              
ccd079c3d2f7        About a minute ago   /bin/sh -c rm -r /root/.cache                   0B                  
14eb112a6efa        About a minute ago   /bin/sh -c if [[ ! -e /usr/bin/python ]]; th…   16B                 
e3ba4e9739df        About a minute ago   /bin/sh -c if [ ! -e /usr/bin/pip ]; then ln…   0B                  
c1ba63f369ac        2 minutes ago        /bin/sh -c pip3 install --upgrade pip setupt…   11.9MB              
8e83a26f0d89        2 minutes ago        /bin/sh -c rm -r /usr/lib/python*/ensurepip     0B                  
e7c1cbe3a522        2 minutes ago        /bin/sh -c python3 -m ensurepip                 0B                  
8691ba59b47b        2 minutes ago        /bin/sh -c apk add --no-cache python3 dos2un…   47.4MB              
430fab324907        2 months ago         /bin/sh -c set -ex &&     [[ ${JAVA_VERSION_…   123MB               
<missing>           2 months ago         /bin/sh -c #(nop)  ENV JAVA_VERSION_MAJOR=8 …   0B                  
<missing>           2 months ago         /bin/sh -c #(nop)  MAINTAINER Anastas Dancha…   0B                  
<missing>           5 months ago         /bin/sh -c #(nop)  CMD ["/bin/sh"]              0B                  
<missing>           5 months ago         /bin/sh -c #(nop) ADD file:093f0723fa46f6cdb…   4.15MB  

After combining all RUN commands into one:

kevin@Kevins-MacBook-Pro:docker  [develop] ≠1 -> docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
stacoan2              latest              81d4ac9f211f        3 seconds ago       207MB
stacoan               latest              6a04f8627491        26 minutes ago      278MB
anapsix/alpine-java   latest              430fab324907        2 months ago        127MB

kevin@Kevins-MacBook-Pro:docker  [develop] ≠1 -> docker history stacoan2
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
81d4ac9f211f        35 seconds ago      /bin/sh -c #(nop)  ENTRYPOINT ["/bin/bash" "…   0B                  
a6b6d2c38d8c        35 seconds ago      /bin/sh -c #(nop)  EXPOSE 8080                  0B                  
223a8be4e1a1        36 seconds ago      /bin/sh -c #(nop)  EXPOSE 8000                  0B                  
c09208d471d5        36 seconds ago      /bin/sh -c #(nop) COPY file:c89ccbb55cd84a5f…   445B                
8afd394d3639        36 seconds ago      /bin/sh -c apk add --update --no-cache pytho…   79.9MB              
430fab324907        2 months ago        /bin/sh -c set -ex &&     [[ ${JAVA_VERSION_…   123MB               
<missing>           2 months ago        /bin/sh -c #(nop)  ENV JAVA_VERSION_MAJOR=8 …   0B                  
<missing>           2 months ago        /bin/sh -c #(nop)  MAINTAINER Anastas Dancha…   0B                  
<missing>           5 months ago        /bin/sh -c #(nop)  CMD ["/bin/sh"]              0B                  
<missing>           5 months ago        /bin/sh -c #(nop) ADD file:093f0723fa46f6cdb…   4.15MB 

After housekeeping:

kevin@Kevins-MacBook-Pro:docker  [develop] ≠2 -> docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
stacoan_mini          latest              b861aaf31ffa        4 seconds ago       168MB
stacoan               latest              6a04f8627491        About an hour ago   278MB
anapsix/alpine-java   latest              430fab324907        2 months ago        127MB

kevin@Kevins-MacBook-Pro:docker  [develop] ≠2 -> docker history stacoan_mini
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
b861aaf31ffa        20 seconds ago      /bin/sh -c #(nop)  ENTRYPOINT ["/bin/bash" "…   0B                  
5033dc271485        8 minutes ago       /bin/sh -c #(nop)  EXPOSE 8080                  0B                  
cf95fac7f65a        8 minutes ago       /bin/sh -c #(nop)  EXPOSE 8000                  0B                  
339a4175feea        8 minutes ago       /bin/sh -c #(nop) COPY file:5dd7933a97ec60c9…   397B                
69a4d6b0634b        8 minutes ago       /bin/sh -c #(nop) WORKDIR /StaCoAn              0B                  
fbd3206d46e5        8 minutes ago       /bin/sh -c apk add --update --no-cache pytho…   40.3MB              
430fab324907        2 months ago        /bin/sh -c set -ex &&     [[ ${JAVA_VERSION_…   123MB               
<missing>           2 months ago        /bin/sh -c #(nop)  ENV JAVA_VERSION_MAJOR=8 …   0B                  
<missing>           2 months ago        /bin/sh -c #(nop)  MAINTAINER Anastas Dancha…   0B                  
<missing>           5 months ago        /bin/sh -c #(nop)  CMD ["/bin/sh"]              0B                  
<missing>           5 months ago        /bin/sh -c #(nop) ADD file:093f0723fa46f6cdb…   4.15MB     

So this minimizes the image again (278MB -> 168MB) which is a reduce of almost 40%. In the end we went from 1.44GB to 168MB which is a decrease of almost 90%. Not bad if you ask me :)

As you can see, the largest size is the JAVA layer. The StaCoAn Layer is about 40MB, but it can be reduced maybe, but I leave that part as an exercise for the reader.