Small docker container with numpy Pillow Pyhon3 for flask micro-service appliaction
Author: Rafal MarguzewiczPublished:
Categories: DevOps
Tags: docker • numpy
In fist stage we are build docker container needed to compile numpy, Pillow and many other packages to wheel files. That container have ~500mb
FROM python:3.6.10-alpine3.11
RUN apk --no-cache --update-cache add \
git less openssh gcc gfortran python python-dev build-base wget freetype-dev libpng-dev openblas-dev \
libjpeg \
# accelerated baseline JPEG compression and decompression library
libjpeg-turbo-dev && \
rm -rf /var/lib/apt/lists/*
RUN ln -s /usr/include/locale.h /usr/include/xlocale.h
If we have a lot of micro-services we can build this container separately to improve build performance other containers.
docker build -t numpy_pillow_base -f Dockerfile .
In finish stage we are building container for flask application with numpy, pillow pip packages.
FROM numpy_pillow_base:latest as builder
ADD o_handler/requirements.txt requirements.txt
ARG SSH_PRIVATE_KEY
RUN mkdir -p /root/.ssh/ && echo "${SSH_PRIVATE_KEY}" > /root/.ssh/id_rsa && \
chmod -R 600 /root/.ssh/ && \
ssh-keyscan -t rsa gitlab.com > ~/.ssh/known_hosts
RUN python3 -m pip wheel -r requirements.txt --wheel-dir wheelhouse/
FROM python:3.6.10-alpine3.10
ADD o_handler o_handler
COPY --from=builder /wheelhouse /wheelhouse
# for function pillow
RUN apk update && apk add jpeg-dev zlib-dev
RUN python3 -m pip install /wheelhouse/*
python3 -m pip install o_application_with setup/
ENV BIND_ADDR 0.0.0.0
ENV BIND_PORT 8000
EXPOSE $BIND_PORT
USER worker
CMD ["run gunicorn or flask"]
Build
docker build -t app_name -f Dockerfile2.1 --build-arg SSH_PRIVATE_KEY="$(cat ~/.ssh/id_rsa)"
Now, our coinainter have ~170mb with numpy, pillow, gunicorn …