I've created a microsservice (https://github.com/staticdev/enelvo-microservice) that needs to clone a git repository to create a docker image, with a single stage Dockerfile the final image has 759MB:
FROM python:3.7.6-slim-stretch
# set the working directory to /app
WORKDIR /app
# copy the current directory contents into the container at /app
COPY . /app
RUN apt-get update && apt-get install -y git \
&& pip install -r requirements.txt \
&& git clone https://github.com/tfcbertaglia/enelvo.git enelvo-src \
&& cd enelvo-src \
&& python setup.py install \
&& cd .. \
&& mv enelvo-src/enelvo enelvo \
&& rm -fr enelvo-src
EXPOSE 50051
# run app.py when the container launches
CMD ["python", "app.py"]
I've tried the approach of using a multistage build (https://blog.bitsrc.io/a-guide-to-docker-multi-stage-builds-206e8f31aeb8) to reduce the image size without git and apt-get lists (from update):
FROM python:3.7.6-slim-stretch as cloner
RUN apt-get update && apt-get install -y git \
&& git clone https://github.com/tfcbertaglia/enelvo.git enelvo-src
FROM python:3.7.6-slim-stretch
COPY --from=cloner /enelvo-src /app/enelvo-src
# set the working directory to /app
WORKDIR /app
# copy the current directory contents into the container at /app
COPY . /app
RUN pip install -r requirements.txt \
&& cd enelvo-src \
&& python setup.py install \
&& cd .. \
&& mv enelvo-src/enelvo enelvo \
&& rm -fr enelvo-src
EXPOSE 50051
# run app.py when the container launches
CMD ["python", "app.py"]
The problem is that, after doing that, the final size got even bigger (815MB). Any idea of what could be wrong in this case?