Skip to content

Commit 638a3b1

Browse files
committed
add production docker files, update readme, bump dependencies
1 parent acfd55d commit 638a3b1

18 files changed

+141
-49
lines changed

.env-sample

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
DEBUG=0
2+
SECRET_KEY=change_me
3+
SQL_ENGINE=django.db.backends.postgresql
4+
SQL_DATABASE=hello_django_prod
5+
SQL_USER=hello_django
6+
SQL_PASSWORD=hello_django
7+
SQL_HOST=db
8+
SQL_PORT=5432
9+
DATABASE=postgres

.env.db-sample

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
POSTGRES_USER=hello_django
2+
POSTGRES_PASSWORD=hello_django
3+
POSTGRES_DB=hello_django_prod

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
*.pyc
22
__pycache
33
.DS_Store
4+
.env
5+
.env.db

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2018 Michael Herman
3+
Copyright (c) 2019 Michael Herman
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,28 @@ Check out the [post](https://testdriven.io/dockerizing-django-with-postgres-guni
66

77
## Want to use this project?
88

9-
1. Fork/Clone
9+
### Development
1010

11-
1. Build the images and run the containers:
11+
Uses the default Django development server.
1212

13-
- development (django default server):
13+
1. Update the environment variables in the *docker-compose.yml* file.
14+
1. Build the images and run the containers:
1415

1516
```sh
1617
$ docker-compose up -d --build
1718
```
18-
Test it out at [http://localhost:8000](http://localhost:8000)
1919

20-
App's folder is mounted into container and your changes apply automatically.
20+
Test it out at [http://localhost:8000](http://localhost:8000). The "app" folder is mounted into the container and your code changes apply automatically.
21+
22+
### Production
2123

22-
- production (gunicorn + nginx):
24+
Uses gunicorn + nginx.
25+
26+
1. Rename *.env-sample* to *.env* and *.env.db-sample* to *.env.db*. Update the environment variables.
27+
1. Build the images and run the containers:
2328

2429
```sh
25-
$ docker-compose -f docker-compose.prod.yml up --build -d
30+
$ docker-compose -f docker-compose.prod.yml up -d --build
2631
```
27-
No mounted folders -- to apply changes container should be rebuilt
2832

29-
Test it out at [http://localhost:1337](http://localhost:1337)
33+
Test it out at [http://localhost:1337](http://localhost:1337). No mounted folders. To apply changes, the image must be re-built.

app/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# pull official base image
22
FROM python:3.7-alpine
33

4+
# set work directory
5+
WORKDIR /usr/src/app
6+
47
# set environment varibles
58
ENV PYTHONDONTWRITEBYTECODE 1
69
ENV PYTHONUNBUFFERED 1
710

8-
# set work directory
9-
WORKDIR /usr/src/app
10-
1111
# install psycopg2
1212
RUN apk update \
1313
&& apk add --virtual build-deps gcc python3-dev musl-dev \

app/Dockerfile.prod

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# pull official base image
2+
FROM python:3.7-alpine
3+
4+
# set work directory
5+
WORKDIR /usr/src/app
6+
7+
# set environment varibles
8+
ENV PYTHONDONTWRITEBYTECODE 1
9+
ENV PYTHONUNBUFFERED 1
10+
11+
# install psycopg2
12+
RUN apk update \
13+
&& apk add --virtual build-deps gcc python3-dev musl-dev \
14+
&& apk add postgresql-dev \
15+
&& pip install psycopg2 \
16+
&& apk del build-deps
17+
18+
# install dependencies
19+
RUN pip install --upgrade pip
20+
RUN pip install pipenv
21+
COPY ./Pipfile /usr/src/app/Pipfile
22+
RUN pipenv install --skip-lock --system
23+
24+
# copy entrypoint-prod.sh
25+
COPY ./entrypoint.prod.sh /usr/src/app/entrypoint.prod.sh
26+
27+
# copy project
28+
COPY . /usr/src/app/
29+
30+
# run entrypoint.prod.sh
31+
ENTRYPOINT ["/usr/src/app/entrypoint.prod.sh"]

app/Pipfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ name = "pypi"
77

88
[packages]
99

10-
django= "==2.1"
10+
django = "==2.2"
1111
gunicorn= "==19.9.0"
1212

1313

app/Pipfile.lock

Lines changed: 14 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/entrypoint.prod.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/sh
2+
3+
if [ "$DATABASE" = "postgres" ]
4+
then
5+
echo "Waiting for postgres..."
6+
7+
while ! nc -z $SQL_HOST $SQL_PORT; do
8+
sleep 0.1
9+
done
10+
11+
echo "PostgreSQL started"
12+
fi
13+
14+
exec "$@"

0 commit comments

Comments
 (0)