Skip to content

Instantly share code, notes, and snippets.

@ParkinT
Last active August 29, 2015 14:09
Show Gist options
  • Save ParkinT/7c1c5312d5e3d1161ed6 to your computer and use it in GitHub Desktop.
Save ParkinT/7c1c5312d5e3d1161ed6 to your computer and use it in GitHub Desktop.
An Implementation of Docker for Serial Rails Development and Deployment.
development:
adapter: postgresql
database: railsapp_development
username: docker
host: <%= ENV['DB_PORT_5432_TCP_ADDR'] %>
port: 5432
template: template0
pool: 5
timeout: 5000
test:
adapter: postgresql
database: railsapp_test
username: docker
host: <%= ENV['DB_PORT_5432_TCP_ADDR'] %>
port: 5432
template: template0
pool: 5
timeout: 5000
production:
adapter: postgresql
database: railsapp_production
username: docker
host: <%= ENV['DB_PORT_5432_TCP_ADDR'] %>
port: 5432
template: template0
pool: 5
timeout: 5000
FROM ubuntu:14.10
MAINTAINER Thom Parkin, [email protected]
# Rails with Nginx and Passenger - Read the article on RubySource.com
# Update local OS
RUN apt-get update
RUN apt-get upgrade -y
# Install dependencies
RUN apt-get install -y git-core wget curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties
## Install Ruby 2.1.3
RUN mkdir ~/ruby
WORKDIR ~/ruby
RUN wget http://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.3.tar.gz
RUN tar -xzvf ruby-2.1.3.tar.gz
WORKDIR ruby-2.1.3
RUN ./configure
RUN make
RUN make install
RUN rm -rf ~/ruby && ruby -v
RUN echo "gem: --no-ri --no-rdoc" > ~/.gemrc
RUN gem install bundler rails --no-ri --no-rdoc
WORKDIR ~/
# Add a default Rails application to use for "smoke test"
ADD ./ /opt/
WORKDIR /opt/dockerapp
ENV RAILS_ENV production
RUN bundle install
# Install Phusion's PGP key to verify packages
RUN gpg --keyserver keyserver.ubuntu.com --recv-keys 561F9B9CAC40B2F7
RUN gpg --armor --export 561F9B9CAC40B2F7 | sudo apt-key add -
# Add HTTPS support to APT
RUN apt-get install apt-transport-https
# Add the passenger repository
RUN sh -c "echo 'deb https://oss-binaries.phusionpassenger.com/apt/passenger trusty main' >> /etc/apt/sources.list.d/passenger.list"
RUN chown root: /etc/apt/sources.list.d/passenger.list
RUN chmod 600 /etc/apt/sources.list.d/passenger.list
RUN apt-get update
# Install nginx and passenger
RUN apt-get install -y nginx-full passenger
RUN service nginx start
RUN sed -i 's/# passenger_root /passenger_root /' /etc/nginx/nginx.conf
RUN sed -i 's/# passenger_ruby.*;/passenger_ruby \/usr\/local\/bin\/ruby;/' /etc/nginx/nginx.conf
# nginx configuration
ADD nginx.conf /etc/nginx/sites-enabled/default
# Restart nginx service
CMD sudo service nginx restart
EXPOSE 80
FROM rails_passenger_nginx:1.0
MAINTAINER Thom Parkin, [email protected]
# Add Rails app
ADD . /opt/
ENV RAILS_ENV production
# bundle install
WORKDIR /opt/YOUR_RAILS_APPLICATION
RUN bash -l -c "bundle install"
RUN rake db:create db:migrate
# nginx configuration
ADD nginx.conf /etc/nginx/sites-enabled/default
RUN sed -i 's/XXX.XXX.XXX.XXX/SERVERNAME_OR_IPADDRESS/' /etc/nginx/sites-enabled/default
RUN sed -i 's/RAILS_APP_NAME/YOUR_RAILS_APPLICATION/' /etc/nginx/sites-enabled/default
# Restart nginx service
CMD sudo service nginx restart
EXPOSE 80

Working with Docker on a Linux Server

If you are interested in working with Docker I strongly recommend using Digital Ocean's hosting solution. They offer a 'Docker' plan; which means it already has all the required software and dependencies.

Otherwise, here are instructions for installing Docker on an Ubuntu 14.04 LTS server (which you can also get from Digital Ocean):

$ apt-get update
$ apt-get -y install docker.io
# fix paths
$ ln -sf /usr/bin/docker.io /usr/local/bin/docker
$ sed -i '$acomplete -F _docker docker' /etc/bash_completion.d/docker.io
# configure Docker to start when the server boots
$ update-rc.d docker.io defaults

server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name XXX.XXX.XXX.XXX;
passenger_enabled on;
rails_env production;
root /opt/RAILS_APP_NAME/public;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

Docker is changing rapidly. I found this list of steps to upgrade docker:

$ wget -qO- https://get.docker.io/gpg | sudo apt-key add -
$ sudo sh -c "echo deb http://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list"
$ sudo apt-get update
$ sudo apt-get install lxc-docker
$ docker -v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment