Merge branch 'feature/docker-rewrite' into develop

This commit is contained in:
vabene1111 2020-03-24 17:23:04 +01:00
commit 08848da4a3
17 changed files with 123 additions and 110 deletions

View File

@ -7,4 +7,12 @@ docker-compose*
.gitignore .gitignore
README.md README.md
LICENSE LICENSE
.vscode .vscode
.env
.env.template
.github
.idea
LICENSE.md
docs
nginx
update.sh

View File

@ -1,7 +1,3 @@
VIRTUAL_HOST=
LETSENCRYPT_HOST=
LETSENCRYPT_EMAIL=
DEBUG=1 DEBUG=1
ALLOWED_HOSTS=* ALLOWED_HOSTS=*
SECRET_KEY= SECRET_KEY=

View File

@ -21,6 +21,7 @@ jobs:
run: | run: |
python -m pip install --upgrade pip python -m pip install --upgrade pip
pip install -r requirements.txt pip install -r requirements.txt
python3 manage.py collectstatic --noinput
- name: Django Testing project - name: Django Testing project
run: | run: |
python3 manage.py test python3 manage.py test

2
.gitignore vendored
View File

@ -76,4 +76,4 @@ staticfiles/
postgresql/ postgresql/
/docker-compose.yml /docker-compose.override.yml

View File

@ -1,24 +1,23 @@
FROM python:3.8-slim-buster FROM python:3.8-alpine
RUN mkdir /Recipes
WORKDIR /Recipes
ADD . /Recipes/
RUN apt-get update
RUN apt-get -y upgrade
RUN apt-get install -y \
python3 \
python3-pip \
postgresql-client \
gettext
RUN pip3 install --upgrade pip
RUN pip3 install -r requirements.txt
RUN apt-get autoremove -y
RUN apk add --no-cache postgresql-libs gettext zlib libjpeg libxml2-dev libxslt-dev
ENV PYTHONUNBUFFERED 1 ENV PYTHONUNBUFFERED 1
EXPOSE 8080
EXPOSE 8080 # Don't run container as root
RUN adduser -D recipes
RUN mkdir /opt/recipes
RUN chown recipes:recipes /opt/recipes
WORKDIR /opt/recipes
COPY --chown=recipes:recipes . ./
RUN chmod +x boot.sh setup.sh
RUN ln -s /opt/recipes/setup.sh /usr/local/bin/createsuperuser
RUN apk add --no-cache --virtual .build-deps gcc musl-dev postgresql-dev zlib-dev jpeg-dev && \
python -m venv venv && \
venv/bin/pip install -r requirements.txt --no-cache-dir &&\
apk --purge del .build-deps
USER recipes
ENTRYPOINT ["/opt/recipes/boot.sh"]

View File

