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
2018-03-08 15:56:15 +03:00
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
2018-03-08 15:56:15 +03:00
* Docker; if you don't have it yet, follow the `installation instructions`_ ;
* Docker Compose; refer to the official documentation for the `installation guide`_ .
2015-09-19 00:26:29 +03:00
2018-03-08 15:56:15 +03:00
.. _`installation instructions`: https://docs.docker.com/install/#supported-platforms
.. _`installation guide`: https://docs.docker.com/compose/install/
2017-06-20 23:29:56 +03:00
2015-09-19 00:26:29 +03:00
2018-03-08 15:56:15 +03:00
Attention, Windows Users
------------------------
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.
2018-03-08 15:56:15 +03:00
2015-09-19 00:26:29 +03:00
Build the Stack
---------------
2018-03-08 15:56:15 +03:00
This can take a while, especially the first time you run this particular command 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
2018-03-08 15:56:15 +03:00
Generally, if you want to emulate production environment use `` production.yml `` instead. And this is true for any other actions you might need to perform: whenever a switch is required, just do it!
2015-09-19 00:26:29 +03:00
2015-10-04 02:35:48 +03:00
2018-03-08 15:56:15 +03:00
Run the Stack
-------------
2015-09-19 00:26:29 +03:00
2018-03-08 15:56:15 +03:00
This brings up both Django and PostgreSQL. The first time it is run it might take a while to get started, but subsequent 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
2018-03-08 15:56:15 +03:00
To run in a detached (background) mode, just::
2015-10-04 02:44:43 +03:00
2018-03-08 15:56:15 +03:00
$ docker-compose up -d
2015-09-18 19:22:55 +03:00
2018-03-08 15:56:15 +03:00
Execute Management Commands
---------------------------
2015-10-04 02:21:08 +03:00
2018-03-08 15:56:15 +03:00
As with any shell command that we wish to run in our container, this is done using the `` docker-compose -f local.yml run --rm `` command: ::
2015-10-04 02:44:43 +03:00
2018-03-08 15:56:15 +03:00
$ docker-compose -f local.yml run --rm django python manage.py migrate
$ docker-compose -f local.yml run --rm django python manage.py createsuperuser
2017-02-13 23:27:09 +03:00
2018-03-08 15:56:15 +03:00
Here, `` django `` is the target service we are executing the commands against.
2017-02-13 23:27:09 +03:00
2015-10-04 02:35:48 +03:00
2018-03-08 15:56:15 +03:00
(Optionally) Designate your Docker Development Server IP
--------------------------------------------------------
2015-10-04 02:21:08 +03:00
2018-03-08 15:56:15 +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:21:08 +03:00
2018-03-09 21:17:56 +03:00
.. _envs:
2018-03-08 15:56:15 +03:00
Configuring the Environment
---------------------------
2015-10-04 02:21:08 +03:00
2018-03-08 15:56:15 +03:00
This is the excerpt from your project's `` local.yml `` : ::
2015-10-04 02:21:08 +03:00
2018-03-08 15:56:15 +03:00
# ...
2015-10-04 02:35:48 +03:00
2018-03-08 15:56:15 +03:00
postgres:
build:
context: .
dockerfile: ./compose/production/postgres/Dockerfile
volumes:
- postgres_data_local:/var/lib/postgresql/data
- postgres_backup_local:/backups
env_file:
- ./.envs/.local/.postgres
2015-10-04 02:35:48 +03:00
2018-03-08 15:56:15 +03:00
# ...
2015-10-04 02:35:48 +03:00
2018-03-08 15:56:15 +03:00
The most important thing for us here now is `` env_file `` section enlisting `` ./.envs/.local/.postgres `` . Generally, the stack's behavior is governed by a number of environment variables (`env(s)` , for short) residing in `` envs/ `` , for instance, this is what we generate for you: ::
2015-10-04 22:09:18 +03:00
2018-03-08 15:56:15 +03:00
.envs
├── .local
│ ├── .django
│ └── .postgres
└── .production
├── .caddy
├── .django
└── .postgres
2016-06-03 16:04:16 +03:00
2018-03-08 15:56:15 +03:00
By convention, for any service `` sI `` in environment `` e `` (you know `` someenv `` is an environment when there is a `` someenv.yml `` file in the project root), given `` sI `` requires configuration, a `` .envs/.e/.sI `` `service configuration` file exists.
2016-06-03 16:04:16 +03:00
2018-03-08 15:56:15 +03:00
Consider the aforementioned `` .envs/.local/.postgres `` : ::
2016-06-03 16:04:16 +03:00
2018-03-08 15:56:15 +03:00
# PostgreSQL
# ------------------------------------------------------------------------------
2018-05-09 12:49:39 +03:00
POSTGRES_HOST=postgres
2018-03-08 16:59:41 +03:00
POSTGRES_DB=<your project slug>
2018-03-08 15:56:15 +03:00
POSTGRES_USER=XgOWtQtJecsAbaIyslwGvFvPawftNaqO
POSTGRES_PASSWORD=jSljDz4whHuwO3aJIgVBrqEml5Ycbghorep4uVJ4xjDYQu0LfuTZdctj7y0YcCLu
2016-06-03 16:04:16 +03:00
2018-03-08 16:59:41 +03:00
The three envs we are presented with here are `` POSTGRES_DB `` , `` POSTGRES_USER `` , and `` POSTGRES_PASSWORD `` (by the way, their values have also been generated for you). You might have figured out already where these definitions will end up; it's all the same with `` django `` and `` caddy `` service container envs.
2016-06-03 16:04:16 +03:00
2018-03-09 13:16:47 +03:00
One final touch: should you ever need to merge `` .envs/production/* `` in a single `` .env `` run the `` merge_production_dotenvs_in_dotenv.py `` : ::
$ python merge_production_dotenvs_in_dotenv.py
The `` .env `` file will then be created, with all your production envs residing beside each other.
2016-06-03 16:04:16 +03:00
2018-03-08 15:56:15 +03:00
Tips & Tricks
-------------
Activate a Docker Machine
~~~~~~~~~~~~~~~~~~~~~~~~~
2016-06-03 16:04:16 +03:00
2018-03-08 15:56:15 +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.::
2016-06-03 16:04:16 +03:00
2018-03-08 15:56:15 +03:00
$ eval "$(docker-machine env dev1)"
2016-06-03 22:50:23 +03:00
2018-03-08 15:56:15 +03:00
Debugging
~~~~~~~~~
2016-06-03 16:04:16 +03:00
2018-03-08 15:56:15 +03:00
ipdb
"""""
If you are using the following within your code to debug: ::
2016-06-03 16:04:16 +03:00
2018-03-08 15:56:15 +03:00
import ipdb; ipdb.set_trace()
2016-06-03 16:04:16 +03:00
2018-03-08 15:56:15 +03:00
Then you may need to run the following for it to work as desired: ::
2016-06-03 16:04:16 +03:00
2018-03-08 15:56:15 +03:00
$ docker-compose -f local.yml run --rm --service-ports django
2016-06-03 16:04:16 +03:00
2018-03-08 15:56:15 +03:00
django-debug-toolbar
""""""""""""""""""""
2016-06-03 16:04:16 +03:00
2018-03-08 15:56:15 +03:00
In order for `` django-debug-toolbar `` to work designate your Docker Machine IP with `` INTERNAL_IPS `` in `` local.py `` .
2016-06-03 16:04:16 +03:00
2018-03-08 15:56:15 +03:00
Mailhog
~~~~~~~
2016-06-03 16:04:16 +03:00
2018-03-08 15:56:15 +03:00
When developing locally you can go with MailHog_ for email testing provided `` use_mailhog `` was set to `` y `` on setup. To proceed,
2016-06-03 16:04:16 +03:00
2018-03-08 15:56:15 +03:00
#. make sure `` mailhog `` container is up and running;
2016-06-13 15:56:12 +03:00
2018-03-08 15:56:15 +03:00
#. open up `` http://127.0.0.1:8025 `` .
2016-06-13 15:56:12 +03:00
.. _Mailhog: https://github.com/mailhog/MailHog/