Docker files to be ignored on .gitignore - django

I'm new to docker. I've finalized dockerizing a Django projet of mine. What files and/or folders should I add to my .gitginore before pushing the changes to the project? Or no files should be added? I'm not talking about my .dockerignore file.

You should include any files that don't need to be in the final distribution or will be built when the container is built. Depending on your build process, this might include what is compiled for static assets (JS/CSS), Python packages downloaded by pip in venv as the build process might download again for each new build, any keys/secrets/passwords should not be committed to the .git repo as well, any caches or temporary files.
With docker builds, I prefer to keep them isolated so that only code is copied and then the rest is built by the docker build process. So packages are installed by pip and the config/keys/passwords are passed into the docker container as environment variables. Also, I typically put .git into the .dockerignore so the entire git repository isn't copied into the docker image.
You might find these two links helpful:
Docker Django
Django .gitignore

Related

Is it good practice to add .git folder to Github?

After i entered git init I have directory D:myproject\.git
Is it good to add .git folder it in Github or should i add it in .gitignore?
You should not worry about .git folder. It contains git internals and all information in your repository like commits, branches and blobs. So .git is a repository itself and is handled automatically.
If you are in the root directory of your app and you do git init command what happens is that the command says initialize this directory and everything below it as a git repository. And it will set up a local repository for you on your machine.
Local repository on your machine allows git to track version changes in our files. Once we are happy with all those changes and our files then we push them to a remote repository on GitHub.
And you don't have to add your .git folder in .gitignore file.

What should I do with plugin-created migrations?

I have, as usual the <project>/<app>/migrations folder that I added to the version control for deployment. Since recently I am also using django-auditlog, which creates its own migrations in <project>/env/Lib/site-packages/auditlog/migrations. These migrations are applied just like my own ones. So I wonder: should I also add them to VCS and deploy them?
No. You shouldn't deploy these migrations. In general you shouldn't deploy any third-party migrations.
The reason is simple. Third-party packages have their initial migrations inside their migrations directory. Each time you create a migration that depends on a third-party package, this migration will live in your app's migrations dir (not inside the package one).
So, when you first deploy your code and run pip install -r requirements.txt on your server, then all 3rd party packages will be installed and when you do ./manage.py migrate then Django will look at your INSTALLED_APPS and execute all migrations for each one in this list.
The same thing happens with any third-party package, just like Django itself. Suppose you have in your requirements.txt file only Django==1.10.6. When you deploy this to the server and do pip install -r requirements.txt, django-admin createproject myproject and then ./manage.py migrate, then your database will be "filled" with all Django-specific tables that has built-in (i.e auth, sessions, contenttypes etc) and are enabled in the INSTALLED_APPS setting list.
The one and only that you should deploy is everything under your project directory (requirements.txt, manage.py, robots.txt etc) and not your 3rd party apps which may live either inside your project (under a .virtualenv dir, probably) or in a separate .virtualenv dir outside of your project.
An analogous to your question is node_modules for nodejs. node_modules directory is never deployed to VCS (because it can get quite large in size etc), instead only the package.json is, because knowing what dependencies (package.json/requirements.txt) your project needs, then your project can "reconstruct" itself from zero.

A pip install to include template/static files in actually app directory (not just in the site-packages dir)

I am trying to create a pip install off of a git repo (i.e. pip install git+https://github.com/username/Test-Theme.git) that is for a theme; so it includes just static and template files.
My pip install works fine in terms of installing the static and template files in my 'site-packages' directory. And my website picks up the theme fine. I can also run a 'python manage.py collectstatic' to put all the static files from the theme directly in my project's static folder. However, that command does not create a templates folder with my templates in that folder.
So, is there a way to create a pip install where the static/template files are placed directly in the project directory as well? Or is there an equivalent to 'python manage.py collecstatic" that can collect my templates folder? So, for example, after the pip install the structure would look like this, with the static and templates files physically included in the project folder.
project
manage.py
project/
app-name/
static/
templates/
Thanks for any help on this.
UPDATE
So, using Django's startproject template feature might be the best way to get everything installed with one command: https://docs.djangoproject.com/en/1.8/ref/django-admin/
something like django-admin startproject --template=https://github.com/githubuser/django-project-template/archive/master.zip myproject...
There is no point doing this. Both the template system and the static asset system already support running directly from site-packages, without any extra configuration steps: in fact that is exactly what the built-in admin app does.
As you already know, collectstatic collects static files from directories inside each app and puts them in a central place for deployment. But for templates, you don't even need that step: the default TEMPLATE_LOADERS includes a loader that loads templates from a templates directory inside the app itself. There is absolutely no extra step required.

Duplicate the virtual environment for production

This may be a silly question, but I would still like to ask:
I am developing a project using Django, CherryPy, and Nginx. I noticed that a file requirement.txt is usually created to indicate the packages and versions installed in the development environment. Suppose the directory of the virtual environment is /home/me/project/python2Venv.
When I deploy my Django project (tango) into production, the project is copied to the production directory:
sudo cp -r /home/me/project/tango /webapps/tango
For the virtual environment, may I just copy the whole directory using the following command or I should install each of the packages into the production environment again according to requirement.txt?
sudo cp -r /home/me/project/python2Venv /webapps/tango/python2Venv
I think virtualenv uses absolute paths in some files so recreating the env and installing the packages via requirements.txt would be more safe.
In my opinion, it is recommended to install the packages with requirements.txt. Copying directory, can end up being a nightmare.
Say in Update 1:
You have 4 packages each with a specific version(pkg1-ver1, pkg2-ver1, pkg3-ver1, pkg-ver1).
In Update 2:
You have upgraded one package to its new version(pkg1-ver2). With requirements.txt you would just upgrade that one package. Instead of the copying all the packages(Although, i am not sure how well copying of the directory would work).
Hope this helps !
You should install packages with the file requirements.txt.
Or you can use virtualenvwrapper. It helps to clone virtual environments locally easily such as cpvirtualenv, rmvirtualenv, etc.

How to import a Django app from Git into a project

I want to include a Django app into the project I'm working on. The app is hosted on Github ( https://github.com/lmorchard/django-badger ). As well as the app's directory containing the goodies, there are some files in the root - README, LICENCE, TODO and setup.py. If I clone the app into my project's root directory, the app folder will be in the correct place, but those root files will be in my project's root. How can I add the app while still tracking the upstream code in Github?
I had a similar issue where I was working on two independent projects where both were in a repo, and one of them used the other as an app:
Create a virtualenv and install all dependencies for both projects. I usually like to have a virtualenv for each project/repo but in this case you need one env which can execute Python from both repos.
Clone both repos to independent location. Do not clone the depending app inside the other project. Your file-structure then might look like this (assuming Django 1.3 project layout):
project/
manage.py
project/
__init__.py
settings.py
...
...
app/
README
...
app/
__init__.py
models.py
...
And final step is to create a symlink (or shortcut on Windows) from the app directory which has __init__.py in it to the project path.
$ ln -s /abs/path/to/app/app /abs/path/to/project/
Now you can use the virtualenv to run the project!
The final result is that you have two independent repos however one of projects is using the other project without directly copying the code, hence allowing you to maintain two repos.
U can install it by running
python setup.py
or through pip
sudo pip install -e git+https://github.com/lmorchard/django-badger#egg=django-badger
Clone the repository from github using git://github.com/lmorchard/django-badger.git. Then open the cloned folder in terminal. Install the app using the command sudo python setup.py install. This will work good. If you want to have the app included in your project, create a folder named badger(or anything you wish) and copy the installed app from dist-packages to created folder.