@ -48,11 +48,10 @@ When clicking submit, every recipe containing the word will be updated (tags are
### Docker-Compose ### Docker-Compose
1. Clone this repository to your desired install location 1. Clone this repository to your desired install location
2. Choose one of the included `docker-compose.yml` files [here](https://github.com/vabene1111/recipes/tree/develop/docs/docker). 2. Choose one of the included configurations [here](https://github.com/vabene1111/recipes/tree/develop/docs/docker).
3. Copy it to the root directory (where this readme is) 3. Copy (if needed) the `docker-compose.override.yml.template` to `docker-compose.override.yml` and uncomment the configurations you need.
4. Start the container (`docker-compose up -d`) 4. Start the container (`docker-compose up -d`)
5. This time and **on each update** run `update.sh` to apply migrations and collect static files 5. Create a default user by running `docker-compose exec -it <web_recipes> createsuperuser`. **Replace "<web_recipes>" with your instance name which can be found via running `docker ps`**
6. Create a default user by executing into the container with `docker-compose exec web_recipes sh` and run `python3 manage.py createsuperuser`.
### Manual ### Manual
Copy `.env.template` to `.env` and fill in the missing values accordingly. Copy `.env.template` to `.env` and fill in the missing values accordingly.
@ -63,9 +62,7 @@ Otherwise simply follow the instructions for any django based deployment
## Updating ## Updating
0. Before updating it is recommended to **backup your database** 0. Before updating it is recommended to **backup your database**
1. Stop the container using `docker-compose down`. 1. Pull the project files and start the container again using `docker-compose up -d --build`.
2. Pull the project files and start the container again using `docker-compose up -d --build`.
3. Run `update.sh`
## Contributing ## Contributing
Pull Requests and ideas are welcome, feel free to contribute in any way. Pull Requests and ideas are welcome, feel free to contribute in any way.

9
boot.sh Normal file
View File

@ -0,0 +1,9 @@
#!/bin/sh
source venv/bin/activate
echo "Updating database"
python manage.py migrate
python manage.py collectstatic --noinput
echo "Done"
exec gunicorn -b :8080 --access-logfile - --error-logfile - recipes.wsgi

View File

@ -2,3 +2,10 @@ This is a docker compose example when using [jwilder's nginx reverse proxy](http
in combination with [jrcs's letsencrypt companion](https://hub.docker.com/r/jrcs/letsencrypt-nginx-proxy-companion/). in combination with [jrcs's letsencrypt companion](https://hub.docker.com/r/jrcs/letsencrypt-nginx-proxy-companion/).
Please refer to the appropriate documentation on how to setup the reverse proxy and networks. Please refer to the appropriate documentation on how to setup the reverse proxy and networks.
Remember to add the appropriate environment variables to `.env` file:
```
VIRTUAL_HOST=
LETSENCRYPT_HOST=
LETSENCRYPT_EMAIL=
```

View File

@ -2,42 +2,42 @@ version: "3"
services: services:
db_recipes: db_recipes:
restart: always restart: always
image: "postgres:11-alpine" image: postgres:11-alpine
volumes: volumes:
- ./postgresql:/var/lib/postgresql/data - ./postgresql:/var/lib/postgresql/data
env_file: env_file:
- ./.env - ./.env
networks: networks:
- default - default
web_recipes: web_recipes:
build: . image: vabene1111/recipes
restart: always restart: always
env_file: env_file:
- ./.env - ./.env
command: "gunicorn --bind 0.0.0.0:8080 recipes.wsgi"
volumes: volumes:
- .:/Recipes - ./staticfiles:/opt/recipes/staticfiles
- ./mediafiles:/opt/recipes/mediafiles
depends_on: depends_on:
- db_recipes - db_recipes
networks: networks:
- default - default
nginx_recipes: nginx_recipes:
image: "nginx" image: nginx:mainline-alpine
restart: always restart: always
env_file: env_file:
- ./.env - ./.env
volumes: volumes:
- ./nginx/conf.d:/etc/nginx/conf.d - ./nginx/conf.d:/etc/nginx/conf.d
- ./staticfiles:/static - ./staticfiles:/static
- ./mediafiles:/media - ./mediafiles:/media
networks: networks:
- default - default
- nginx-proxy - nginx-proxy
networks: networks:
default: default:
nginx-proxy: nginx-proxy:
external: external:
name: nginx-proxy name: nginx-proxy

View File

@ -0,0 +1,5 @@
This is the most basic configuration to run this image with docker compose.
> **NOTE**: There is no proxy included in this configuration and gunicorn is directly exposed as the webserver which is
> not recommended by according to the [gunicorn devs](https://serverfault.com/questions/331256/why-do-i-need-nginx-and-something-like-gunicorn).
> It is higly recommended to configure an additional proxy (nginx, ...) in front of this.

View File

@ -2,39 +2,28 @@ version: "3"
services: services:
db_recipes: db_recipes:
restart: always restart: always
image: "postgres:11-alpine" image: postgres:11-alpine
volumes: volumes:
- ./postgresql:/var/lib/postgresql/data - ./postgresql:/var/lib/postgresql/data
env_file: env_file:
- ./.env - ./.env
networks: networks:
- default - default
web_recipes: web_recipes:
build: . image: vabene1111/recipes
restart: always restart: always
env_file: env_file:
- ./.env - ./.env
command: "gunicorn --bind 0.0.0.0:8080 recipes.wsgi"
volumes: volumes:
- .:/Recipes - ./staticfiles:/opt/recipes/staticfiles
depends_on: - ./mediafiles:/opt/recipes/mediafiles
- db_recipes
networks:
- default
nginx_recipes:
image: "nginx"
restart: always
env_file:
- ./.env
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d
- ./staticfiles:/static
- ./mediafiles:/media
ports: ports:
- 80:80 - 80:8080
networks:
- default
depends_on: depends_on:
- web_recipes - db_recipes
networks:
- default
networks:
default:

View File

@ -2,39 +2,34 @@ version: "3"
services: services:
db_recipes: db_recipes:
restart: always restart: always
image: "postgres:11-alpine" image: postgres:11-alpine
volumes: volumes:
- ./postgresql:/var/lib/postgresql/data - ./postgresql:/var/lib/postgresql/data
env_file: env_file:
- ./.env - ./.env
networks:
- default
web_recipes: web_recipes:
build: . image: vabene1111/recipes
restart: always restart: always
env_file: env_file:
- ./.env - ./.env
command: "gunicorn --bind 0.0.0.0:8080 recipes.wsgi"
volumes: volumes:
- .:/Recipes - ./staticfiles:/opt/recipes/staticfiles
- ./mediafiles:/opt/recipes/mediafiles
depends_on: depends_on:
- db_recipes - db_recipes
labels: # This lables are only examples!
nginx_recipes:
image: "nginx"
restart: always
env_file:
- ./.env
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d
- ./staticfiles:/static
- ./mediafiles:/media
labels:
- "traefik.enable=true" - "traefik.enable=true"
- "traefik.http.routers.recipes.rule=Host(`recipes.mydomain.com`, `recipes.myotherdomain.com`)" - "traefik.http.routers.recipes.rule=Host(`recipes.mydomain.com`, `recipes.myotherdomain.com`)"
- "traefik.http.routers.recipes.entrypoints=web_secure" - "traefik.http.routers.recipes.entrypoints=web_secure"
- "traefik.http.routers.recipes.tls.certresolver=le_resolver" - "traefik.http.routers.recipes.tls.certresolver=le_resolver"
networks:
- default
- traefik
networks: networks:
default: default:
external: traefik: # This is you external traefic network
name: traefik external: true

View File

@ -58,6 +58,7 @@ INSTALLED_APPS = [
MIDDLEWARE = [ MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware', 'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware', 'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware', 'django.middleware.csrf.CsrfViewMiddleware',
@ -133,8 +134,8 @@ USE_L10N = True
USE_TZ = True USE_TZ = True
LANGUAGES = [ LANGUAGES = [
('de', _('German')), ('de', _('German')),
('en', _('English')), ('en', _('English')),
] ]
# Static files (CSS, JavaScript, Images) # Static files (CSS, JavaScript, Images)
@ -145,3 +146,6 @@ STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
MEDIA_URL = '/media/' MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "mediafiles") MEDIA_ROOT = os.path.join(BASE_DIR, "mediafiles")
# Serve static files with gzip
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

View File

@ -17,4 +17,5 @@ lxml
webdavclient3 webdavclient3
python-dotenv python-dotenv
psycopg2-binary psycopg2-binary
whitenoise
gunicorn gunicorn

5
setup.sh Normal file
View File

@ -0,0 +1,5 @@
#!/bin/sh
source venv/bin/activate
echo "Creating Superuser."
python manage.py createsuperuser
echo "Done"

View File

@ -1,3 +0,0 @@
#!/usr/bin/env bash
docker-compose run web_recipes python3 manage.py migrate
docker-compose run web_recipes python3 manage.py collectstatic --noinput