dockerize django apache developer environment - django

I tried to dockerize my django/apache web application.
In the big picture the dockerization worked out.
For deployment Annot is running in 3 containers. The apache_annot container holds the apache server and the postgresql database The media_annot container serves as web application media folder. The annot container holds the django python related code.
For development only 2 containers are needed: the apache_annot container and media_annot container. The django and python related code is placed into a directory at the host machine. apache_annot then simply mounts this directory as a data volume under /var/www/annot/.
Here the link to the Docker files:
https://gitlab.com/biotransistor/dock/tree/master
Issue:
Even the dockerized version works, it seems not to be done the real docker way.
Most annoying issue: Every time apache is restarted (which happens a lot in development) the apache_annot container and as such the whole web application stops.
To continuer apache_annot have to be restarted:
docker exec -ti apache_annot /bin/bash
Then postgresql database have to be restarted:
/etc/init.d/postgresql start
And cd into /var/www/annot/ to execute django development specific python code like:
python3 manage.py makemigrations.
It is nearly not possible to do development with this solution.
I can not be the only one who like to use a dockerize django apache developer environment. What do I do wrong?
Is there a way to run apache_annot so that the apache daemon can be rebooted without shouting down the apache_annot docker container?

Related

How to Dockerize a Django + Postgres App with Apache (Instead of NGINX) for production

I'm new to the DevOps world and I have been following this tutorial successfully, my Docker setup is almost identical to the one on the tutorial. I now would like to setup a Docker container with Django and Postgres but with Apache instead of NGINX for production. What commands should I add/modify to the Dockerfile, volumes and configurations to the docker-compose file and what configuration files should I have for the Apache configuration?

Django: How to switch back to local docker development after deployment on heroku

Hi I just finished deploying a project with Django, Docker and Heroku based on the https://djangoforprofessionals.com/ tutorial. However, I don't know how to switch back to local development after having deployed the app. If i go to 127.0.0.1:8000 in my browser i get a ERR_TIMED_OUT message.
Here is the official source code, which corresponds 1:1 to my code: https://github.com/wsvincent/djangoforprofessionals/tree/master/ch18-deployment
In the tutorial there are two docker-compose files, a docker-compose.yml for local development and a docker-compose-prod.yml for deployment.
My idea was to run docker-compose down and docker-compose -f docker-compose.yml up -d --build after deployment to continue working locally, but it did not work out.
Any help would be much appreciated!

Running postgres 9.5 and django in one container for CI (Bamboo)

I am trying to configure a CI job on Bamboo for a Django app, the tests to be run rely on a database (postgres 9.5). It seems that a prudent way to go about is it run the whole test in a docker container, as I do not control the agent environment so I cannot install Postgres there.
Most guides I found recommend running postgres and django in two separate containers and using docker-compose to easily manage them. In this scenario each docker image runs just one service, started with CMD. In Bamboo I cannot use docker-compose however, I need to use just one image, so I am trying to get Postgres and Django to run nicely together in one container but with little success so far.
My problem is that I see no easy way to start Postgres as a service inside docker but NOT as a docker CMD command, official postgre image uses an entrypoint.sh approach, also described in the official docker docs
But it is not clear to me how to implement that. I would appreciate your help!
Well, basically you would start postgres as a background process in the docker-entrypoint shell script that does otherwise start your django application.
The only trick here is that you need to put a 'trap' command in it so that you can send a shutdown/kill to the background process when your master process stops.
Although I have done that a thousand times, I know that it is a good source for programming errors. In general I do just use my docker-systemctl-replacement which takes care of running multiple applications as services, just as if the container is a virtual machine hosting multiple applications.
Your only other option is to add in a startup script in your Dockerfile, or kick it off as part of your docker run ... commands. We don't generally use the "Docker" tasks, as I find them ... distasteful (also why I usually just fall back to running a "Script" task, and directly calling docker run in that script task)
Anyway, you'd have to have your Docker container execute a script that would:
Start up Postgres (like a sudo systemctl start postgresql)
Execute your tests.
Your Dockerfile will have to install Postgresql and do some minor setup work I imagine (like create relevant users and databases with the proper owner). Since we're all good citizens, we remember to never run your containers as root, right?
Note - you can always hack around getting two containers to talk to each other without using docker-compose. It's a bit less convenient, but you could do something like:
docker run --detach --cidfile=db_cidfile --name ci_db postgresql_image
...
docker run --link ci_db testing_image
Make sure that you EXPOSE the right ports on the postgresql image to the testing_image container.
EDIT: I'm looking more at my specific case - we just install Postgresql into a base CentOS host rather than use the postgresql default image (using yum install http://yum.postgresql.org/..../pgdg-centos...rpm and then just install postgresql-server and postgresql-contrib packages from there). There is a CMD [ "/usr/pgsql-ver/bin/postgres", "-D", "/var/lib/pgsql/ver/data"] in our Dockerfile, too. We don't do anything fancy with the docker container, though. NOTE: we don't use this in production at all, this is strictly for local and CI testing.

