-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdjango.html
151 lines (136 loc) · 7.69 KB
/
django.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<!DOCTYPE html>
<html lang="en-gb">
<head>
<meta charset="utf-8">
<title>Getting started with Fig and Django</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href='https://fonts.googleapis.com/css?family=Lilita+One|Lato:300,400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/fig.css?20150226081472935746419">
<link rel="canonical" href="http://www.fig.sh/django.html">
</head>
<body>
<div class="container">
<nav class="deprecation">
<p>Fig has been replaced by Docker Compose, and is now deprecated. The new documentation is on the <a href="http://docs.docker.com/compose/django">Docker website</a>.</p>
</nav>
<div class="logo mobile-logo">
<a href="index.html">
<img src="img/logo.png">
Fig
</a>
</div>
<div class="content"><h1>Getting started with Fig and Django</h1>
<p>Let's use Fig to set up and run a Django/PostgreSQL app. Before starting, you'll need to have <a href="install.html">Fig installed</a>.</p>
<p>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 <code>Dockerfile</code>. It'll contain this to start with:</p>
<div class="highlight"><pre><code class="text language-text" data-lang="text">FROM python:2.7
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
</code></pre></div>
<p>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 <a href="https://docs.docker.com/userguide/dockerimages/#building-an-image-from-a-dockerfile">Docker user guide</a> and the <a href="http://docs.docker.com/reference/builder/">Dockerfile reference</a>.</p>
<p>Second, we define our Python dependencies in a file called <code>requirements.txt</code>:</p>
<div class="highlight"><pre><code class="text language-text" data-lang="text">Django
psycopg2
</code></pre></div>
<p>Simple enough. Finally, this is all tied together with a file called <code>fig.yml</code>. 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.</p>
<div class="highlight"><pre><code class="text language-text" data-lang="text">db:
image: postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
links:
- db
</code></pre></div>
<p>See the <a href="yml.html"><code>fig.yml</code> reference</a> for more information on how it works.</p>
<p>We can now start a Django project using <code>fig run</code>:</p>
<div class="highlight"><pre><code class="text language-text" data-lang="text">$ fig run web django-admin.py startproject figexample .
</code></pre></div>
<p>First, Fig will build an image for the <code>web</code> service using the <code>Dockerfile</code>. It will then run <code>django-admin.py startproject figexample .</code> inside a container using that image.</p>
<p>This will generate a Django app inside the current directory:</p>
<div class="highlight"><pre><code class="text language-text" data-lang="text">$ ls
Dockerfile fig.yml figexample manage.py requirements.txt
</code></pre></div>
<p>First thing we need to do is set up the database connection. Replace the <code>DATABASES = ...</code> definition in <code>figexample/settings.py</code> to read:</p>
<div class="highlight"><pre><code class="text language-text" data-lang="text">DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}
</code></pre></div>
<p>These settings are determined by the <a href="https://registry.hub.docker.com/_/postgres/">postgres</a> Docker image we are using.</p>
<p>Then, run <code>fig up</code>:</p>
<div class="highlight"><pre><code class="text language-text" data-lang="text">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 'figexample.settings'
myapp_web_1 | Starting development server at http://0.0.0.0:8000/
myapp_web_1 | Quit the server with CONTROL-C.
</code></pre></div>
<p>And your Django app should be running at port 8000 on your docker daemon (if you're using boot2docker, <code>boot2docker ip</code> will tell you its address).</p>
<p>You can also run management commands with Docker. To set up your database, for example, run <code>fig up</code> and in another terminal run:</p>
<div class="highlight"><pre><code class="text language-text" data-lang="text">$ fig run web python manage.py syncdb
</code></pre></div></div>
<div class="sidebar">
<h1 class="logo">
<a href="index.html">
<img src="img/logo.png">
Fig
</a>
</h1>
<ul class="nav">
<li><a href="index.html">Home</a></li>
<li><a href="install.html">Install</a></li>
<li><a href="rails.html">Get started with Rails</a></li>
<li><a href="django.html">Get started with Django</a></li>
<li><a href="wordpress.html">Get started with Wordpress</a></li>
</ul>
<ul class="nav">
<li>Reference:</li>
<ul>
<li><a href="yml.html">fig.yml</a></li>
<li><a href="cli.html">Commands</a></li>
<li><a href="env.html">Environment variables</a></li>
</ul>
</ul>
<ul class="nav">
<li><a href="https://github.com/docker/fig">Fig on GitHub</a></li>
<li><a href="http://webchat.freenode.net/?channels=%23docker-fig&uio=d4">#docker-fig on Freenode</a></li>
</ul>
<p>Fig is a project from <a href="https://www.docker.com">Docker</a>.</p>
<div class="badges">
<iframe src="https://ghbtns.com/github-btn.html?user=docker&repo=fig&type=watch&count=true" allowtransparency="true" frameborder="0" scrolling="0" width="100" height="20"></iframe>
<a href="https://twitter.com/share" class="twitter-share-button" data-url="http://www.fig.sh/">Tweet</a>
</div>
</div>
</div>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-43996733-3', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>