151 lines
8.3 KiB
HTML
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?20140218071035391237738">
|
|
</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're going to use Fig to set up and run a Rails/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 binaryphile/ruby:2.0.0-p247
|
|
RUN apt-get update -qq && 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'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'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 'https://rubygems.org'
|
|
gem 'rails', '4.0.2'
|
|
</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'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'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'll run <code>rails new</code> inside a new container, using that image. Once it'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've got a Javascript runtime:</p>
|
|
<div class="highlight"><pre><code class="text language-text" data-lang="text">gem 'therubyracer', platforms: :ruby
|
|
</code></pre></div>
|
|
<p>Now that we'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'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'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: &default
|
|
adapter: postgresql
|
|
encoding: unicode
|
|
database: myapp_development
|
|
pool: 5
|
|
username: docker
|
|
password: docker
|
|
host: <%= ENV.fetch('DB_1_PORT_5432_TCP_ADDR', 'localhost') %>
|
|
port: <%= ENV.fetch('DB_1_PORT_5432_TCP_PORT', '5432') %>
|
|
|
|
test:
|
|
<<: *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'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'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're using docker-osx).</p>
|
|
|
|
<p><img src="https://orchardup.com/static/images/fig-rails-screenshot.png" alt="Screenshot of Rails' 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&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://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>
|