From 11e335bb71c2a48d3a76bdc0a7fec0945c96c66d Mon Sep 17 00:00:00 2001 From: Douglass Clem Date: Mon, 9 Apr 2018 22:31:38 -0700 Subject: [PATCH 1/2] Make a smaller Docker image This shrinks the resulting docker image from 500+ mb to 151, by switching to the official python alpine image and using multi-stage builds. --- .dockerignore | 6 ++++++ Dockerfile | 28 +++++++++++++++++++++------- 2 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..36c0c9e --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +*.sqlite* +*.pyc +*local_settings.* +Dockerfile +README.md +LICENSE diff --git a/Dockerfile b/Dockerfile index 9534ab2..0aa34a6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,22 @@ -FROM ubuntu:latest -EXPOSE 8000 -RUN apt-get update && apt-get upgrade -y && apt-get install -y python3 python3-pip -COPY . /home/python +FROM python:3-alpine as builder + WORKDIR /home/python -RUN pip3 install -r requirements.txt -RUN python3 manage.py migrate -ENTRYPOINT python3 manage.py runserver 0.0.0.0:8000 + +RUN apk add --no-cache zlib-dev build-base python-dev jpeg-dev +RUN pip install virtualenv +RUN virtualenv venv + +ADD requirements.txt /home/python/ +RUN venv/bin/pip install --no-cache-dir -r requirements.txt +RUN virtualenv --relocatable venv/ + +ADD . /home/python/ +RUN venv/bin/python manage.py migrate + +FROM python:3-alpine + +WORKDIR /home/python +COPY --from=builder /home/python /home/python + +ENTRYPOINT ["venv/bin/python"] +CMD ["manage.py", "runserver", "0.0.0.0:8000"] From 5ce3aa476cc14eb0d1e5775b03da637e2bc9e03c Mon Sep 17 00:00:00 2001 From: Michael Hall Date: Sun, 19 Aug 2018 11:31:27 -0400 Subject: [PATCH 2/2] Include /lib and /usr/lib from builder for PIL to work. Use environ_settings to pass config info to Django --- Dockerfile | 3 +++ README.md | 2 +- get_together/views/__init__.py | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0aa34a6..ba2da75 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,6 +17,9 @@ FROM python:3-alpine WORKDIR /home/python COPY --from=builder /home/python /home/python +COPY --from=builder /lib /lib +COPY --from=builder /usr/lib /usr/lib ENTRYPOINT ["venv/bin/python"] +ENV DJANGO_SETTINGS_MODULE=get_together.environ_settings CMD ["manage.py", "runserver", "0.0.0.0:8000"] diff --git a/README.md b/README.md index f7fc949..f7eeb5d 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ file): ### Using the docker container ``` docker build -t get_together . -docker run -d --name get_together -p 8000:8000 get_together +docker run -e "DEBUG_MODE=True" -e "SECRET_KEY=xxxxx" -e "ALLOWED_HOSTS=localhost,127.0.0.1" -d --name get_together -p 8000:8000 get_together docker exec -it get_together python3 manage.py createsuperuser ``` diff --git a/get_together/views/__init__.py b/get_together/views/__init__.py index 1d5c972..9d724a3 100644 --- a/get_together/views/__init__.py +++ b/get_together/views/__init__.py @@ -60,7 +60,7 @@ def home(request, *args, **kwards): context['city_search'] = False try: g = location.get_geoip(request) - if g.latlng is not None and g.latlng[0] is not None and g.latlng[1] is not None: + if g.latlng is not None and len(g.latlng) >= 2 and g.latlng[0] is not None and g.latlng[1] is not None: ll = g.latlng context['geoip_lookup'] = True