Skip to content

Commit

Permalink
remove pipenv, add multi-stage build, and upgrade to python 3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhea0 committed Oct 16, 2019
1 parent 07cf292 commit cf5fbd0
Show file tree
Hide file tree
Showing 16 changed files with 120 additions and 137 deletions.
10 changes: 10 additions & 0 deletions .env.dev-sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
DEBUG=1
SECRET_KEY=foo
DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]
SQL_ENGINE=django.db.backends.postgresql
SQL_DATABASE=hello_django_dev
SQL_USER=hello_django
SQL_PASSWORD=hello_django
SQL_HOST=db
SQL_PORT=5432
DATABASE=postgres
1 change: 1 addition & 0 deletions .env-sample → .env.prod-sample
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
DEBUG=0
SECRET_KEY=change_me
DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]
SQL_ENGINE=django.db.backends.postgresql
SQL_DATABASE=hello_django_prod
SQL_USER=hello_django
Expand Down
File renamed without changes.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.pyc
__pycache
.DS_Store
.env
.env.db
.env.dev
.env.prod
.env.prod.db
14 changes: 5 additions & 9 deletions app/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# pull official base image
FROM python:3.7.4-alpine
FROM python:3.8.0-alpine

# set work directory
WORKDIR /usr/src/app
Expand All @@ -8,18 +8,14 @@ WORKDIR /usr/src/app
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install psycopg2
# install psycopg2 dependencies
RUN apk update \
&& apk add --virtual build-deps gcc python3-dev musl-dev \
&& apk add postgresql-dev \
&& pip install psycopg2 \
&& apk del build-deps
&& apk add postgresql-dev gcc python3-dev musl-dev

# install dependencies
RUN pip install --upgrade pip
RUN pip install pipenv
COPY ./Pipfile /usr/src/app/Pipfile
RUN pipenv install --skip-lock --system --dev
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt

# copy entrypoint.sh
COPY ./entrypoint.sh /usr/src/app/entrypoint.sh
Expand Down
63 changes: 50 additions & 13 deletions app/Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
###########
# BUILDER #
###########

# pull official base image
FROM python:3.7.4-alpine
FROM python:3.8.0-alpine as builder

# set work directory
WORKDIR /usr/src/app
Expand All @@ -8,24 +12,57 @@ WORKDIR /usr/src/app
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install psycopg2
# install psycopg2 dependencies
RUN apk update \
&& apk add --virtual build-deps gcc python3-dev musl-dev \
&& apk add postgresql-dev \
&& pip install psycopg2 \
&& apk del build-deps
&& apk add postgresql-dev gcc python3-dev musl-dev

# install dependencies
# lint
RUN pip install --upgrade pip
RUN pip install pipenv
COPY ./Pipfile /usr/src/app/Pipfile
RUN pipenv install --skip-lock --system
RUN pip install flake8
COPY . /usr/src/app/
RUN flake8 --ignore=E501,F401 .

# install dependencies
COPY ./requirements.txt .
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt


#########
# FINAL #
#########

# pull official base image
FROM python:3.8.0-alpine

# create directory for the app user
RUN mkdir -p /home/app

# create the app user
RUN addgroup -S app && adduser -S app -G app

# create the home directory
ENV HOME=/home/app
ENV APP_HOME=/home/app/web
RUN mkdir $APP_HOME
WORKDIR $APP_HOME

