cookiecutter-django/docs/developing-locally-docker.rst

166 lines
4.4 KiB
ReStructuredText
Raw Normal View History

2016-06-13 18:40:48 +03:00
Getting Up and Running Locally With Docker
==========================================
.. index:: Docker
The steps below will get you up and running with a local development environment.
All of these commands assume you are in the root of your generated project.
Prerequisites
2016-03-08 12:07:48 +03:00
-------------
2016-06-13 18:40:48 +03:00
You'll need at least Docker 1.10.
2015-10-04 02:59:52 +03:00
2016-03-08 12:07:48 +03:00
If you don't already have it installed, follow the instructions for your OS:
2016-03-08 12:07:48 +03:00
- On Mac OS X/Windows, you'll need `Docker Toolbox`_
- On Linux, you'll need `docker-engine`_
2015-10-04 02:59:52 +03:00
.. _`Docker Toolbox`: https://github.com/docker/toolbox/releases
2016-03-08 12:07:48 +03:00
.. _`docker-engine`: https://docs.docker.com/engine/installation/
2015-10-04 02:54:01 +03:00
Create the Machine (Optional)
2016-03-08 12:07:48 +03:00
-----------------------------
2016-03-08 12:07:48 +03:00
On Linux you have native Docker, so you don't need to create a VM with
2015-10-04 02:54:01 +03:00
docker-machine to use it.
2016-03-08 12:07:48 +03:00
However, on Mac/Windows/other systems without native Docker, you'll want to
2015-10-04 02:54:01 +03:00
start by creating a VM with docker-machine::
$ docker-machine create --driver virtualbox dev1
**Note:** If you want to have more than one docker development environment, then
name them accordingly. Instead of 'dev1' you might have 'dev2', 'myproject',
'djangopackages', et al.
Get the IP Address
2016-03-08 12:07:48 +03:00
------------------
2015-10-04 02:56:53 +03:00
Once your machine is up and running, run this::
$ docker-machine ip dev1
123.456.789.012
2015-10-04 02:56:53 +03:00
This is also the IP address where the Django project will be served from.
Build the Stack
---------------
This can take a while, especially the first time you run this particular command
2015-10-04 02:44:43 +03:00
on your development system::
$ docker-compose -f dev.yml build
2016-03-08 12:07:48 +03:00
If you want to build the production environment you don't have to pass an argument -f, it will automatically use docker-compose.yml.
Boot the System
---------------
2016-03-08 12:07:48 +03:00
This brings up both Django and PostgreSQL.
2016-03-08 12:07:48 +03:00
The first time it is run it might take a while to get started, but subsequent
runs will occur quickly.
Open a terminal at the project root and run the following for local development::
$ docker-compose -f dev.yml up
You can also set the environment variable ``COMPOSE_FILE`` pointing to ``dev.yml`` like this::
$ export COMPOSE_FILE=dev.yml
And then run::
$ docker-compose up
2016-03-08 12:07:48 +03:00
2015-10-04 02:44:43 +03:00
Running management commands
2016-03-08 12:07:48 +03:00
~~~~~~~~~~~~~~~~~~~~~~~~~~~
2015-10-04 02:44:43 +03:00
2016-03-08 12:07:48 +03:00
As with any shell command that we wish to run in our container, this is done
using the ``docker-compose run`` command.
To migrate your app and to create a superuser, run::
2016-01-04 13:52:26 +03:00
$ docker-compose -f dev.yml run django python manage.py migrate
$ docker-compose -f dev.yml run django python manage.py createsuperuser
2015-10-04 02:44:43 +03:00
Here we specify the ``django`` container as the location to run our management commands.
Production Mode
2016-03-08 12:07:48 +03:00
~~~~~~~~~~~~~~~
Instead of using `dev.yml`, you would use `docker-compose.yml`.
Other Useful Tips
2016-03-08 12:07:48 +03:00
-----------------
Make a machine the active unit
2016-03-08 12:07:48 +03:00
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2016-03-08 12:07:48 +03:00
This tells our computer that all future commands are specifically for the dev1 machine.
Using the ``eval`` command we can switch machines as needed.
::
$ eval "$(docker-machine env dev1)"
Detached Mode
~~~~~~~~~~~~~
2015-10-04 22:09:18 +03:00
If you want to run the stack in detached mode (in the background), use the ``-d`` argument:
2015-10-04 22:09:18 +03:00
::
2016-01-04 13:52:26 +03:00
$ docker-compose -f dev.yml up -d
Debugging
~~~~~~~~~~~~~
ipdb
"""""
If you are using the following within your code to debug:
::
import ipdb; ipdb.set_trace()
Then you may need to run the following for it to work as desired:
::
$ docker-compose run -f dev.yml --service-ports django
django-debug-toolbar
""""""""""""""""""""
In order for django-debug-toolbar to work with docker you need to add your docker-machine ip address (the output of `Get the IP ADDRESS`_) to INTERNAL_IPS in local.py
.. May be a better place to put this, as it is not Docker specific.
You may need to add the following to your css in order for the django-debug-toolbar to be visible (this applies whether Docker is being used or not):
.. code-block:: css
/* Override Bootstrap 4 styling on Django Debug Toolbar */
#djDebug[hidden], #djDebug [hidden] {
display: block !important;
}
#djDebug [hidden][style='display: none;'] {
display: none !important;
}
2016-06-13 18:40:48 +03:00
Using the Mailhog Docker Container
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2016-06-13 18:40:48 +03:00
In development you can (optionally) use MailHog_ for email testing. If you selected `use_docker`, MailHog is added as a Docker container. To use MailHog:
1. Make sure, that ``mailhog`` docker container is up and running
2. Open your browser and go to ``http://127.0.0.1:8025``
.. _Mailhog: https://github.com/mailhog/MailHog/