2016-06-13 18:40:48 +03:00
Getting Up and Running Locally With Docker
==========================================
2015-09-18 19:22:55 +03:00
2015-09-19 00:26:29 +03:00
.. index :: Docker
The steps below will get you up and running with a local development environment.
2015-10-04 02:21:08 +03:00
All of these commands assume you are in the root of your generated project.
2015-09-19 00:26:29 +03:00
Prerequisites
2016-03-08 12:07:48 +03:00
-------------
2015-09-19 00:26:29 +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:
2015-09-19 00:26:29 +03:00
2016-09-12 16:53:21 +03:00
- On Mac OS X, you'll need `Docker for Mac`_
- On Windows, you'll need `Docker for Windows`_
2016-03-08 12:07:48 +03:00
- On Linux, you'll need `docker-engine`_
2017-06-20 23:29:56 +03:00
2016-09-12 16:53:21 +03:00
.. _`Docker for Mac`: https://docs.docker.com/engine/installation/mac/
.. _`Docker for Windows`: https://docs.docker.com/engine/installation/windows/
2016-03-08 12:07:48 +03:00
.. _`docker-engine`: https://docs.docker.com/engine/installation/
2015-09-19 00:26:29 +03:00
2016-11-02 12:12:06 +03:00
Attention Windows users
2017-06-20 23:29:56 +03:00
-----------------------
2016-11-02 12:12:06 +03:00
Currently PostgreSQL (`` psycopg2 `` python package) is not installed inside Docker containers for Windows users, while it is required by the generated Django project. To fix this, add `` psycopg2 `` to the list of requirements inside `` requirements/base.txt `` ::
# Python-PostgreSQL Database Adapter
psycopg2==2.6.2
Doing this will prevent the project from being installed in an Windows-only environment (thus without usage of Docker). If you want to use this project without Docker, make sure to remove `` psycopg2 `` from the requirements again.
2015-09-19 00:26:29 +03:00
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::
2015-09-27 20:42:21 +03:00
2017-07-14 17:09:41 +03:00
$ docker-compose -f local.yml build
2016-03-08 12:07:48 +03:00
2017-07-31 13:16:38 +03:00
If you want to build the production environment you use `` production.yml `` as -f argument (`` docker-compose.yml `` or `` docker-compose.yaml `` are the defaults).
2015-09-19 00:26:29 +03:00
2015-10-04 02:35:48 +03:00
Boot the System
---------------
2016-03-08 12:07:48 +03:00
This brings up both Django and PostgreSQL.
2015-09-19 00:26:29 +03:00
2016-03-08 12:07:48 +03:00
The first time it is run it might take a while to get started, but subsequent
2015-10-04 02:35:48 +03:00
runs will occur quickly.
2015-09-18 19:22:55 +03:00
Open a terminal at the project root and run the following for local development::
2017-07-14 17:09:41 +03:00
$ docker-compose -f local.yml up
2015-09-18 19:22:55 +03:00
2017-07-14 17:09:41 +03:00
You can also set the environment variable `` COMPOSE_FILE `` pointing to `` local.yml `` like this::
2015-09-18 19:22:55 +03:00
2017-07-14 17:09:41 +03:00
$ export COMPOSE_FILE=local.yml
2015-09-18 19:22:55 +03:00
And then run::
2017-07-31 13:27:58 +03:00
$ 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
2017-07-31 13:27:58 +03:00
using the `` docker-compose -f local.yml run `` command.
2015-09-18 19:22:55 +03:00
To migrate your app and to create a superuser, run::
2017-07-14 17:09:41 +03:00
$ docker-compose -f local.yml run django python manage.py migrate
$ docker-compose -f local.yml run django python manage.py createsuperuser
2015-10-04 02:21:08 +03:00
2015-10-04 02:44:43 +03:00
Here we specify the `` django `` container as the location to run our management commands.
2017-02-13 23:27:09 +03:00
Add your Docker development server IP
2017-06-20 23:29:56 +03:00
-------------------------------------
2017-02-13 23:27:09 +03:00
When `` DEBUG `` is set to `True` , the host is validated against `` ['localhost', '127.0.0.1', '[::1]'] `` . This is adequate when running a `` virtualenv `` . For Docker, in the `` config.settings.local `` , add your host development server IP to `` INTERNAL_IPS `` or `` ALLOWED_HOSTS `` if the variable exists.
2015-10-04 02:35:48 +03:00
Production Mode
2016-03-08 12:07:48 +03:00
~~~~~~~~~~~~~~~
2015-10-04 02:35:48 +03:00
2017-07-14 17:09:41 +03:00
Instead of using `local.yml` , you would use `production.yml` .
2015-10-04 02:21:08 +03:00
Other Useful Tips
2016-03-08 12:07:48 +03:00
-----------------
2015-10-04 02:21:08 +03:00
Make a machine the active unit
2016-03-08 12:07:48 +03:00
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2015-10-04 02:21:08 +03:00
2016-03-08 12:07:48 +03:00
This tells our computer that all future commands are specifically for the dev1 machine.
2015-10-04 02:21:08 +03:00
Using the `` eval `` command we can switch machines as needed.
::
$ eval "$(docker-machine env dev1)"
2015-10-04 02:35:48 +03:00
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 02:35:48 +03:00
2015-10-04 22:09:18 +03:00
::
2017-07-14 17:09:41 +03:00
$ docker-compose -f local.yml up -d
2016-06-03 16:04:16 +03:00
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:
::
2017-07-14 17:09:41 +03:00
$ docker-compose -f local.yml run --service-ports django
2016-06-03 22:50:23 +03:00
2016-06-03 16:04:16 +03:00
django-debug-toolbar
""""""""""""""""""""
2017-06-20 23:29:56 +03:00
In order for django-debug-toolbar to work with docker you need to add your docker-machine ip address to `` INTERNAL_IPS `` in `` local.py ``
2016-06-03 16:04:16 +03:00
.. 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-03 16:04:16 +03:00
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:
2016-06-13 15:56:12 +03:00
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/