自动化部署基于 Docker 的 Rails 应用
[TL;DR] 这是系列文章的第三篇,讲述了我的公司是如何将基础设施从PaaS移植到Docker上的。
- 第一部分:谈论了我接触Docker之前的经历;
- 第二部分:一步步搭建一个安全而又私有的registry。
在系列文章的最后一篇里,我们将用一个实例来学习如何自动化整个部署过程。
基本的Rails应用程序
现在让我们启动一个基本的Rails应用。为了更好的展示,我使用Ruby 2.2.0和Rails 4.1.1
在终端运行:
<span class="pln">$ rvm </span><span class="kwd">use</span><span class="lit">2.2</span><span class="pun">.</span><span class="lit">0</span>
<span class="pln">$ rails </span><span class="kwd">new</span><span class="pun">&&</span><span class="pln"> cd docker</span><span class="pun">-</span><span class="pln">test</span>
创建一个基本的控制器:
<span class="pln">$ rails g controller welcome index</span>
……,然后编辑 routes.rb
,以便让该项目的根指向我们新创建的welcome#index方法:
<span class="pln">root </span><span class="str">'welcome#index'</span>
在终端运行 rails s
,然后打开浏览器,登录http://localhost:3000,你会进入到索引界面当中。我们不准备给应用加上多么神奇的东西,这只是一个基础的实例,当我们将要创建并部署容器的时候,用它来验证一切是否运行正常。
安装webserver
我们打算使用Unicorn当做我们的webserver。在Gemfile中添加 gem 'unicorn'
和 gem 'foreman'
然后将它bundle起来(运行 bundle install
命令)。
启动Rails应用时,需要先配置好Unicorn,所以我们将一个unicorn.rb文件放在config目录下。这里有一个Unicorn配置文件的例子,你可以直接复制粘贴Gist的内容。
接下来,在项目的根目录下添加一个Procfile,以便可以使用foreman启动应用,内容为下:
<span class="pln">web</span><span class="pun">:</span><span class="pln"> bundle </span><span class="kwd">exec</span><span class="pln"> unicorn </span><span class="pun">-</span><span class="pln">p $PORT </span><span class="pun">-</span><span class="pln">c </span><span class="pun">./</span><span class="pln">config</span><span class="pun">/</span><span class="pln">unicorn</span><span class="pun">.</span><span class="pln">rb </span>
现在运行foreman start命令启动应用,一切都将正常运行,并且你将能够在http://localhost:5000上看到一个正在运行的应用。
构建一个Docker镜像
现在我们构建一个镜像来运行我们的应用。在这个Rails项目的根目录下,创建一个名为Dockerfile的文件,然后粘贴进以下内容:
<span class="com"># 基于镜像 ruby 2.2.0</span>
<span class="pln">FROM ruby</span><span class="pun">:</span><span class="lit">2.2</span><span class="pun">.</span><span class="lit">0</span>
<span class="com"># 安装所需的库和依赖</span>
<span class="pln">RUN apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> update </span><span class="pun">&&</span><span class="pln"> apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> install </span><span class="pun">-</span><span class="pln">qy nodejs postgresql</span><span class="pun">-</span><span class="pln">client sqlite3 </span><span class="pun">--</span><span class="kwd">no</span><span class="pun">-</span><span class="pln">install</span><span class="pun">-</span><span class="pln">recommends </span><span class="pun">&&</span><span class="pln"> rm </span><span class="pun">-</span><span class="pln">rf </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">apt</span><span class="pun">/</span><span class="pln">lists</span><span class="com">/*</span>
<span class="com"># 设置 Rails 版本</span>
<span class="com">ENV RAILS_VERSION 4.1.1</span>
<span class="com"># 安装 Rails</span>
<span class="com">RUN gem install rails --version "$RAILS_VERSION"</span>
<span class="com"># 创建代码所运行的目录 </span>
<span class="com">RUN mkdir -p /usr/src/app </span>
<span class="com">WORKDIR /usr/src/app</span>
<span class="com"># 使 webserver 可以在容器外面访问</span>
<span class="com">EXPOSE 3000</span>
<span class="com"># 设置环境变量</span>
<span class="com">ENV PORT=3000</span>
<span class="com"># 启动 web 应用</span>
<span class="com">CMD ["foreman","start"]</span>
<span class="com"># 安装所需的 gems </span>
<span class="com">ADD Gemfile /usr/src/app/Gemfile </span>
<span class="com">ADD Gemfile.lock /usr/src/app/Gemfile.lock </span>
<span class="com">RUN bundle install --without development test</span>
<span class="com"># 将 rails 项目(和 Dockerfile 同一个目录)添加到项目目录</span>
<span class="com">ADD ./ /usr/src/app</span>
<span class="com"># 运行 rake 任务</span>
<span class="com">RUN RAILS_ENV=production rake db:create db:migrate </span>
使用上述Dockerfile,执行下列命令创建一个镜像(确保boot2docker已经启动并在运行当中):
<span class="pln">$ docker build </span><span class="pun">-</span><span class="pln">t localhost</span><span class="pun">:</span><span class="lit">5000</span><span class="pun">/</span><span class="pln">your_username</span><span class="pun">/</span><span class="pln">docker</span><span class="pun">-</span><span class="pln">test </span><span class="pun">.</span>
然后,如果一切正常,长长的日志输出的最后一行应该类似于:
<span class="typ">Successfully</span><span class="pln"> built </span><span class="lit">82e48769506c</span>
<span class="pln">$ docker images</span>
<span class="pln">REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE </span>
<span class="pln">localhost</span><span class="pun">:</span><span class="lit">5000</span><span class="pun">/</span><span class="pln">your_username</span><span class="pun">/</span><span class="pln">docker</span><span class="pun">-</span><span class="pln">test latest </span><span class="lit">82e48769506c</span><span class="typ">About</span><span class="pln"> a minute ago </span><span class="lit">884.2</span><span class="pln"> MB </span>
让我们运行一下容器试试!
<span class="pln">$ docker run </span><span class="pun">-</span><span class="pln">d </span><span class="pun">-</span><span class="pln">p </span><span class="lit">3000</span><span class="pun">:</span><span class="lit">3000</span><span class="pun">--</span><span class="pln">name docker</span><span class="pun">-</span><span class="pln">test localhost</span><span class="pun">:</span><span class="lit">5000</span><span class="pun">/</span><span class="pln">your_username</span><span class="pun">/</span><span class="pln">docker</span><span class="pun">-</span><span class="pln">test</span>
通过你的boot2docker虚拟机的3000号端口(我的是http://192.168.59.103:3000),你可以观察你的Rails应用。(如果不清楚你的boot2docker虚拟地址,输入$ boot2docker ip
命令查看。)
使用shell脚本进行自动化部署
前面的文章(指文章1和文章2)已经告诉了你如何将新创建的镜像推送到私有registry中,并将其部署在服务器上,所以我们跳过这一部分直接开始自动化进程。
我们将要定义3个shell脚本,然后最后使用rake将它们捆绑在一起。
清除
每当我们创建镜像的时候,
- 停止并重启boot2docker;
- 去除Docker孤儿镜像(那些没有标签,并且不再被容器所使用的镜像们)。
在你的工程根目录下的clean.sh文件中输入下列命令。
<span class="pln">echo </span><span class="typ">Restarting</span><span class="pln"> boot2docker</span><span class="pun">...</span>
<span class="pln">boot2docker down </span>
<span class="pln">boot2docker up</span>
<span class="pln">echo </span><span class="typ">Exporting</span><span class="typ">Docker</span><span class="pln"> variables</span><span class="pun">...</span>
<span class="pln">sleep </span><span class="lit">1</span>
<span class="kwd">export</span><span class="pln"> DOCKER_HOST</span><span class="pun">=</span><span class="pln">tcp</span><span class="pun">:</span><span class="com">//192.168.59.103:2376 </span>
<span class="kwd">export</span><span class="pln"> DOCKER_CERT_PATH</span><span class="pun">=</span><span class="str">/Users/</span><span class="pln">user</span><span class="pun">/.</span><span class="pln">boot2docker</span><span class="pun">/</span><span class="pln">certs</span><span class="pun">/</span><span class="pln">boot2docker</span><span class="pun">-</span><span class="pln">vm </span>
<span class="kwd">export</span><span class="pln"> DOCKER_TLS_VERIFY</span><span class="pun">=</span><span class="lit">1</span>
<span class="pln">sleep </span><span class="lit">1</span>
<span class="pln">echo </span><span class="typ">Removing</span><span class="pln"> orphaned images without tags</span><span class="pun">...</span>
<span class="pln">docker images </span><span class="pun">|</span><span class="pln"> grep </span><span class="str">"<none>"</span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'{print $3}'</span><span class="pun">|</span><span class="pln"> xargs docker rmi </span>
给脚本加上执行权限:
<span class="pln">$ chmod </span><span class="pun">+</span><span class="pln">x clean</span><span class="pun">.</span><span class="pln">sh</span>
构建
构建的过程基本上和之前我们所做的(docker build)内容相似。在工程的根目录下创建一个build.sh脚本,填写如下内容:
<span class="pln">docker build </span><span class="pun">-</span><span class="pln">t localhost</span><span class="pun">:</span><span class="lit">5000</span><span class="pun">/</span><span class="pln">your_username</span><span class="pun">/</span><span class="pln">docker</span><span class="pun">-</span><span class="pln">test </span><span class="pun">.</span>
记得给脚本执行权限。
部署
最后,创建一个deploy.sh脚本,在里面填进如下内容:
<span class="com"># 打开 boot2docker 到私有注册库的 SSH 连接</span>
<span class="pln">boot2docker ssh </span><span class="str">"ssh -o 'StrictHostKeyChecking no' -i /Users/username/.ssh/id_boot2docker -N -L 5000:localhost:5000 [email protected] &"</span><span class="pun">&</span>
<span class="com"># 在推送前先确认该 SSH 通道是开放的。</span>
<span class="pln">echo </span><span class="typ">Waiting</span><span class="lit">5</span><span class="pln"> seconds before pushing image</span><span class="pun">.</span>
<span class="pln">echo </span><span class="lit">5.</span><span class="pun">..</span>
<span class="pln">sleep </span><span class="lit">1</span>
<span class="pln">echo </span><span class="lit">4.</span><span class="pun">..</span>
<span class="pln">sleep </span><span class="lit">1</span>
<span class="pln">echo </span><span class="lit">3.</span><span class="pun">..</span>
<span class="pln">sleep </span><span class="lit">1</span>
<span class="pln">echo </span><span class="lit">2.</span><span class="pun">..</span>
<span class="pln">sleep </span><span class="lit">1</span>
<span class="pln">echo </span><span class="lit">1.</span><span class="pun">..</span>
<span class="pln">sleep </span><span class="lit">1</span>
<span class="com"># Push image onto remote registry / repo</span>
<span class="pln">echo </span><span class="typ">Starting</span><span class="pln"> push</span><span class="pun">!</span>
<span class="pln">docker push localhost</span><span class="pun">:</span><span class="lit">5000</span><span class="pun">/</span><span class="pln">username</span><span class="pun">/</span><span class="pln">docker</span><span class="pun">-</span><span class="pln">test </span>
如果你不理解这其中的含义,请先仔细阅读这部分第二部分。
给脚本加上执行权限。
使用rake将以上所有绑定
现在的情况是,每次你想要部署你的应用时,你都需要单独运行这三个脚本。
- clean
- build
- deploy / push
这一点都不费工夫,可是事实上开发者比你想象的要懒得多!那么咱们就索性再懒一点!
我们最后再把工作好好整理一番,我们现在要将三个脚本通过rake捆绑在一起。
为了更简单一点,你可以在工程根目录下已经存在的Rakefile中添加几行代码,打开Rakefile文件,把下列内容粘贴进去。
<span class="kwd">namespace</span><span class="pun">:</span><span class="pln">docker </span><span class="kwd">do</span>
<span class="pln">desc </span><span class="str">"Remove docker container"</span>
<span class="pln">task </span><span class="pun">:</span><span class="pln">clean </span><span class="kwd">do</span>
<span class="pln">sh </span><span class="str">'./clean.sh'</span>
<span class="kwd">end</span>
<span class="pln">desc </span><span class="str">"Build Docker image"</span>
<span class="pln">task </span><span class="pun">:</span><span class="pln">build </span><span class="pun">=></span><span class="pun">[:</span><span class="pln">clean</span><span class="pun">]</span><span class="kwd">do</span>
<span class="pln">sh </span><span class="str">'./build.sh'</span>
<span class="kwd">end</span>
<span class="pln">desc </span><span class="str">"Deploy Docker image"</span>
<span class="pln">task </span><span class="pun">:</span><span class="pln">deploy </span><span class="pun">=></span><span class="pun">[:</span><span class="pln">build</span><span class="pun">]</span><span class="kwd">do</span>
<span class="pln">sh </span><span class="str">'./deploy.sh'</span>
<span class="kwd">end</span>
<span class="kwd">end</span>
即使你不清楚rake的语法(其实你真应该去了解一下,这玩意太酷了!),上面的内容也是很显然的吧。我们在一个命名空间(docker)里声明了三个任务。
三个任务是:
- rake docker:clean
- rake docker:build
- rake docker:deploy
Deploy独立于build,build独立于clean。所以每次我们输入命令运行的时候。
<span class="pln">$ rake docker</span><span class="pun">:</span><span class="pln">deploy</span>
所有的脚本都会按照顺序执行。
测试
现在我们来看看是否一切正常,你只需要在app的代码里做一个小改动:
<span class="pln">$ rake docker</span><span class="pun">:</span><span class="pln">deploy</span>
接下来就是见证奇迹的时刻了。一旦镜像文件被上传(第一次可能花费较长的时间),你就可以ssh登录产品服务器,并且(通过SSH管道)把docker镜像拉取到服务器并运行了。多么简单!
也许你需要一段时间来习惯,但是一旦成功,它几乎与用Heroku部署一样简单。
备注:像往常一样,请让我了解到你的意见。我不敢保证这种方法是最好,最快,或者最安全的Docker开发的方法,但是这东西对我们确实奏效。
Docker 的详细介绍:请点这里
Docker 的下载地址:请点这里