如何用Docker部署Rails应用

近期在运维界有一个新兴技术docker特别火,在看了相关的介绍之后果断决定尝试一下用docker部署一台服务器。过程中记录了一下整个操作的过程及相关配置文件,分享给各位也爱追求技术时尚的程序猿们。

适用环境

服务器:阿里云 (双核 + 2GB 内存) Ubuntu 14.04
应用的stack: nginx + unicorn + mongodb

在阿里云上安装dockerengine

基本按照官网上的安装指南来做的。我刚开始选择的是ubuntu管理的安装包,docker.io, 版本是 1.0.1,发现bug太多,后来重新安装了最新的版本 1.4.1。官网的安装包似乎被墙了,用了网页最下面的Yandex的镜像才把docker安装好。

启动docker的daemon程序

正常的情况下只需要执行下面的命令就可以启动docker

$ sudo service docker start

但是在阿里云的ECS上报出无闲置IP的错误,百度了一下才找到解决方案,操作步骤如下:

打开/etc/network/interfaces,注释掉以下配置

# route del -net 172.16.0.0 netmask 255.240.0.0 dev eth0 

重新启动networking

$ sudo service networking restart

重新启动docker

$ sudo service docker restart

测试一下docker是否正常运行

$ docker info
Containers: 33
Images: 176
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Dirs: 242
Execution Driver: native-0.2
Kernel Version: 3.13.0-32-generic
Operating System: Ubuntu 14.04.1 LTS
CPUs: 2
Total Memory: 3.859 GiB
Name: iZ256yal27dZ
ID: BQ3A:ZJIY:5EOM:JOTY:EROQ:7UI6:SB6P:QVBC:3FM5:DEMB:WBY2:ZDH6
WARNING: No swap limit support

启动nginx的container

在阿里云的机器上构建以下文件夹,并创建相应的文件

dockers
└── nginx
    ├── Dockerfile
    └── config
        └── nginx-app.conf

注意:我们暂时先将与rails app有关的配置文件注释了

# Dockerfile for installing and running Nginx

# Select ubuntu as the base image
From registry.mirrors.aliyuncs.com/library/ubuntu:14.04

# Install nginx
RUN apt-get update
RUN apt-get install -y nginx
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
# ADD config/nginx-app.conf /etc/nginx/sites-enabled/default

# Publish port 80
EXPOSE 80

# Start nginx when container starts
ENTRYPOINT /usr/sbin/nginx
# nginx-app.conf

# this can be any application server, not just Unicorn/Rainbows!
upstream rails-app {
  server app:8080 fail_timeout=0;
}

server {
  listen 80 default deferred; # for Linux

  client_max_body_size 4G;
  server_name _;

  keepalive_timeout 5;

  # path for static files
  root /webapps/app/public;

  try_files $uri/index.html $uri.html $uri @unicorn;

  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://rails-app;
  }

  # Rails error pages
  error_page 500 502 503 504 /500.html;
  location = /500.html {
    root /webapps/app/public;
  }
}

然后在nginx文件夹下,生成新的docker image,并启动nginx的container

$ docker build -t junhao/nginx .
$ docker run --name web -d -p 80:80 junhao/nginx

运行docker ps来检查一下container的运行情况

$ docker ps
CONTAINER ID        IMAGE                  COMMAND                CREATED             STATUS              PORTS                    NAMES
87ae87c89a78        junhao/nginx:latest    "/bin/sh -c /usr/sbi   5 days ago          Up 5 days           0.0.0.0:80->80/tcp       web

打开浏览器,输入你的阿里云VM地址,应该就能看到“Welcome to Nginx”的页面。阶段性成功,yay!

启动unicorn的container

先把rails app上传到服务器上,在应用根目录下创建这样几个文件,Dockerfile, .dockerignore, scripts/start-server.sh

# Dockerfile for a Rails application using Nginx and Unicorn

# Select ubuntu as the base image
From registry.mirrors.aliyuncs.com/library/ubuntu:14.04

RUN apt-get update -q
RUN apt-get install -qy curl

