Skip to content

Commit

Permalink
Merge pull request #18 from SteveKueng/docker
Browse files Browse the repository at this point in the history
Docker
  • Loading branch information
SteveKueng authored Sep 17, 2023
2 parents 69f5806 + 45f4681 commit 8a3bf48
Show file tree
Hide file tree
Showing 1,654 changed files with 6,792 additions and 214 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ db.sqlite3
.vscode/settings.json

# ignore collectstatics
static_root
munkiwebadmin/static/styles
static
staticfiles

config.codekit3
*.log*
Expand Down
83 changes: 0 additions & 83 deletions Dockerfile

This file was deleted.

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ virtualenv mwa2_virtualenv

### 5. Init DB
```bash
cd app
python manage.py migrate
python manage.py createsuperuser
```
Expand All @@ -52,6 +53,8 @@ open startDevServer.sh and change the environment variables
## Docker
comming soon

docker exec -it munkiwebadmin sh -c "python manage.py createsuperuser"

### Docker variables

| Variable | Usage | Default|
Expand Down
37 changes: 37 additions & 0 deletions app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
FROM python:3.11-slim

# set work directory
WORKDIR /usr/src/app
COPY . .

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# ------ update and install software
# update
RUN apt-get update \
&& apt-get upgrade -y \
&& apt-get install -y gcc default-libmysqlclient-dev python3-dev \
libxml2-dev libxslt1-dev zlib1g-dev libsasl2-dev libldap2-dev \
build-essential libssl-dev libffi-dev libjpeg-dev \
libpq-dev liblcms2-dev libblas-dev libatlas-base-dev pkg-config curl unzip \
&& rm -rf /var/lib/apt/lists/*
# -------

# install dependencies
RUN pip install --upgrade pip
RUN pip install -r requirements.txt

# create dirs
RUN mkdir /munkirepo
RUN mkdir /munkitools

# download munkitools
RUN curl -Lk -o /tmp/munkitools.zip `curl --silent https://api.github.com/repos/munki/munki/releases/latest | /usr/bin/awk '/zipball_url/ { print $2 }' | sed 's/[",]//g'` && unzip /tmp/munkitools.zip -d /tmp/munkitools && rm -rf /tmp/munkitools.zip
RUN cp -r /tmp/munkitools/munki-munki*/code/client/* /munkitools && rm -rf /tmp/munkitools

VOLUME [ "/munkirepo" ]

# run entrypoint.sh
ENTRYPOINT ["/usr/src/app/entrypoint.sh"]
76 changes: 76 additions & 0 deletions app/Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
###########
# BUILDER #
###########

# pull official base image
FROM python:3.11-slim as builder

# set work directory
WORKDIR /usr/src/app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install system dependencies
RUN apt-get update \
&& apt-get upgrade -y \
&& apt-get install -y gcc default-libmysqlclient-dev python3-dev \
libxml2-dev libxslt1-dev zlib1g-dev libsasl2-dev libldap2-dev \
build-essential libssl-dev libffi-dev libjpeg-dev \
libpq-dev liblcms2-dev libblas-dev libatlas-base-dev pkg-config curl unzip \
&& rm -rf /var/lib/apt/lists/*

# lint
RUN pip install --upgrade pip
COPY . /usr/src/app/

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

# download munkitools
RUN curl -Lk -o /tmp/munkitools.zip `curl --silent https://api.github.com/repos/munki/munki/releases/latest | /usr/bin/awk '/zipball_url/ { print $2 }' | sed 's/[",]//g'` && unzip /tmp/munkitools.zip -d /tmp/munkitools && rm -rf /tmp/munkitools.zip


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

# pull official base image
FROM python:3.11-slim

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

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

# copy project
COPY . $APP_HOME

# install dependencies
RUN apt-get update && apt-get install -y --no-install-recommends nginx
COPY --from=builder /usr/src/app/wheels /wheels
RUN pip install --upgrade pip
RUN pip install --no-cache /wheels/*
COPY --from=builder /tmp/munkitools /munkitools

# nginx config
COPY munkiwebadmin/munkiwebadmin.conf /etc/nginx/sites-available/default

# static files
RUN mkdir $APP_HOME/staticfiles
RUN python manage.py collectstatic --noinput

# volumes
VOLUME [ "/munkirepo" ]

# run entrypoint.prod.sh
ENTRYPOINT ["/home/app/web/entrypoint.prod.sh"]
EXPOSE 80
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
40 changes: 40 additions & 0 deletions app/entrypoint.prod.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/sh

NAME="MunkiWebAdmin" # Name of the application
DJANGODIR=/home/app/web # Django project directory
SOCKFILE=/home/app/gunicorn.sock # we will communicte using this unix socket
USER=root # the user to run as
GROUP=root # the group to run as
NUM_WORKERS=3 # how many worker processes should Gunicorn spawn
DJANGO_SETTINGS_MODULE=munkiwebadmin.settings # which settings file should Django use


if [ "$DATABASE" = "postgres" ]
then
echo "Waiting for postgres..."

while ! nc -z $SQL_HOST $SQL_PORT; do
sleep 0.1
done

echo "PostgreSQL started"
fi

# migrate database
python manage.py migrate

echo "Starting $NAME as `whoami`"

export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE

# Start your Django Unicorn
gunicorn munkiwebadmin.wsgi:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--bind=0.0.0.0:8000 \
--log-level=debug \
--log-file=/home/app/gunicorn.log \
--daemon

exec nginx -g "daemon off;"
17 changes: 17 additions & 0 deletions app/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh

if [ "$DATABASE" = "postgres" ]
then
echo "Waiting for postgres..."

while ! nc -z $SQL_HOST $SQL_PORT; do
sleep 0.1
done

echo "PostgreSQL started"
fi

# python manage.py flush --no-input
python manage.py migrate

exec "$@"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
37 changes: 37 additions & 0 deletions app/munkiwebadmin/munkiwebadmin.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#upstream munkiwebadmin {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response (in case the Unicorn master nukes a
# single worker for timing out).
# server unix:/home/app/gunicorn.sock fail_timeout=0;
#}

upstream munkiwebadmin {
server localhost:8000;
}

server {
listen 80;
client_max_body_size 12G;
access_log /home/app/access.log;
error_log /home/app/error.log;
location /static/ {
alias /home/app/web/staticfiles/;
}
location /media/ {
alias /munkirepo;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://munkiwebadmin;
break;
}
}
# Error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /home/app/web/munkiwebadmin/templates/;
}
}
File renamed without changes.
Loading

0 comments on commit 8a3bf48

Please sign in to comment.