Skip to content

Commit

Permalink
Merge pull request #567 from FJNR-inc/documentation-add_docker_docume…
Browse files Browse the repository at this point in the history
…ntation

Documentation add docker documentation
  • Loading branch information
RignonNoel authored Jul 8, 2020
2 parents 2cb2719 + 2290c55 commit 846a727
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .env.docker
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ DATABASE_URL=postgres://root:my_password@db:5432/blitz
## GENERAL SETTINGS
DEBUG=True
SECRET_KEY=NSH1FNjqRuZCZA2MxM7pNSH1FNjqRuZCZA2MxM7p
ALLOWED_HOSTS=127.0.0.1, localhost,
ALLOWED_HOSTS=127.0.0.1, localhost, 0.0.0.0
#ADMINS=("You", "[email protected]"),
MAILCHIMP_API_KEY=00000000000000000000000000000000-aa00 #fake key
MAILCHIMP_SUBSCRIBE_LIST_ID=1
Expand Down
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ RUN mkdir -p /opt/project

WORKDIR /opt/project

CMD ["bash"]
EXPOSE 8000

# Run the production server
CMD newrelic-admin run-program gunicorn --bind 0.0.0.0:$PORT --access-logfile - Blitz-API.wsgi:application
7 changes: 5 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@ services:
- 5432:5432

api:
restart: always
build:
context: .
env_file: .env.docker
volumes:
- .:/opt/project
- /opt/project/src
- ~/.aws/:/root/.aws
command: >
bash -c "python wait_for_postgres.py &&
./manage.py migrate &&
./manage.py runserver 0.0.0.0:8000"
depends_on:
- db
# See dockerfile for runserver command
command: python -u manage.py runserver 0.0.0.0:8000
ports:
- 8000:8000

Expand Down
38 changes: 32 additions & 6 deletions docs/getting_started/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,52 @@

## Initialize the project

Start the dev server for local development:
One command only will allow you to initialize the database, run the migration on it, configure you API,
launch your documentation website and allow you to begin use the project:

```bash
docker-compose up
```

!!! warning
This step can take some time on first launch but you should see periodic message to inform you that the work
is in progress. Breathe in, breathe out, have a coffee and come back in 2-3 minutes

## Create super admin

!!! danger
This section is not documented and any contribution on this part of the documentation is welcome
Since you don't have user the first time you launch the API you will need to execute this command in a separate
terminal.

```bash
docker-compose run api python manage.py createsuperuser
```

## Initial configurations
!!! tip
If you try to understand this command you can see that we ask docker-compose to `run` a command on the `api` image
that represent our API in the `docker-compose.yml`. The command is `python manage.py createsuperuser`, the
default Django command to create a new superuser. With this technique you can execute other Django commands like
`migrate`, `makemigrations`, `makemessages` or even custom command you create to automatise your tasks.

## Configurations

If you want to modify some settings of your Docker image you can just overwrite your `.env.docker` file with the
ENV variable that you want. Differents Django settings are based on ENV variables as you can see
in `blitz_api/settings.py`

!!! danger
This section is not documented and any contribution on this part of the documentation is welcome
Take care to not push your confidentials credentials when you push code to our repository or on your open-source
forks. You can review your change by using `git diff` before committing your change.

!!! danger
Try to always use `python-decouple` to manage your settings and to not directly edit the `settings.py`.

## Using the services

You can now visit these links to validate the installation:

- The root of the API: [http://localhost:8000/](http://localhost:8000/)
- The root of the API (will need authentication as a first step): [http://localhost:8000/](http://localhost:8000/)
- The admin site: [http://localhost:8000/admin/](http://localhost:8000/admin/)
- The documentation you're reading: [http://localhost:8001/](http://localhost:8001/)

Do not hesitate to check the API section of this documentation to know how to authenticate yourself with the API
and begin asking queries.
41 changes: 41 additions & 0 deletions wait_for_postgres.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import os
import logging
from time import time, sleep
import psycopg2
check_timeout = os.getenv("POSTGRES_CHECK_TIMEOUT", 30)
check_interval = os.getenv("POSTGRES_CHECK_INTERVAL", 1)
interval_unit = "second" if check_interval == 1 else "seconds"
config = {
"dbname": os.getenv("POSTGRES_DB", "postgres"),
"user": os.getenv("POSTGRES_USER", "postgres"),
"password": os.getenv("POSTGRES_PASSWORD", ""),
"host": os.getenv("DATABASE_URL", "postgres")
}

start_time = time()
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler())


def pg_isready(host, user, password, dbname):
while time() - start_time < check_timeout:
try:
conn = psycopg2.connect(**vars())
logger.info("Postgres is ready! ✨ 💅")
conn.close()
return True
except psycopg2.OperationalError:
logger.info(
f"Postgres isn't ready. Waiting for {check_interval} "
f"{interval_unit}..."
)
sleep(check_interval)

logger.error(
f"We could not connect to Postgres within {check_timeout} seconds."
)
return False


pg_isready(**config)

0 comments on commit 846a727

Please sign in to comment.