# Install rvm, ruby, bundler
RUN curl -sSL https://get.rvm.io | bash -s stable
RUN /bin/bash -l -c "rvm requirements"
RUN /bin/bash -l -c "rvm install 2.1.5"
RUN /bin/bash -l -c "gem install bundler --no-ri --no-rdoc"

# Copy the Gemfile and Gemfile.lock into the image. 
# Temporarily set the working directory to where they are. 
WORKDIR /tmp 
ADD ./Gemfile Gemfile
ADD ./Gemfile.lock Gemfile.lock
RUN /bin/bash -l -c "bundle install"

# Add rails project to project directory
ADD ./ /webapps/app

# set WORKDIR
WORKDIR /webapps/app

# bundle install
# RUN /bin/bash -l -c "bundle install"

# Add configuration files in repository to filesystem
ADD scripts/start-server.sh /usr/bin/start-server
RUN chmod +x /usr/bin/start-server

# Publish port 80
EXPOSE 8080

# Startup commands
ENTRYPOINT /usr/bin/start-server
# .dockerignore

# Ignore bundler config.
/.bundle

# Ignore the default SQLite database.
/db

# Ignore all logfiles and tempfiles.
/log
/tmp

# Gemfile.lock

# Redis
dump.rdb

注意:我有一个unicorn的配置文件在config文件夹下,没有用配置文件的需要修改start-server.sh的最后一行命令

#!/bin/bash

cd /webapps/app
source /etc/profile.d/rvm.sh
mkdir -p /webapps/shared/pids
mkdir -p /webapps/shared/log
cat /webapps/shared/pids/unicorn.pid
kill -QUIT `cat /webapps/shared/pids/unicorn.pid`
bundle exec unicorn -c config/unicorn.rb -E production -p 8080

然后创建unicorn的docker image,并启动container

$ cd /webapps/app
$ docker build -t junhao/app .
$ docker run --name app -d -p 8080:8080 junhao/app

接着,我们要对nginx的container做一些改动:把和rails app相关的配置添加回来,并重新创建、启动nginx的container。

打开dockers/nginx/conf/nginx-app.conf,把下面这行设置添加回来

# ADD config/nginx-app.conf /etc/nginx/sites-enabled/default

然后停止现在的container,并重建container。

$ cd dockers/nginx
$ docker stop web
$ docker build -t junhao/web .

下一步就是重启,在重启的时候我们要用到一个叫container linking的技术手法。仔细看一下nginx-app.conf,里面有这样一段代码:

upstream rails-app {
  server app:8080 fail_timeout=0;
}

这里的app:8080中的app指的是我们创建的unicorn container。那么在nginx的container中,app代表的其实是unicorn container在本机的地址映射。这个是需要我们在启动nginx container的时候做特殊处理的,不然nginx container无法获得相关信息。--link app:app就是把app container的信息传递给了web container。

$ docker run --name web --link app:app -d -p 80:80 junhao/nginx

现在打开浏览器,试试打开一个不需要访问数据库的页面。

配置MongodDB

我用了MongoDB官方的部署服务MMS来管理MongoDB,所以没有用docker。大家也可以尝试不同的方法。在本机安装完MongoDB之后,在config/mongoid.yml中修改hosts的地址:- dockerhost:27000。这里的dockerhost指的是container运行的VM的地址。
这个地址我们可以在container启动时定义,由于之前运行时没有定义这个值,我们需要重启app container。

$ docker stop app
$ docker build -t junhao/app .
$ docker run --name app --add-host=dockerhost:<enter your host address here> -d -p 8080:8080 junhao/app

然后重启一下web container

$ docker stop web
$ docker run --name web --link app:app -d -p 80:80 junhao/nginx

这样就大功告成啦!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,635评论 6 515
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,543评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 168,083评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,640评论 1 296
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,640评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,262评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,833评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,736评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,280评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,369评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,503评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,185评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,870评论 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,340评论 0 24
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,460评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,909评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,512评论 2 359

推荐阅读更多精彩内容