Why Docker container images become so large while deplying python3.6, Virtualenv, Flask, Gunicorn on UBUNTU 16.04

Hi all,
I am new to docker and containerization. So I am trying to create minimal size docker container with a easy application deploying and rollback features. I have some queries related to ubuntu 16.04 docker container:

While deploying the flask application within virtualenv python3 -m venv FLSK-ENV

Dockerfile:

    `FROM appcontainers/ubuntu:xenial
    
    MAINTAINER user <user>
    
    RUN apt-get install -y software-properties-common \
            && add-apt-repository ppa:jonathonf/python-3.6 -y \
            && apt-get update -y \
            && apt-get install -y python3.6-minimal python3.6-venv \
            && apt-get install -y git \
            && apt-get install python-pip -y \
            && pip install --upgrade pip \
        	&& pip install gunicorn \
            && mkdir -p /home/EZMOVE 
    
    WORKDIR /home/workdir
    
    RUN git clone -b develop --single-branch http://repo

    RUN ["chmod", "+x", "./prepareenv.sh"]

    RUN /bin/bash -c "source prepareenv.sh"

    EXPOSE 5000`

So above dockerfile will pull the minimal ubuntu-16.04 from “appcontainers/ubuntu:xenial” and will update and install only required packages like:

 1. python3.6
 2. git
 3. python-pip
 4. gunicorn

Then it will execute the ‘prepareenv.sh’ to create the python3.6 virtual environment, activate venv and then install requirements.txt using pip, then it will expose the port and gunicorn will serv the Flask application…etc.

But while creating the image the docker image size increase from 70MB to 550MB.

while installing the packages it is installing the other packages like as follows:

  `The following additional packages will be installed:
  binutils build-essential bzip2 cpp cpp-5 dpkg-dev fakeroot g++ g++-5 gcc
  gcc-5 gcc-5-base libalgorithm-diff-perl libalgorithm-diff-xs-perl
  libalgorithm-merge-perl libasan2 libatomic1 libc-dev-bin libc6 libc6-dev
  libcc1-0 libcilkrts5 libdpkg-perl libexpat1-dev libfakeroot
  libfile-fcntllock-perl libgcc-5-dev libgomp1 libisl15 libitm1 liblsan0
  libmpc3 libmpfr4 libmpx0 libpython-all-dev libpython-dev libpython-stdlib
  libpython2.7 libpython2.7-dev libpython2.7-minimal libpython2.7-stdlib
  libquadmath0 libstdc++-5-dev libstdc++6 libtsan0 libubsan0 linux-libc-dev
  make manpages manpages-dev python python-all python-all-dev python-dev
  python-minimal python-pkg-resources python-setuptools python-wheel python2.7
  python2.7-dev python2.7-minimal
Suggested packages:
  binutils-doc bzip2-doc cpp-doc gcc-5-locales debian-keyring g++-multilib
  g++-5-multilib gcc-5-doc libstdc++6-5-dbg gcc-multilib autoconf automake
  libtool flex bison gdb gcc-doc gcc-5-multilib libgcc1-dbg libgomp1-dbg
  libitm1-dbg libatomic1-dbg libasan2-dbg liblsan0-dbg libtsan0-dbg
  libubsan0-dbg libcilkrts5-dbg libmpx0-dbg libquadmath0-dbg glibc-doc locales
  libstdc++-5-doc make-doc man-browser python-doc python-tk
  python-setuptools-doc python2.7-doc binfmt-support
The following NEW packages will be installed:
  binutils build-essential bzip2 cpp cpp-5 dpkg-dev fakeroot g++ g++-5 gcc
  gcc-5 libalgorithm-diff-perl libalgorithm-diff-xs-perl
  libalgorithm-merge-perl libasan2 libatomic1 libc-dev-bin libc6-dev libcc1-0
  libcilkrts5 libdpkg-perl libexpat1-dev libfakeroot libfile-fcntllock-perl
  libgcc-5-dev libgomp1 libisl15 libitm1 liblsan0 libmpc3 libmpfr4 libmpx0
  libpython-all-dev libpython-dev libpython-stdlib libpython2.7
  libpython2.7-dev libpython2.7-minimal libpython2.7-stdlib libquadmath0
  libstdc++-5-dev libtsan0 libubsan0 linux-libc-dev make manpages manpages-dev
  python python-all python-all-dev python-dev python-minimal python-pip
  python-pkg-resources python-setuptools python-wheel python2.7 python2.7-dev
  python2.7-minimal
The following packages will be upgraded:
  gcc-5-base libc6 libstdc++6
3 upgraded, 59 newly installed, 0 to remove and 24 not upgraded.
Need to get 76.6 MB of archives.
After this operation, 210 MB of additional disk space will be used.`  

So How to Reduce The Docker Image Size ?

Hi all,

I added the extra argument in ‘apt-get install -y –no-install-recommends’, so some how it reduced the size of Ubunut 16.04 docker container from 550MB to 210MB.
so I am planning to host flask application using Gunicorn so, for this How can I optimised the Docker container like:

  1. Will add only binary of flask app into the container, so for this I need not to install python-dev like packages.

  2. While creating the docker container many layers are geting created so how can I merge all previous layer into single layer.

  3. Also why ‘systemd’ service are not working in docker container, what will be the alternative for creating daemonised service in docker container.

try to install python packages without cache: pip --no-cache-dir install gunicorn.
These caches will stay in your image if you do not explicitly delete them.

Layers could be squashed (merged together) with docker build --squash.

Systemd: try to design your containers that there is always just one application running.