-
-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Dockerfile config #441
Open
lkraider
wants to merge
7
commits into
nsupdate-info:master
Choose a base branch
from
nKey:docker
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0e718b3
build: add Dockerfile config
lkraider 3ce4c54
Merge remote-tracking branch 'upstream/master' into docker
lkraider c941a9e
build: move docker into contrib
lkraider 4d166c8
build(docker): parameterize Dockerfile path
lkraider e5d3453
build(docker): simplify release action
lkraider a181c6d
build(docker): PEP8 fixes on build script
lkraider 685b5bd
build(docker): require git tag for release
lkraider File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
MAINTAINER=nkeyapps | ||
TAG=nsupdate.info | ||
VER=$(shell git describe) | ||
UWSGI_UID=700 | ||
UWSGI_GID=700 | ||
BASE_DIR='../../..' | ||
DOCKER_DIR='misc/contrib/docker' | ||
|
||
all: prod | ||
|
||
prod: | ||
cd $(BASE_DIR); docker build --build-arg BUILD=prod --build-arg uwsgi_uid=$(UWSGI_UID) --build-arg uwsgi_gid=$(UWSGI_GID) --build-arg DOCKER_DIR=$(DOCKER_DIR) -t $(MAINTAINER)/$(TAG):$(VER) --rm -f $(DOCKER_DIR)/build/Dockerfile . | ||
docker tag $(MAINTAINER)/$(TAG):$(VER) $(MAINTAINER)/$(TAG):prod | ||
|
||
release: require_tag prod release_latest | ||
docker push $(MAINTAINER)/$(TAG):$(VER) | ||
|
||
release_latest: | ||
docker tag $(MAINTAINER)/$(TAG):$(VER) $(MAINTAINER)/$(TAG):latest | ||
docker push $(MAINTAINER)/$(TAG):latest | ||
|
||
require_tag: | ||
@if ! git describe --exact-match; then\ | ||
echo " *** Don't forget to create a tag by creating an official GitHub release.";\ | ||
exit 1;\ | ||
fi | ||
|
||
dev: | ||
cd $(BASE_DIR); docker build --build-arg BUILD=dev --build-arg uwsgi_uid=$(UWSGI_UID) --build-arg uwsgi_gid=$(UWSGI_GID) --build-arg DOCKER_DIR=$(DOCKER_DIR) -t $(MAINTAINER)/$(TAG):$(VER)-dev --rm -f $(DOCKER_DIR)/build/Dockerfile . | ||
docker tag $(MAINTAINER)/$(TAG):$(VER)-dev $(MAINTAINER)/$(TAG):dev | ||
|
||
test: dev clean | ||
docker run -P --name $(TAG)-test-dev -d $(MAINTAINER)/$(TAG):dev | ||
|
||
clean: | ||
-docker rm -f -v $(TAG)-test-dev |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
FROM debian:stable-slim | ||
LABEL maintainer="[email protected]" | ||
|
||
ARG BUILD=prod | ||
ARG DOCKER_DIR=./misc/contrib/docker | ||
ARG uwsgi_uid=700 | ||
ARG uwsgi_gid=700 | ||
|
||
ENV BUILD=$BUILD | ||
ENV DOCKER_CONTAINER=1 | ||
ENV UWSGI_INI /nsupdate/uwsgi.ini | ||
ENV DJANGO_SETTINGS_MODULE=local_settings | ||
ENV DJANGO_SUPERUSER=django | ||
ENV DJANGO_SUPERPASS=S3cr3t | ||
ENV [email protected] | ||
ENV [email protected] | ||
ENV SECRET_KEY=S3cr3t | ||
ENV BASEDOMAIN=nsupdate.localdomain | ||
ENV REGISTRATION_OPEN=False | ||
|
||
RUN mkdir /static | ||
RUN mkdir /upload | ||
RUN mkdir /var/run/uwsgi | ||
|
||
# Install runtime tools | ||
RUN DEBIAN_FRONTEND=noninteractive apt-get update \ | ||
&& apt-get install -y --no-install-recommends \ | ||
python3 \ | ||
python3-setuptools \ | ||
python3-pip | ||
|
||
# Install confd | ||
RUN apt-get install -y --no-install-recommends wget \ | ||
&& mkdir -p /usr/local/bin \ | ||
&& wget -O /usr/local/bin/confd https://github.com/kelseyhightower/confd/releases/download/v0.16.0/confd-0.16.0-linux-amd64 \ | ||
&& chmod +x /usr/local/bin/confd \ | ||
&& mkdir -p /etc/confd/{conf.d,templates} \ | ||
&& apt-get autoremove -y wget | ||
|
||
# Use local version of nsupdate from sources | ||
COPY ./*.py /nsupdate/ | ||
lkraider marked this conversation as resolved.
Show resolved
Hide resolved
|
||
COPY ./*.rst /nsupdate/ | ||
COPY ./*.in /nsupdate/ | ||
COPY ./*.cfg /nsupdate/ | ||
COPY ./*.txt /nsupdate/ | ||
COPY ./requirements.d/ /nsupdate/requirements.d/ | ||
COPY ./.git/ /nsupdate/.git/ | ||
COPY ./src/ /nsupdate/src/ | ||
WORKDIR /nsupdate | ||
|
||
# Build and install | ||
RUN DEBIAN_FRONTEND=noninteractive apt-get update \ | ||
&& apt-get install -y --no-install-recommends \ | ||
git \ | ||
python3-dev \ | ||
build-essential \ | ||
libpcre3-dev \ | ||
&& pip3 install wheel \ | ||
&& python3 setup.py bdist_wheel \ | ||
&& pip3 install psycopg2-binary uwsgi \ | ||
&& pip3 install -r requirements.d/$BUILD.txt \ | ||
&& pip3 install -e . \ | ||
&& cp /usr/lib/x86_64-linux-gnu/libpython* /tmp \ | ||
&& apt-get autoremove -y \ | ||
git \ | ||
python3-dev \ | ||
build-essential \ | ||
libpcre3-dev \ | ||
&& cp /tmp/libpython* /usr/lib/x86_64-linux-gnu/ \ | ||
&& rm -rf build \ | ||
&& rm -rf .git && rm -rf /root/.cache \ | ||
&& rm -rf /tmp/* /var/tmp/* \ | ||
&& rm -rf /var/lib/apt/lists/* | ||
|
||
# Copy helper files | ||
COPY $DOCKER_DIR/build/django/create-superuser.py /nsupdate/src/nsupdate/management/commands/create-superuser.py | ||
COPY $DOCKER_DIR/build/uwsgi.ini /nsupdate/uwsgi.ini | ||
COPY $DOCKER_DIR/build/confd/ /etc/confd/ | ||
COPY $DOCKER_DIR/build/setup.sh / | ||
COPY $DOCKER_DIR/build/docker-entrypoint.sh /var/local/ | ||
|
||
# Set the permissions according to env options | ||
RUN chmod a+x /var/local/docker-entrypoint.sh | ||
RUN bash /setup.sh "${uwsgi_uid}" "${uwsgi_gid}" | ||
|
||
VOLUME /nsupdate | ||
VOLUME /static | ||
VOLUME /upload | ||
VOLUME /var/run/uwsgi | ||
|
||
EXPOSE 3031 | ||
EXPOSE 8080 | ||
|
||
ENTRYPOINT ["/var/local/docker-entrypoint.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[template] | ||
src = "local_settings.tmpl" | ||
dest = "/nsupdate/local_settings.py" |
27 changes: 27 additions & 0 deletions
27
misc/contrib/docker/build/confd/templates/local_settings.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from nsupdate.settings.{{getenv "BUILD"}} import * | ||
|
||
STATIC_ROOT='/static' | ||
MEDIA_ROOT='/upload' | ||
|
||
SECRET_KEY = '{{ getenv "SECRET_KEY" }}' | ||
BASEDOMAIN = '{{ getenv "BASEDOMAIN" }}' | ||
WWW_HOST='{{ getenv "BASEDOMAIN" }}' | ||
|
||
REGISTRATION_OPEN = False | ||
|
||
SERVICE_CONTACT = '{{ getenv "SERVICE_CONTACT" }}' | ||
|
||
ALLOWED_HOSTS=['localhost', '127.0.0.1', '[::1]', '{{ getenv "BASEDOMAIN" }}'] | ||
|
||
{{ if getenv "DB_NAME"}} | ||
DATABASES = { | ||
'default': { | ||
'ENGINE': 'django.db.backends.postgresql', | ||
'NAME': '{{getenv "DB_NAME"}}', # database name | ||
'USER': '{{getenv "DB_USER"}}', | ||
'PASSWORD': '{{getenv "DB_PASS"}}', | ||
'HOST': '{{getenv "DB_HOST"}}', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP. | ||
'PORT': '{{getenv "DB_PORT"}}' # Set to empty string for default. | ||
} | ||
} | ||
{{end}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Title: create-superuser.py | ||
# Link: https://gist.github.com/c00kiemon5ter/7806c1eac8c6a3e82f061ec32a55c702 | ||
# License: None (Public Domain) | ||
|
||
from django.contrib.auth.management.commands import createsuperuser | ||
from django.core.management import CommandError | ||
|
||
|
||
class Command(createsuperuser.Command): | ||
help = 'Create a superuser with a password non-interactively' | ||
|
||
def add_arguments(self, parser): | ||
super(Command, self).add_arguments(parser) | ||
parser.add_argument( | ||
'--preserve', dest='preserve', default=False, action='store_true', | ||
help='Exit normally if the user already exists.', | ||
) | ||
parser.add_argument( | ||
'--password', dest='password', default=None, | ||
help='Specifies the password for the superuser.', | ||
) | ||
|
||
def handle(self, *args, **options): | ||
options.setdefault('interactive', False) | ||
database = options.get('database') | ||
password = options.get('password') | ||
username = options.get('username') | ||
email = options.get('email') | ||
|
||
if not password or not username or not email: | ||
raise CommandError( | ||
"--username, --password, and --email are required options") | ||
|
||
if username and options.get('preserve'): | ||
exists = self.UserModel._default_manager.db_manager( | ||
database).filter(username=username).exists() | ||
if exists: | ||
self.stdout.write( | ||
"User exists, exiting normally due to --preserve") | ||
return | ||
|
||
user_data = { | ||
'username': username, | ||
'password': password, | ||
'email': email, | ||
} | ||
|
||
self.UserModel._default_manager.db_manager( | ||
database).create_superuser(**user_data) | ||
|
||
if options.get('verbosity', 0) >= 1: | ||
self.stdout.write("Superuser created successfully.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/usr/bin/env bash | ||
|
||
/usr/local/bin/confd -onetime -backend env | ||
|
||
python3 manage.py collectstatic | ||
python3 manage.py migrate | ||
python3 manage.py create-superuser --preserve --username $DJANGO_SUPERUSER --password $DJANGO_SUPERPASS --email $DJANGO_EMAIL | ||
|
||
# Fix Permissions prior to running uwsgi server | ||
chown -R www-data:www-data /static | ||
chown -R www-data:www-data /nsupdate | ||
|
||
uwsgi --uid=www-data --gid=www-data --ini uwsgi.ini |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/usr/bin/bash | ||
|
||
# Quit on error. | ||
set -e | ||
# Treat undefined variables as errors. | ||
set -u | ||
|
||
|
||
function main { | ||
local uwsgi_uid="${1:-}" | ||
local uwsgi_gid="${2:-}" | ||
|
||
# Change the uid | ||
if [[ -n "${uwsgi_uid:-}" ]]; then | ||
usermod -u "${uwsgi_uid}" www-data | ||
fi | ||
# Change the gid | ||
if [[ -n "${uwsgi_gid:-}" ]]; then | ||
groupmod -g "${uwsgi_gid}" www-data | ||
fi | ||
|
||
# Setup permissions on the run directory where the sockets will be | ||
# created, so we are sure the app will have the rights to create them. | ||
|
||
# Set owner. | ||
chown www-data:www-data /var/run/uwsgi | ||
# Set permissions. | ||
chmod u=rwX,g=rwX,o=--- /var/run/uwsgi | ||
|
||
# Set app folder permissions | ||
chown -R www-data:www-data /nsupdate | ||
chown -R www-data:www-data /static | ||
chown -R www-data:www-data /upload | ||
|
||
} | ||
|
||
|
||
main "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[uwsgi] | ||
socket = /var/run/uwsgi/uwsgi.sock | ||
socket = :3031 | ||
http-socket = :8080 | ||
workers = 3 | ||
master = true | ||
wsgi-file=/nsupdate/src/nsupdate/wsgi.py | ||
env = LANG=en_US.UTF-8 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that the nsupdate.info version?
We use setuptools_scm - maybe that can help with versioning here also?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it uses the git tag from the project for versioning the generated docker image. This way there are no dependencies, using setuptools would require the release manager to create a virtualenv/pip install things first on the host machine. (one of the usefulness of docker is that all dependencies are installed into the container, not on the host)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: setuptools_scm basically does the same command behind the scenes (https://github.com/pypa/setuptools_scm/blob/fbaeaa947e403d78964654d3ab68cbc75caccfec/src/setuptools_scm/git.py#L15)