====== Anthenao setup ====== * http://www.marinamele.com/taskbuster-django-tutorial/taskbuster-working-environment-and-start-django-project#working-directory ====== Docker ====== * Docker tutorial: http://localhost/~dang/wiki/doku.php?id=devops:docker:docker_tutorial * Using the docker VM: /mnt/nfv/01_docker_django_lab/ansible_quicklabs * Git source + docker: https://thuydang@bitbucket.org/thuydang/anthenao.git ====== Project Folder ====== ===== cookiecutter-django-cms ===== ===== Docker Web ===== ===== Docker DB ===== ===== Needs update ===== (anthenao_env) [dang@dai142 76_anthenao]$ tree . -L 1 . ├── anthenao_env --> env ├── anthenao_docker --> django-cms projectgit │   ├── anthenao_db --> db container │   ├── anthenao_web --> django container │   ├── eco --> docker-compose xml │   └── run_dev.sh --> image, etc.. └── anthenao_ws --> design, docs, etc... $ virtualenv env $ source env/bin/activate (env) $ pip install djangocms-installer (env) $ djangocms mysite ====== Nginx ====== * https://www.techandme.se/set-up-nginx-reverse-proxy/ ===== Serving Static Files ===== * Make django serve static files: http://stackoverflow.com/questions/39228112/dockerized-nginx-and-django-how-to-serve-static-files * Export static folder and serve with nginx: http://stackoverflow.com/questions/25271773/docker-nginx-django-and-how-to-serve-static-files 2nd approach sounds better. DJ-static settings/production.py ... PRODUCTION=True ... wsgi.py from django.core.wsgi import get_wsgi_application from django.conf import settings if settings.PRODUCTION: from dj_static import Cling, MediaCling application = Cling(MediaCling(get_wsgi_application())) #application = Cling(get_wsgi_application()) else: application = get_wsgi_application() ====== Test with docker ====== See workflow: http://localhost/~dang/wiki/doku.php?id=devops:docker:docker_dev_workflow#mounting_host_folders Start test container cd anthenao_s2i docker-compose -f docker-compose-dev.yml run --service-ports web bash From compose.yml we can see: * Set DJANGO_SETTINGS_MODULE=anthenao.settings.dev to run django in dev mode. * Not setting DATABASES vars. * Using the same docker image and port mapping: host 8000--> container 8080. ./manage.py runserver 0.0.0.0:8080 === Compose.yml === version: "2" services: web: container_name: web #image: centos/python-27-centos7 image: anthenao_img_web_final build: ../../anthenao_web networks: - anthenao_bridge ports: #- "host-->container" - "8000:8080" #volumes: # - ./api:/var/www/app #env_file: ../../anthenao_web/anthenao/.s2i/environment environment: - DJANGO_SETTINGS_MODULE=anthenao.settings.dev #- DB_ENGINE=django.db.backends.postgresql_psycopg2 #- DB_NAME=anthenaodb #- DB_USER=dbuser #- #DB_PASSWORD= #- DB_HOST=db #- DB_Port=5432 #tty: true depends_on: - db #command: /var/www/app/start.sh ===== Dev with docker ===== **Quickstart container to dev app** ==== Dockerfile ==== === Code === [dang@dai142 anthenao_web]$ cat Dockerfile.development # This image is built with s2i and ready to use. ### 2 Steps # 1. Make (pre)build image: docker build -t anthenao_img_web_build | or make build (Makefile) # 2. build s2i command # s2i build --loglevel=4 anthenao_src anthenao_img_web_build anthenao_img_web_final # s2i build --loglevel=4 anthenao anthenao_img_web_build anthenao_img_web_final # change src file and run more s2i build to create final image. # 3. Commit anthenao_img_web_final # 4. Use uploaded image for production with production Dockerfile #FROM vsrccom/anthenao_img_web FROM centos/python-27-centos7 MAINTAINER vsrc.com EXPOSE 8080 ENV PYTHON_VERSION=2.7 \ PATH=$HOME/.local/bin/:$PATH LABEL io.k8s.description="Platform for building and running Python 2.7 applications" \ io.k8s.display-name="Python 2.7" \ io.openshift.expose-services="8080:http" \ io.openshift.tags="builder,python,python27,rh-python27" USER root RUN yum install -y centos-release-scl && \ INSTALL_PKGS="libjpeg-turbo libjpeg-turbo-devel python27-python-pip nss_wrapper atlas-devel gcc-gfortran gettext postgresql-libs nmap-ncat vim-enhanced net-tools" && \ yum install -y --setopt=tsflags=nodocs --enablerepo=centosplus $INSTALL_PKGS && \ rpm -V $INSTALL_PKGS && \ # Some additional packages #pip install virtualenvwapper && \ #echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc && \ # Remove centos-logos (httpd dependency, ~20MB of graphics) to keep image # size smaller. #rpm -e --nodeps centos-logos && \ yum clean all -y # Each language image can have 'contrib' a directory with extra files needed to # run and build the applications. COPY ./contrib/ /opt/app-root RUN mkdir -p /opt/app-root/logs RUN chown -R 1001:0 /opt/app-root && chmod -R ug+rwx /opt/app-root USER 1001 ENV DJANGO_SETTINGS_MODULE=anthenao.settings.dev WORKDIR /opt/app-root/src ## run dev env. # ==== Headline ==== Create image with vim, etc Run as root with source folder on host mounted: sudo docker run -it -u root -v $(pwd)/anthenao:/opt/app-root/src -p 8000:8080 anthenao_dev_img bash **Install packages, make changes ** pip install requirements/development.txt **Run Server** avoid binding to 127.0.0.1, can not access from outside container: python manage.py runserver 0.0.0.0:8080 http://stackoverflow.com/questions/27806631/docker-rails-app-fails-to-be-served-curl-56-recv-failure-connection-reset **Commit Changes** [dang@dai142 anthenao_web]$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 549ee7dc07fd anthenao_dev_img "container-entrypoint" 2 minutes ago Up 2 minutes 8080/tcp dreamy_ptolemy [dang@dai142 anthenao_web]$ sudo commit 549ee7dc07fd anthenao/ Dockerfile Dockerfile.s2i.base Makefile runtime.txt AUTHORS.rst Dockerfile.development LICENSE.rst Procfile s2i/ contrib/ Dockerfile.production logs/ README.rst test/ [dang@dai142 anthenao_web]$ sudo docker commit 549ee7dc07fd anthenao_dev_img sha256:e66985cfd36b2bf7ca14f566b5a33708e58d5f290e0f469db0bf4ff78eacc797 [dang@dai142 anthenao_web]$ ===== Auto Create SuperUser ===== * For old DJ: http://stackoverflow.com/questions/6244382/how-to-automate-createsuperuser-on-django * http://stackoverflow.com/questions/15048963/alternative-to-the-deprecated-setup-environ-for-one-off-django-scripts ==== createsu.py ==== import os import django #from myapp import models os.environ.setdefault("DJANGO_SETTINGS_MODULE", "anthenao.settings") django.setup() from django.conf import settings from django.contrib.auth.models import User #print models.MyModel.objects.get(pk=1) #username = '$USER' if User.objects.filter(username='admin').count()==0: User.objects.create_superuser('admin', 'admin@email.com', 'password'); print('Superuser created.'); else: print('Superuser creation skipped.'); #u = User(username='unique_fellow') #u.set_password('a_very_cryptic_password') #u.is_superuser = True #u.is_staff = True #u.save()