2015-06-07 22:59:58 +02:00
<!-- [metadata]>
+++
title = "Quickstart Guide: Compose and Django"
description = "Getting started with Docker Compose and Django"
keywords = ["documentation, docs, docker, compose, orchestration, containers"]
[menu.main]
parent="smn_workw_compose"
weight=4
+++
<![end-metadata]-->
2014-01-27 16:03:21 +01:00
2015-06-07 22:59:58 +02:00
## Quickstart Guide: Compose and Django
2014-01-27 16:03:21 +01:00
2015-02-27 03:58:06 +01:00
This Quick-start Guide will demonstrate how to use Compose to set up and run a
simple Django/PostgreSQL app. Before starting, you'll need to have
[Compose installed ](install.md ).
### Define the project
Start by setting up the three files you'll need to build the app. First, since
your app is going to run inside a Docker container containing all of its
dependencies, you'll need to define exactly what needs to be included in the
container. This is done using a file called `Dockerfile` . To begin with, the
Dockerfile consists of:
2014-01-27 16:03:21 +01:00
2014-08-07 01:30:27 +02:00
FROM python:2.7
2014-05-22 15:15:17 +02:00
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/
2015-02-27 03:58:06 +01:00
This Dockerfile will define an image that is used to build a container that
includes your application and has Python installed alongside all of your 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
2015-02-27 03:58:06 +01:00
Second, you'll define your Python dependencies in a file called
`requirements.txt` :
2014-01-27 16:03:21 +01:00
Django
2014-08-07 01:30:27 +02:00
psycopg2
2014-01-27 16:03:21 +01:00
2015-02-27 03:58:06 +01:00
Finally, this is all tied together with a file called `docker-compose.yml` . It
describes the services that comprise your app (here, a web server and database),
which 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:
2014-08-07 01:30:27 +02:00
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:
2014-02-20 13:53:43 +01:00
- "8000:8000"
2014-01-27 16:03:21 +01:00
links:
- db
2015-02-27 03:58:06 +01:00
See the [`docker-compose.yml` reference ](yml.html ) for more information on how
this file works.
2014-01-27 16:03:21 +01:00
2015-02-27 03:58:06 +01:00
### Build the project
You can now start a Django project with `docker-compose run` :
2014-01-27 16:03:21 +01:00
2015-01-20 18:44:48 +01:00
$ docker-compose run web django-admin.py startproject composeexample .
2014-01-27 16:03:21 +01:00
2015-02-27 03:58:06 +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 built using that image.
2014-01-27 16:03:21 +01:00
This will generate a Django app inside the current directory:
$ ls
2015-01-20 18:44:48 +01:00
Dockerfile docker-compose.yml composeexample manage.py requirements.txt
2014-01-27 16:03:21 +01:00
2015-02-27 03:58:06 +01:00
### Connect the database
Now you need to 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',
2014-08-07 01:30:27 +02:00
'NAME': 'postgres',
'USER': 'postgres',
2014-08-08 20:00:10 +02:00
'HOST': 'db',
2014-08-07 01:30:27 +02:00
'PORT': 5432,
2014-01-27 16:03:21 +01:00
}
}
2015-02-27 03:58:06 +01:00
These settings are determined by the
[postgres ](https://registry.hub.docker.com/_/postgres/ ) Docker image specified
in the Dockerfile.
2014-01-28 01:19:38 +01:00
2015-01-20 18:29:28 +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
2015-01-20 18:44:48 +01:00
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.
2015-02-27 03:58:06 +01:00
Your Django app should nw 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
2015-02-27 03:58:06 +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
2015-01-20 18:29:28 +01:00
$ docker-compose run web python manage.py syncdb
2014-01-27 16:03:21 +01:00
2015-02-27 03:58:06 +01:00
## More Compose documentation
2015-02-25 09:43:33 +01:00
2015-06-07 22:59:58 +02:00
- [User guide ](compose-overview.md )
2015-05-12 13:44:43 +02:00
- [Installing Compose ](install.md )
- [Get started with Django ](django.md )
- [Get started with Rails ](rails.md )
- [Get started with Wordpress ](wordpress.md )
2015-02-25 09:43:33 +01:00
- [Command line reference ](cli.md )
- [Yaml file reference ](yml.md )
- [Compose environment variables ](env.md )
- [Compose command line completion ](completion.md )