1

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 ?

halfer
  • 19,824
  • 17
  • 99
  • 186
Dhairya
  • 743
  • 2
  • 11
  • 29
  • [See my answer on a similar question](https://stackoverflow.com/questions/24394243/why-are-docker-container-images-so-large/39078793#39078793) – Munchkin Oct 17 '17 at 13:09

1 Answers1

4

Try the following Dockerfile, it gets the size of the image down to 255 MB. "--no-install-recommends" ensures that only the required packages are installed and "rm -rf /var/lib/apt/lists/*" cleans up including any index files.

FROM appcontainers/ubuntu:xenial

RUN apt-get update \
            && apt-get install -y --no-install-recommends software-properties-common \
            && add-apt-repository -y ppa:jonathonf/python-3.6 \
            && apt-get update \
            && apt-get install -y --no-install-recommends python3.6-minimal python3.6-venv \
            && apt-get install -y --no-install-recommends git \
            && apt-get install -y --no-install-recommends python-pip \
            && pip install --upgrade pip \
            && pip install gunicorn \
            && rm -rf /var/lib/apt/lists/* \
            && mkdir -p /home/EZMOVE

WORKDIR /home/workdir

EXPOSE 5000
dino
  • 181
  • 1
  • 4
  • Thanks #Dino, 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. – Dhairya Oct 17 '17 at 11:47