From 4daaf718db4650002e98ffd3819d2d6164e80404 Mon Sep 17 00:00:00 2001 From: Alex Franco Date: Mon, 31 Aug 2015 22:16:34 +0000 Subject: [PATCH] Docker based environment setup Replicate production setup in Docker containers [YOCTO #7575] Signed-off-by: Alex Franco Signed-off-by: Paul Eggleton --- Dockerfile | 34 ++++++++++++++++++++++++++++++ Dockerfile.web | 4 ++++ README | 3 +++ docker/.gitconfig | 11 ++++++++++ docker/README | 26 +++++++++++++++++++++++ docker/git-proxy | 12 +++++++++++ docker/migrate.sh | 2 ++ docker/nginx.conf | 47 ++++++++++++++++++++++++++++++++++++++++++ docker/updatelayers.sh | 9 ++++++++ wsgi.py | 6 ++++++ 10 files changed, 154 insertions(+) create mode 100644 Dockerfile create mode 100644 Dockerfile.web create mode 100644 docker/.gitconfig create mode 100644 docker/README create mode 100755 docker/git-proxy create mode 100755 docker/migrate.sh create mode 100644 docker/nginx.conf create mode 100755 docker/updatelayers.sh create mode 100644 wsgi.py diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1f63e2a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,34 @@ +FROM buildpack-deps:latest +MAINTAINER Michael Halstead + +EXPOSE 80 +ENV PYTHONUNBUFFERED 1 +## Uncomment to set proxy ENVVARS within container +#ENV http_proxy http://your.proxy.server:port +#ENV https_proxy https://your.proxy.server:port + +RUN apt-get update +RUN apt-get install -y --no-install-recommends \ + python-pip \ + python-mysqldb \ + python-dev \ + python-imaging \ + netcat-openbsd \ + vim \ + && rm -rf /var/lib/apt/lists/* +RUN pip install --upgrade pip +RUN pip install gunicorn +CMD mkdir /opt/workdir +ADD . /opt/layerindex +RUN pip install -r /opt/layerindex/requirements.txt +ADD settings.py /opt/layerindex/settings.py +ADD docker/updatelayers.sh /opt/updatelayers.sh +ADD docker/migrate.sh /opt/migrate.sh + +## Uncomment to add a .gitconfig file within container +#ADD docker/.gitconfig /root/.gitconfig +## Uncomment to add a proxy script within container, if you choose to +## do so, you will also have to edit .gitconfig appropriately +#ADD docker/git-proxy /opt/bin/git-proxy + +CMD ["/usr/local/bin/gunicorn", "wsgi:application", "--workers=4", "--bind=:5000", "--log-level=debug", "--chdir=/opt/layerindex"] diff --git a/Dockerfile.web b/Dockerfile.web new file mode 100644 index 0000000..0b9e2c2 --- /dev/null +++ b/Dockerfile.web @@ -0,0 +1,4 @@ +FROM nginx:latest +MAINTAINER Michael Halstead +COPY docker/nginx.conf /etc/nginx/nginx.conf +COPY layerindex/static /usr/share/nginx/html/static diff --git a/README b/README index 1903fbf..84edee2 100644 --- a/README +++ b/README @@ -107,6 +107,9 @@ Setup instructions: * templates/base.html * templates/layerindex/about.html +7. To use layerindex-web with Docker containers, refer to docker/README + keeping in mind you'll need to set up Docker properly as part of the + setup process. Usage ----- diff --git a/docker/.gitconfig b/docker/.gitconfig new file mode 100644 index 0000000..f3ab372 --- /dev/null +++ b/docker/.gitconfig @@ -0,0 +1,11 @@ +# This .gitconfig file resides in /root/ in the "layersapp" container +# add any settings you'd like to have in there, such as proxy servers +[http] + #proxy = http://your.proxy.server:port +[https] + #proxy = https://your.proxy.server:port +[core] + # Optional: uncomment this line if you want to use a proxy script + #gitproxy = /opt/bin/git-proxy +[socks] + #proxy = your.socks.proxy:port diff --git a/docker/README b/docker/README new file mode 100644 index 0000000..14bc392 --- /dev/null +++ b/docker/README @@ -0,0 +1,26 @@ +## This is set up to make a cluster of three containers. First we build two from the root of the repo. +docker build -t halstead/layerindex-app . +docker build -t halstead/layerindex-web -f Dockerfile.web . + +## Start a database server. We use MariaDB in production. +## In order to configure your settings.py file to use this database server, use: +## 'ENGINE': 'django.db.backends.mysql', +## 'NAME': 'layersdb', +## 'USER': 'root', +## 'PASSWORD': 'testingpw', +## 'HOST': 'layersdb', +## 'PORT': '', +docker run -d --name layerdb -e MYSQL_ROOT_PASSWORD=testingpw -e MYSQL_DATABASE=layersdb mariadb + +## If you have a copy of the the production data now is the time to insert it. +## If not you can skip the next step for a clean install. +xzcat ./layerdb.sql.xz | docker run -i --link layerdb:layersdb --rm mariadb sh -c 'exec mysql -hlayersdb -uroot -p"testingpw" layersdb' + +docker run -d --link layerdb:layersdb --name layersapp halstead/layerindex-app +docker run -d --link layersapp:layersapp --name layersweb -p 49153:80 halstead/layerindex-web + +## To apply layerindex migration +docker run --rm --link layerdb:layersdb halstead/layerindex-app /opt/migrate.sh + +## To update the layer info we can run the job in a temporary container. +docker run --rm --link layerdb:layersdb halstead/layerindex-app /opt/updatelayers.sh diff --git a/docker/git-proxy b/docker/git-proxy new file mode 100755 index 0000000..9fd844e --- /dev/null +++ b/docker/git-proxy @@ -0,0 +1,12 @@ +#!/bin/bash + +# This simple proxy script (for git) resides at /opt/bin in the layersapp +# container. If you use it, uncomment the appropriate line in .gitproxy +# this method has been tested using a socks proxy +PROXY=your.proxy.server +PORT=portnumber + +METHOD="-X 5 -x ${PROXY}:${PORT}" + +# BSD netcat is used to connect +/bin/nc $METHOD $* diff --git a/docker/migrate.sh b/docker/migrate.sh new file mode 100755 index 0000000..ea394c3 --- /dev/null +++ b/docker/migrate.sh @@ -0,0 +1,2 @@ +#!/bin/bash +python /opt/layerindex/manage.py migrate layerindex diff --git a/docker/nginx.conf b/docker/nginx.conf new file mode 100644 index 0000000..7afc3a8 --- /dev/null +++ b/docker/nginx.conf @@ -0,0 +1,47 @@ +#daemon off; ##Included in CMD +error_log /dev/stdout info; +worker_processes 1; + +# user nobody nogroup; +pid /tmp/nginx.pid; + +events { + worker_connections 1024; + accept_mutex off; +} + +http { + include mime.types; + default_type application/octet-stream; + access_log /dev/stdout combined; + sendfile on; + + upstream app_server { + # For a TCP configuration: + server layersapp:5000 fail_timeout=0; + } + + server { + listen 80 default; + client_max_body_size 4G; + server_name _; + + keepalive_timeout 5; + + # path for static files + root /usr/share/nginx/html; + + location / { + try_files $uri @proxy_to_app; + } + + location @proxy_to_app { + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host $http_host; + proxy_redirect off; + + proxy_pass http://app_server; + } + + } +} diff --git a/docker/updatelayers.sh b/docker/updatelayers.sh new file mode 100755 index 0000000..50c73bf --- /dev/null +++ b/docker/updatelayers.sh @@ -0,0 +1,9 @@ +#!/bin/bash +update=/opt/layerindex/layerindex/update.py +$update -q -r +$update -b jethro -x -r -q +$update -b fido -x -r -q +$update -b dizzy -x -r -q +$update -b daisy -x -r -q +$update -b dora -x -r -q +$update -b dylan -x -r -q diff --git a/wsgi.py b/wsgi.py new file mode 100644 index 0000000..99f0915 --- /dev/null +++ b/wsgi.py @@ -0,0 +1,6 @@ +import os, sys +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings") +sys.path.append('/var/www/html/layerindex') + +from django.core.wsgi import get_wsgi_application +application = get_wsgi_application()