compose/rails.html
Ben Firshman 26308ade7b update
2014-01-30 10:49:39 +00:00

151 lines
8.3 KiB
HTML

<!DOCTYPE html>
<html lang="en-gb">
<head>
<meta charset="utf-8">
<title>Getting started with Fig and Rails</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?20140130041048359961315">
</head>
<body>
<div class="container">
<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 Rails</h1>
<p>We&#39;re going to use Fig to set up and run a Rails/PostgreSQL app. Before starting, you&#39;ll need to have <a href="install.html">Fig installed</a>.</p>
<p>Let&#39;s set up the three files that&#39;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&#39;ll contain this to start with:</p>
<div class="highlight"><pre><code class="text language-text" data-lang="text">FROM binaryphile/ruby:2.0.0-p247
RUN apt-get update -qq &amp;&amp; apt-get install -y build-essential libpq-dev
RUN mkdir /myapp
WORKDIR /myapp
ADD Gemfile /myapp/Gemfile
RUN bundle install
ADD . /myapp
</code></pre></div>
<p>That&#39;ll put our application code inside an image with Ruby, Bundler and all our dependencies. For more information on how to write Dockerfiles, see the <a href="https://www.docker.io/learn/dockerfile/">Dockerfile tutorial</a> and the <a href="http://docs.docker.io/en/latest/use/builder/">Dockerfile reference</a>.</p>
<p>Next, we have a bootstrap <code>Gemfile</code> which just loads Rails. It&#39;ll be overwritten in a moment by <code>rails new</code>.</p>
<div class="highlight"><pre><code class="text language-text" data-lang="text">source &#39;https://rubygems.org&#39;
gem &#39;rails&#39;, &#39;4.0.2&#39;
</code></pre></div>
<p>Finally, <code>fig.yml</code> is where the magic happens. It describes what services our app comprises (a database and a web app), how to get each one&#39;s Docker image (the database just runs on a pre-made PostgreSQL image, and the web app is built from the current directory), and the configuration we need to link them together and expose the web app&#39;s port.</p>
<div class="highlight"><pre><code class="text language-text" data-lang="text">db:
image: orchardup/postgresql
ports:
- 5432
web:
build: .
command: bundle exec rackup -p 3000
volumes:
- .:/myapp
ports:
- 3000:3000
links:
- db
</code></pre></div>
<p>With those files in place, we can now generate the Rails skeleton app using <code>fig run</code>:</p>
<div class="highlight"><pre><code class="text language-text" data-lang="text">$ fig run web rails new . --force --database=postgresql --skip-bundle
</code></pre></div>
<p>First, Fig will build the image for the <code>web</code> service using the <code>Dockerfile</code>. Then it&#39;ll run <code>rails new</code> inside a new container, using that image. Once it&#39;s done, you should have a fresh app generated:</p>
<div class="highlight"><pre><code class="text language-text" data-lang="text">$ ls
Dockerfile app fig.yml tmp
Gemfile bin lib vendor
Gemfile.lock config log
README.rdoc config.ru public
Rakefile db test
</code></pre></div>
<p>Uncomment the line in your new <code>Gemfile</code> which loads <code>therubyracer</code>, so we&#39;ve got a Javascript runtime:</p>
<div class="highlight"><pre><code class="text language-text" data-lang="text">gem &#39;therubyracer&#39;, platforms: :ruby
</code></pre></div>
<p>Now that we&#39;ve got a new <code>Gemfile</code>, we need to build the image again. (This, and changes to the Dockerfile itself, should be the only times you&#39;ll need to rebuild).</p>
<div class="highlight"><pre><code class="text language-text" data-lang="text">$ fig build
</code></pre></div>
<p>The app is now bootable, but we&#39;re not quite there yet. By default, Rails expects a database to be running on <code>localhost</code> - we need to point it at the <code>db</code> container instead. We also need to change the username and password to align with the defaults set by <code>orchardup/postgresql</code>.</p>
<p>Open up your newly-generated <code>database.yml</code>. Replace its contents with the following:</p>
<div class="highlight"><pre><code class="text language-text" data-lang="text">development: &amp;default
adapter: postgresql
encoding: unicode
database: myapp_development
pool: 5
username: docker
password: docker
host: &lt;%= ENV.fetch(&#39;DB_1_PORT_5432_TCP_ADDR&#39;, &#39;localhost&#39;) %&gt;
port: &lt;%= ENV.fetch(&#39;DB_1_PORT_5432_TCP_PORT&#39;, &#39;5432&#39;) %&gt;
test:
&lt;&lt;: *default
database: myapp_test
</code></pre></div>
<p>We can now boot the app.</p>
<div class="highlight"><pre><code class="text language-text" data-lang="text">$ fig up
</code></pre></div>
<p>If all&#39;s well, you should see some PostgreSQL output, and then—after a few seconds—the familiar refrain:</p>
<div class="highlight"><pre><code class="text language-text" data-lang="text">myapp_web_1 | [2014-01-17 17:16:29] INFO WEBrick 1.3.1
myapp_web_1 | [2014-01-17 17:16:29] INFO ruby 2.0.0 (2013-11-22) [x86_64-linux-gnu]
myapp_web_1 | [2014-01-17 17:16:29] INFO WEBrick::HTTPServer#start: pid=1 port=3000
</code></pre></div>
<p>Finally, we just need to create the database. In another terminal, run:</p>
<div class="highlight"><pre><code class="text language-text" data-lang="text">$ fig run web rake db:create
</code></pre></div>
<p>And we&#39;re rolling—see for yourself at <a href="http://localhost:3000">localhost:3000</a> (or <a href="http://localdocker:3000">localdocker:3000</a> if you&#39;re using docker-osx).</p>
<p><img src="https://orchardup.com/static/images/fig-rails-screenshot.png" alt="Screenshot of Rails&#39; stock index.html"></p>
</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/orchardup/fig">Fig on GitHub</a></li>
<li><a href="https://twitter.com/orchardup">Follow us on Twitter</a></li>
<li><a href="http://webchat.freenode.net/?channels=%23orchardup&uio=d4">#orchardup on Freenode</a></li>
</ul>
<div class="badges">
<iframe src="http://ghbtns.com/github-btn.html?user=orchardup&amp;repo=fig&amp;type=watch&amp;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://orchardup.github.io/fig/">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', 'orchardup.github.io');
ga('send', 'pageview');
</script>
</body>
</html>