151 lines
7.7 KiB
HTML
151 lines
7.7 KiB
HTML
<!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='http://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="http://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>
|