emailservice Image Optimization (#51)

Reduce docker image for emailservice to ~240 MB (down from ~ 1.31 GB)

Main application (`email_server.py`) now runs as python 2.7. Before we had both Python 2.7 and Python 3 installed in the image.

Switched to using `python:2.7-alpine3.8` as the base image, and used multi-stage dockerfiles to keep dependencies minimal.

Fixes #49 

From my shell:
```
$ docker build -t emailservice:dev . && docker run -it emailservice:dev
Sending build context to Docker daemon  97.28kB
Step 1/17 : FROM python:2.7-alpine3.8 as base
 ---> b2bc7255b42c
Step 2/17 : FROM base as builder
 ---> b2bc7255b42c
Step 3/17 : RUN apk add --update --no-cache     gcc     linux-headers     make     musl-dev     python-dev     g++     cairo-dev     cairo     openssl-dev     gobject-introspection-dev
 ---> Using cache
 ---> 6daf3d9fe49a
Step 4/17 : ENV GRPC_PYTHON_VERSION 1.15.0
 ---> Using cache
 ---> 3e33d97d9580
Step 5/17 : RUN python -m pip install --upgrade pip
 ---> Using cache
 ---> e8fa3879c282
Step 6/17 : RUN pip install grpcio==${GRPC_PYTHON_VERSION} grpcio-tools==${GRPC_PYTHON_VERSION}
 ---> Using cache
 ---> c6fba7743eed
Step 7/17 : COPY requirements.txt .
 ---> Using cache
 ---> 1f6b0a444980
Step 8/17 : RUN pip install -r requirements.txt
 ---> Using cache
 ---> 8cc0a7af6aa8
Step 9/17 : FROM base as final
 ---> b2bc7255b42c
Step 10/17 : RUN GRPC_HEALTH_PROBE_VERSION=v0.1.0-alpha.1 &&     wget -qO/bin/grpc_health_probe https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/${GRPC_HEALTH_PROBE_VERSION}/grpc_health_probe-linux-amd64 &&     chmod +x /bin/grpc_health_probe
 ---> Using cache
 ---> e954a0384081
Step 11/17 : ENV PYTHONUNBUFFERED=0
 ---> Using cache
 ---> 64ece3d72a66
Step 12/17 : WORKDIR /email_server
 ---> Using cache
 ---> 27b34dc14492
Step 13/17 : COPY --from=builder /usr/local/lib/python2.7/ /usr/local/lib/python2.7/
 ---> Using cache
 ---> 60035ec8dfd4
Step 14/17 : RUN apk add --no-cache libstdc++
 ---> Using cache
 ---> 920be90c126e
Step 15/17 : COPY . .
 ---> Using cache
 ---> 9541bed2d7a0
Step 16/17 : EXPOSE 8080
 ---> Using cache
 ---> 48fbeaa852b9
Step 17/17 : ENTRYPOINT [ "python", "email_server.py" ]
 ---> Using cache
 ---> ff317770992d
Successfully built ff317770992d
Successfully tagged emailservice:dev
starting the email service in dummy mode.
listening on port: 8080
```
This commit is contained in:
Colin Nelson 2018-09-25 16:43:39 -07:00 committed by Ahmet Alp Balkan
parent 6460427bee
commit 2924250449
2 changed files with 57 additions and 29 deletions

View file

@ -1,27 +1,45 @@
FROM python:3-alpine as base
FROM base as builder
# Use the grpc.io provided Python image as the base image
FROM grpc/python:1.0
# gRPC and app deps
RUN apk add --update --no-cache \
gcc \
linux-headers \
make \
musl-dev \
python-dev \
g++ \
# App Deps
cairo-dev \
cairo \
openssl-dev \
gobject-introspection-dev
# download the grpc health probe
# get packages
COPY requirements.txt .
RUN pip install -r requirements.txt
FROM base as final
# Enable unbuffered logging
ENV PYTHONUNBUFFERED=1
# Download the grpc health probe
RUN GRPC_HEALTH_PROBE_VERSION=v0.1.0-alpha.1 && \
wget -qO/bin/grpc_health_probe https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/${GRPC_HEALTH_PROBE_VERSION}/grpc_health_probe-linux-amd64 && \
chmod +x /bin/grpc_health_probe
# show python logs as they occur
ENV PYTHONUNBUFFERED=0
# install pip for python3
RUN apt-get -qqy update && \
apt-get -qqy install python3-pip
# get packages
WORKDIR /email_server
COPY requirements.txt .
RUN pip3 install -r requirements.txt
# Grab packages from builder
COPY --from=builder /usr/local/lib/python3.7/ /usr/local/lib/python3.7/
# Need libstdc++ for grpc
RUN apk add --no-cache libstdc++
# Add the application
COPY . .
EXPOSE 8080
ENTRYPOINT [ "python3", "email_server.py" ]
ENTRYPOINT [ "python", "email_server.py" ]

View file

@ -1,31 +1,41 @@
asn1crypto==0.24.0
cachetools==2.1.0
certifi==2018.4.16
certifi==2018.8.24
cffi==1.11.5
chardet==3.0.4
cryptography==1.7.1
google-api-core==1.2.1
google-auth==1.5.0
configparser==3.5.0
cryptography==2.3.1
entrypoints==0.2.3
enum34==1.1.6
futures==3.1.1
google-api-core==1.4.0
google-auth==1.5.1
google-cloud-core==0.28.1
google-cloud-trace==0.19.0
googleapis-common-protos==1.5.3
grpc-google-iam-v1==0.11.4
grpcio==1.12.1
grpcio-health-checking==1.14.1
grpcio-health-checking==1.12.1
grpcio-tools==1.12.1
idna==2.7
ipaddress==1.0.22
jeepney==0.4
Jinja2==2.10
keyring==10.1
keyrings.alt==1.3
keyring==15.1.0
keyrings.alt==3.1
MarkupSafe==1.0
opencensus==0.1.5
protobuf==3.6.0
pyasn1==0.4.3
opencensus==0.1.7
protobuf==3.6.1
pyasn1==0.4.4
pyasn1-modules==0.2.2
pycairo==1.17.1
pycparser==2.19
pycrypto==2.6.1
pygobject==3.22.0
PyGObject==3.30.1
pytz==2018.5
pyxdg==0.25
pyxdg==0.26
requests==2.19.1
rsa==3.4.2
SecretStorage==2.3.1
rsa==4.0
SecretStorage==3.1.0
six==1.11.0
urllib3==1.23
urllib3==1.23