# install dependencies
RUN apk update && apk add libpq
COPY --from=builder /usr/src/app/wheels /wheels
COPY --from=builder /usr/src/app/requirements.txt .
RUN pip install --no-cache /wheels/*

# copy entrypoint-prod.sh
COPY ./entrypoint.prod.sh /usr/src/app/entrypoint.prod.sh
COPY ./entrypoint.prod.sh $APP_HOME

# copy project
COPY . /usr/src/app/
COPY . $APP_HOME

# chown all the files to the app user
RUN chown -R app:app $APP_HOME

# change to the app user
USER app

# run entrypoint.prod.sh
ENTRYPOINT ["/usr/src/app/entrypoint.prod.sh"]
ENTRYPOINT ["/home/app/web/entrypoint.prod.sh"]
20 changes: 0 additions & 20 deletions app/Pipfile

This file was deleted.

42 changes: 0 additions & 42 deletions app/Pipfile.lock

This file was deleted.

48 changes: 25 additions & 23 deletions app/hello_django/settings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Django settings for hello_django project.
Generated by 'django-admin startproject' using Django 2.2.4.
Generated by 'django-admin startproject' using Django 2.2.6.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/topics/settings/
Expand All @@ -19,24 +19,26 @@
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/

SECRET_KEY = os.environ.get('SECRET_KEY')
SECRET_KEY = os.environ.get("SECRET_KEY")

DEBUG = int(os.environ.get('DEBUG', default=0))
DEBUG = int(os.environ.get("DEBUG", default=0))

ALLOWED_HOSTS = ['localhost', '127.0.0.1']
# 'DJANGO_ALLOWED_HOSTS' should be a single string of hosts with a space between each.
# For example: 'DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]'
ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS").split(" ")


# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'upload',
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",

"upload",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -74,13 +76,13 @@
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': os.environ.get('SQL_ENGINE', 'django.db.backends.sqlite3'),
'NAME': os.environ.get('SQL_DATABASE', os.path.join(BASE_DIR, 'db.sqlite3')),
'USER': os.environ.get('SQL_USER', 'user'),
'PASSWORD': os.environ.get('SQL_PASSWORD', 'password'),
'HOST': os.environ.get('SQL_HOST', 'localhost'),
'PORT': os.environ.get('SQL_PORT', '5432'),
"default": {
"ENGINE": os.environ.get("SQL_ENGINE", "django.db.backends.sqlite3"),
"NAME": os.environ.get("SQL_DATABASE", os.path.join(BASE_DIR, "db.sqlite3")),
"USER": os.environ.get("SQL_USER", "user"),
"PASSWORD": os.environ.get("SQL_PASSWORD", "password"),
"HOST": os.environ.get("SQL_HOST", "localhost"),
"PORT": os.environ.get("SQL_PORT", "5432"),
}
}

Expand Down Expand Up @@ -121,8 +123,8 @@
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/

STATIC_URL = '/staticfiles/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = "/staticfiles/"
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")

MEDIA_URL = '/mediafiles/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'mediafiles')
MEDIA_URL = "/mediafiles/"
MEDIA_ROOT = os.path.join(BASE_DIR, "mediafiles")
4 changes: 2 additions & 2 deletions app/hello_django/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from upload.views import image_upload

urlpatterns = [
path('', image_upload, name='upload'),
path('admin/', admin.site.urls),
path("", image_upload, name="upload"),
path("admin/", admin.site.urls),
]

if bool(settings.DEBUG):
Expand Down
3 changes: 3 additions & 0 deletions app/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Django==2.2.6
gunicorn==19.9.0
psycopg2-binary==2.8.3
11 changes: 6 additions & 5 deletions app/upload/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@


def image_upload(request):
if request.method == 'POST' and request.FILES['image_file']:
image_file = request.FILES['image_file']
if request.method == "POST" and request.FILES["image_file"]:
image_file = request.FILES["image_file"]
fs = FileSystemStorage()
filename = fs.save(image_file.name, image_file)
image_url = fs.url(filename)
return render(request, 'upload.html', {
'image_url': image_url
print(image_url)
return render(request, "upload.html", {
"image_url": image_url
})
return render(request, 'upload.html')
return render(request, "upload.html")
16 changes: 9 additions & 7 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@ services:
dockerfile: Dockerfile.prod
command: gunicorn hello_django.wsgi:application --bind 0.0.0.0:8000
volumes:
- static_volume:/usr/src/app/staticfiles
- media_volume:/usr/src/app/mediafiles
- static_volume:/home/app/web/staticfiles
- media_volume:/home/app/web/mediafiles
expose:
- 8000
env_file: .env
env_file:
- ./.env.prod
depends_on:
- db
db:
image: postgres:11.5-alpine
image: postgres:12.0-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
env_file: .env.db
env_file:
- ./.env.prod.db
nginx:
build: ./nginx
volumes:
- static_volume:/usr/src/app/staticfiles
- media_volume:/usr/src/app/mediafiles
- static_volume:/home/app/web/staticfiles
- media_volume:/home/app/web/mediafiles
ports:
- 1337:80
depends_on:
Expand Down
14 changes: 3 additions & 11 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,12 @@ services:
- ./app/:/usr/src/app/
ports:
- 8000:8000
environment:
- DEBUG=1
- SECRET_KEY=foo
- SQL_ENGINE=django.db.backends.postgresql
- SQL_DATABASE=hello_django_dev
- SQL_USER=hello_django
- SQL_PASSWORD=hello_django
- SQL_HOST=db
- SQL_PORT=5432
- DATABASE=postgres
env_file:
- ./.env.dev
depends_on:
- db
db:
image: postgres:11.5-alpine
image: postgres:12.0-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
Expand Down
2 changes: 1 addition & 1 deletion nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM nginx:1.17.2-alpine
FROM nginx:1.17.4-alpine

RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d
4 changes: 2 additions & 2 deletions nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ server {
}

location /staticfiles/ {
alias /usr/src/app/staticfiles/;
alias /home/app/web/staticfiles/;
}

location /mediafiles/ {
alias /usr/src/app/mediafiles/;
alias /home/app/web/mediafiles/;
}

}

0 comments on commit cf5fbd0

Please sign in to comment.