Running Django Python Server in AWS

I have a django running in AWS Ubuntu machine. Through SSH, I start the server at 8000 port. But when i close the ssh window, server stops and I can't access it through URL. What I want is to run the server all the time once it is started. How to go about it? Thanks.
You can use either Apache or Nginx to deploy Django App. If you are planning to Use Nginx, first install Nginx in the server and add Django configurations to Nginx configuration. You can follow this as a good guide.
You can do it the hacky way: create a bash script that executes the app (just running the same command you execute to run it), and run the bash script with nohup, which detaches the process from the shell and will allow the application to continue running when you close your session:
nohup ./my_bash_script.sh &
If you want to do it properly, create a service file and execute the app as a service. You can create a simple service file like this:
[Unit]
Description=My Django app
After=network.target
[Service]
PIDFile=/run/DjangoApp/pid
User=<your user>
Group=<your group>
WorkingDirectory=<working directory of your Django app>
ExecStart=<path to your bash script>
PrivateTmp=True
[Install]
WantedBy=multi-user.target
Save the file under /etc/systemd/system/djangoService.service. You enable the service with this command:
sudo systemctl enable djangoService
And run it with this command:
sudo service start djangoService
That will keep the service running. Bear in mind, though, that to service a proper Django app you may want to use Gunicorn/wsgi to serve the responses, using Nginx to reverse proxy the requests.
In development mode, Django has a development server, which is sufficient for testing purposes. Once you complete a web application and it's ready for production, the process of setting up the application on a server might be overwhelming for some, especially if you're doing it for the first time. This article provides a step-by-step guide on how to deploy Django-based web applications using mod_wsgi.
You can follow this article for setting mod_wsgi with Apache server. enter link description here
If the one which you are setting up for development only then you need to run the server in daemon mode.
on Ubuntu
run:>./manage.py runserver 0.0.0.0:8000 > /dev/null 2>&1 &
>exit
Django runserver permanent
Hope this helps, it is the best way to create the screen so we can monitor what is happening and take the control back.

Updating Django App on server

I am relatively new to Python/Django and have successfully deployed my first app. I want to update it now with some new changes, but I am not sure what the proper process is. My setup is ubuntu/nginx/gunicorn/postgres.
At the moment I am taking the following steps:
Stop nginx: sudo service nginx stop
Stop gunicorn: sudo service gunicorn stop
Backup the db? (not implemented - cant find it on the server)
Git Pull
python manage.py migrate
python manage.py collectstatic
restart gunicorn: sudo service gunicorn start
restart nginx: sudo service nginx restart
This is working, but I would appreciate some guidance if this is the complete, most accurate and safest way to do this please?
One lazy (yet recommended and professional) way of going about app updates is running automation script, like Fabric or Ansible.
However, if you wish to proceed the manual way (which is tedious), you might do something like:
Pull from git
Run migrations python manage.py migrate (This should ensure changes you made locally to your models reflect in production DB)
Run static collections to ensure new statics are reflected in server /static/ folder like so: python manage.py collectstatic
Then, restart your Django Server not Nginx. So something like: sudo service your_django_server_running_instance restart
On digitalOcean for instance (when used One-Click Install), your django server running instance is likely called gunicorn
Then you might want to look into automating your postgresql db as well