compose/docs/django.md

92 lines
3.8 KiB
Markdown
Raw Normal View History

2014-01-27 16:03:21 +01:00
---
layout: default
title: Getting started with Compose and Django
2014-01-27 16:03:21 +01:00
---
Getting started with Compose and Django
2014-01-27 16:03:21 +01:00
===================================
Let's use Compose to set up and run a Django/PostgreSQL app. Before starting, you'll need to have [Compose installed](install.html).
2014-01-27 16:03:21 +01:00
Let's set up the three files that'll get us started. First, our app is going to be running inside a Docker container which contains all of its dependencies. We can define what goes inside that Docker container using a file called `Dockerfile`. It'll contain this to start with:
FROM python:2.7
ENV PYTHONUNBUFFERED 1
2014-01-27 16:03:21 +01:00
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
That'll install our application inside an image with Python installed alongside all of our Python dependencies. For more information on how to write Dockerfiles, see the [Docker user guide](https://docs.docker.com/userguide/dockerimages/#building-an-image-from-a-dockerfile) and the [Dockerfile reference](http://docs.docker.com/reference/builder/).
2014-01-27 16:03:21 +01:00
Second, we define our Python dependencies in a file called `requirements.txt`:
Django
psycopg2
2014-01-27 16:03:21 +01:00
Simple enough. Finally, this is all tied together with a file called `docker-compose.yml`. It describes the services that our app comprises of (a web server and database), what Docker images they use, how they link together, what volumes will be mounted inside the containers and what ports they expose.
2014-01-27 16:03:21 +01:00
db:
image: postgres
2014-01-27 16:03:21 +01:00
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
2014-01-27 16:03:21 +01:00
links:
- db
See the [`docker-compose.yml` reference](yml.html) for more information on how it works.
2014-01-27 16:03:21 +01:00
We can now start a Django project using `docker-compose run`:
2014-01-27 16:03:21 +01:00
$ docker-compose run web django-admin.py startproject composeexample .
2014-01-27 16:03:21 +01:00
First, Compose will build an image for the `web` service using the `Dockerfile`. It will then run `django-admin.py startproject composeexample .` inside a container using that image.
2014-01-27 16:03:21 +01:00
This will generate a Django app inside the current directory:
$ ls
Dockerfile docker-compose.yml composeexample manage.py requirements.txt
2014-01-27 16:03:21 +01:00
First thing we need to do is set up the database connection. Replace the `DATABASES = ...` definition in `composeexample/settings.py` to read:
2014-01-27 16:03:21 +01:00
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': 5432,
2014-01-27 16:03:21 +01:00
}
}
These settings are determined by the [postgres](https://registry.hub.docker.com/_/postgres/) Docker image we are using.
2014-01-28 01:19:38 +01:00
Then, run `docker-compose up`:
2014-01-27 16:03:21 +01:00
Recreating myapp_db_1...
Recreating myapp_web_1...
Attaching to myapp_db_1, myapp_web_1
myapp_db_1 |
myapp_db_1 | PostgreSQL stand-alone backend 9.1.11
myapp_db_1 | 2014-01-27 12:17:03 UTC LOG: database system is ready to accept connections
myapp_db_1 | 2014-01-27 12:17:03 UTC LOG: autovacuum launcher started
myapp_web_1 | Validating models...
myapp_web_1 |
myapp_web_1 | 0 errors found
myapp_web_1 | January 27, 2014 - 12:12:40
myapp_web_1 | Django version 1.6.1, using settings 'composeexample.settings'
2014-01-27 16:03:21 +01:00
myapp_web_1 | Starting development server at http://0.0.0.0:8000/
myapp_web_1 | Quit the server with CONTROL-C.
And your Django app should be running at port 8000 on your docker daemon (if you're using boot2docker, `boot2docker ip` will tell you its address).
2014-01-27 16:03:21 +01:00
You can also run management commands with Docker. To set up your database, for example, run `docker-compose up` and in another terminal run:
2014-01-27 16:03:21 +01:00
$ docker-compose run web python manage.py syncdb
2014-01-27 16:03:21 +01:00