<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sining  Liu on Medium]]></title>
        <description><![CDATA[Stories by Sining  Liu on Medium]]></description>
        <link>https://medium.com/@siningliu?source=rss-48609ce6d0f2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*v0gkOX2u69LffIJN.jpg</url>
            <title>Stories by Sining  Liu on Medium</title>
            <link>https://medium.com/@siningliu?source=rss-48609ce6d0f2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 01:10:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@siningliu/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[人才市场也是市场]]></title>
            <link>https://medium.com/@siningliu/%E4%BA%BA%E6%89%8D%E5%B8%82%E5%9C%BA%E4%B9%9F%E6%98%AF%E5%B8%82%E5%9C%BA-90aebe40774f?source=rss-48609ce6d0f2------2</link>
            <guid isPermaLink="false">https://medium.com/p/90aebe40774f</guid>
            <category><![CDATA[hr]]></category>
            <dc:creator><![CDATA[Sining  Liu]]></dc:creator>
            <pubDate>Mon, 19 Mar 2018 05:11:28 GMT</pubDate>
            <atom:updated>2018-03-19T05:11:28.795Z</atom:updated>
            <content:encoded><![CDATA[<p>是市场，就会有市场的样子。</p><p>看看股票市场上有多少人因为各种各样的情绪，做出截然相反的交易决定。不难想象，在人才市场（招聘，应聘的市场）上，人们也会有很多种情绪。</p><p>我的一个朋友跟我说，他刚开始找工作的时候，拿到的第一个 offer 就一定会接，因为他担心找不到下一份工作。今天，我也在找工作，也面临类似的恐慌。</p><p>这种恐慌在我身上的表现是什么样呢？明明该准备面试的时候，心里是慌慌的，觉得招聘的要求都好高，没有一家公司的要求完全符合（在程序员的招聘中，这主要表现为技术栈不同），想要补充一些技术栈，但是时间紧，想要补充的又特别多，于是不知所措。</p><h4>冷静</h4><p>但我还是要冷静下来。</p><p>市场是一个充满不确定性的地方，不冷静有什么用，能改变不确定性么？答案当然是不能。而冷静至少还能让自己<a href="https://www.jianshu.com/p/8a75f4fa446a">分析其中成功概率更大的决策方向</a>。</p><p>在我尝试让自己冷静下来时，我想到的第一件事是，<a href="http://zhibimo.com/read/xiaolai/everyone-can-use-english/chapter1.html#4-%E8%A7%82%E5%BF%B5%E6%98%AF%E5%A6%82%E4%BD%95%E5%BD%B1%E5%93%8D%E6%88%91%E7%9A%84%EF%BC%9F">李笑来老师在他28岁的时候也面临在人才市场上的交易</a>。他准备了4个月后去新东方面试，而在这4个月里，他的心情是如何变化的呢？</p><p>从他后来的文章中，我可以看出，他在这4个月至少分析出一件事，那就是在托福阅读中上下文逻辑比背单词更重要，而且后来他也确实拿到了托福高分。</p><p>他自嘲自己那时已经28岁高龄，而且按照当时社会的节奏，很多人在28岁已经当爸爸了，他更有理由比我恐慌（不过，我判断他那时的学习能力比我强）。但他最终让自己完成了28岁的转型。</p><p>所以，有人能让自己在这个市场上冷静下来，也有人在这个市场上成功过，而且还不少吧。</p><h4>肯定不是所有的机会都是我的，但肯定也有机会是我要拿下的</h4><p>不同的程序员能做的事的差别可以是天壤之别。</p><p>前几天读到一个叫道哥的人<a href="http://mp.weixin.qq.com/s/epFSC88J7LF3BGwQdoZ-Rg">写的一篇关于 DNS 技术的文章</a>，他不是来介绍 DNS 的，他是来告诉大家，他在Alibaba 发明了一种新的资源寻址的方法，可以代替 DNS。在很多 web 开发者每天都在 DNS 构建的这个互联网下写各种各样的 http 请求时，有人却能想到替换 DNS，并且真的有能力这样做。</p><p>今天，那样的机会不是我的，只能靠积累才有可能触碰那样的机会。但另外一些机会确实应该抓住，哪怕难一点也要积极准备，不然就是对自己不负责任。</p><p>现在来看，这些机会是那些有成长率的创业公司。</p><p>小公司不代表是创业公司，有成长率才是创业公司。有些创业公司已经成长的很大了，只是没有上市，但他们依然在成长，依然在创更多的业，加入这样的公司可以一起成长。</p><p>如何抓住这些机会呢？</p><p>我将在另一篇文章中给出的答案是，先通过积累，成为一个小技术领域的专才，这样你能抓住一个小领域里90%的机会。积累到一定程度，扩展技术栈，再积累成一个专才，循环往复，成为编程上有多维度竞争力的人，在更大领域的中有更多的机会。</p><h4>这个市场上的卖方</h4><p>公司的招聘要求可以写的很高，但最终能不能产生交易，还要看卖方，也就是应聘者。如果公司以当前的价格买不到需要的人力，怎么办？和股票市场一样，公司就要提价购买，降低招聘要求，或者选择不购买。</p><p>但是，是什么使得公司以当前的要求能够买到或者买不到人力。经济学中的供需理论或许能做出解释。在一个岗位的细分市场中，如果需求大于供给，那么价格就上涨。可以想像，如果新公司或者新团队在写代码的时候普遍不再用某个技术，那么需求就没有增加；如果使用这个技术的团队解散，那么对这个技术的需求就不升反降；分之则分别是需求增加或保持稳定。</p><p>考虑供给时也是一样，如果越来越多的人进入同一个技术领域找工作，那么供给就提高；反之，当很多人离开这个技术领域时，供给就减少。</p><p>不过，市场总是动态调整的，如果需求少了，供给也会慢慢减少；如果供给太少，需求也会被想办法用别的方法解决。所以一个有趣的现象是，如果一个领域需要的人力很少，这个领域的人力供给也会很少。这给不同人的感觉是：很多应聘者会觉得自己难以找到合适的工作，而招聘方也会觉得人太难招。这就好像下面这张交易市场的深度图。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WkeqfIJxhy_0E2CCS1jH8A.jpeg" /><figcaption>某交易所盘口深度图</figcaption></figure><p>还有，从另一个角度想，公司不一定就是购买者。应聘者也可以是购买者，花自己的时间来买钱和成长的购买者。公司也是出售者，出售自己的钱来换回公司应有的成长。当然，人们更多的时候是把钱当做通货，所以有钱的一方通常被视为购买者。</p><p>最后，在市场上准备交易的那一刻，公司也面临恐惧吧：恐惧错过了这个人找不到下一个怎么办，恐惧这个人不是最好的，下一个好的出现时自己没机会购买了怎么办。尤其是在一个没有交易深度的市场。应聘者也应该理解公司的难处。</p><h4>后话</h4><p>看看刚刚那张中间出现沟壑的交易深度图，我有一个猜想：提高自身估值的一个办法就是被招聘方迫切的需要。如果招聘方需要你，他们会不惜到鸿沟的另一端（卖单一端）去购买。</p><p>我有一次被其他公司叫去参加他们的团队的经历，那次经历中得到承诺的待遇比以前团队高很多，也比我后来自己去找工作得到的待遇高。我猜，这可以用上面那个交易深度图解释。当公司更主动的去下即刻交易的买单时（急聘），他们会跨过买单和卖单之间的鸿沟，出个比平时更高的价格；而应聘者更积极的去下即刻交易的卖单时（求工作），他们会也会跨过拿到沟沟。</p><p>这给我的启发是，被别人需要带来的升职加薪更快。</p><p>当然，成长最重要。如果能有更大的成长，那个沟沟不算什么。</p><p>有一个朋友跟我说，如果能跟（大）公司提出 special offer （薪资高于普通 offer）的要求，就尽量提出，因为提出要求自己没什么损失，值得尝试。我觉得主动提出需求，确实是个值得尝试的；<a href="https://www.ted.com/talks/jia_jiang_what_i_learned_from_100_days_of_rejection">TED talk 上还有一个精彩的视频</a>，在努力说服人们学会 ask（提出需求），但我并不总是能做到。而以上我从交易深度图中得到的启发或许能在另外一些时候帮上一点忙。</p><p>希望我们都能成长进步，加油！</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=90aebe40774f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[哪怕转化率再小，这么大的曝光量也会促进比特币的传播吧]]></title>
            <link>https://medium.com/@siningliu/%E5%93%AA%E6%80%95%E8%BD%AC%E5%8C%96%E7%8E%87%E5%86%8D%E5%B0%8F-%E8%BF%99%E4%B9%88%E5%A4%A7%E7%9A%84%E6%9B%9D%E5%85%89%E9%87%8F%E4%B9%9F%E4%BC%9A%E4%BF%83%E8%BF%9B%E6%AF%94%E7%89%B9%E5%B8%81%E7%9A%84%E4%BC%A0%E6%92%AD%E5%90%A7-bdd2b5c7276a?source=rss-48609ce6d0f2------2</link>
            <guid isPermaLink="false">https://medium.com/p/bdd2b5c7276a</guid>
            <category><![CDATA[bitcoin]]></category>
            <dc:creator><![CDATA[Sining  Liu]]></dc:creator>
            <pubDate>Sat, 20 May 2017 12:01:41 GMT</pubDate>
            <atom:updated>2017-05-20T12:16:09.474Z</atom:updated>
            <content:encoded><![CDATA[<h4>先谈关于热点的特度，再谈比特币</h4><p>最近发生了一件事，就是有一种病毒很火。我并不关心这件事，而关心的是病毒作者索要比特币。</p><p>我特别怕自己追热点，因为我也闹不清楚自己为什么要追热点。以前做新媒体的时候，追热点是为了更好收割其他用户的注意力。我故意说的邪恶一点，但我更想做个好人（虽然好人的定义我还没想清楚）。</p><p>但就是因为看到热点的力量：热点真的太适合收割注意力了，弄的自己都有点不知所措：有时候会问自己，那些用户为什么会被热点吸引，他们真的知道自己是在花时间看一篇我们精心设计的，为了收割他们注意力的文案吗？我也会被热点吸引吗？</p><p>热点很可怕吗？</p><p>但想一想，这不是热点的问题，是我的问题。热点会层出不穷，这个世界就是那样，就是隔一段时间来几个热点。但我看热点和有思考能力，以及很有思考能力的人看热点是不一样的，我们看到的东西可能（90%的可能性）很不一样。我只看到了别人的笑话或者别人的悲剧，大多数公共世界不都是这样吗？仿佛那给了我嘲笑或者怜悯别人的权利（请注意仿佛两个字）；而他们（我佩服的人）还看到了做出行动的可能方案（能写的人就写文案，能做事的人就做出点事）。而我等吃瓜群众就是在媒体（不论是纸媒还是新媒体）的设计下，amusing to death.</p><p>但对于任何人来说，这个世界的热点就是有点多。我曾经写过一篇《2012仿佛已是遥远的存在》，写了2012年的热点多也不多，因为我大部分都莫名其妙地参与了，但是多年后我啥都不记得，至少，那一年对我来说最重要的事情显示和这些新闻都不太有关系（而且，真正重要的事情反而没怎么参与）。</p><p>那些有思考能力的人也不是每个热点都参与吧。自己能参与的，能有所行动的会有所行动；不关自己事的，自己就默默的做事，也不会老出来刷存在感。对，我就是搞不清楚自己是真的对那些热点感兴趣，还是在刷存在感。了解自己也是挺难的，需要不断的调整天平上的的砝码，然后观察天平是否平衡。</p><h4>关比特币什么事</h4><p>我扪心自问，我不是来刷存在感的（不是来凑热闹的），只是想分享点观点。</p><p>这里，还想插播一句，什么是分享。分享的得是有质量的东西吧，没质量的东西给别人那不就叫分享。</p><p>病毒作者在勒索信中写了比特币的解释，写了BTC的购买方法，还让受害者「亲身体验」了一下BTC的转账（不通过任何中间机构的转账，这是BTC的一个特点）。这件事被很多媒体记者，尤其是科技媒体记者写在了文章里，这又让多少人知道了比特币，勾起了他们的好奇心，甚至让他们有机会第一次尝试持有比特币呢？</p><p>尤其是病毒作者为什么偏偏选择BTC，这难道不会让一些人去思考吗？所以，我感觉这次病毒至少在一定程度上对BTC是个宣传。BTC可能（只有10%的可能性，因为我并没有确凿的证据，把不确定的投资，哪怕只是选择上的投资调低一点，是避免负面黑天鹅的方法，当然，我也会因此错过正面黑天鹅，但是事前看来，谁也不知道前方是哪种黑天鹅，只有维持自己不下场，避免最坏黑天鹅将我赶下场，我才有更多机会遇到正面黑天鹅。当然，更多的学习这方面的内容，让自己有更多的知识来调整前面的概率是现在就要做的事情）还没有这么沸沸扬扬的出现在一个可以被用于交易的场景下，以前被人们认为是投资品更多一点吧。</p><p>总结一下，病毒这个热点意外的宣传了BTC。</p><p>还有一件事就是，现在中国交易所内的BTC不允许提币（就是在写这句话的时候我有点担心government会继续做出其他限制市场行为的决定，比如没收），也就是说，有很多BTC是不能被转账到病毒作者的账户里的。你要想买点BTC并且给病毒作者转过去，你得先在国内买点其他币种的CryptoCurrency，弄到国外交易所去，换成BTC。或者你把人民币换成外汇，达到国外银行或交易所，在那里买比特币。当然，第二条路可能遇到国内我外汇管制，但我不知道管制是什么样的，我也就不瞎说了。</p><p>如果是国内用户被感染病毒，病毒作者要想教会这些操作也是需要功夫的，还多步骤我也没做过，尽管我自己还写了这样一篇文章。</p><p>最后，我们当然谴责病毒作者。</p><h4>关我什么事</h4><p>你说我对BTC是什么态度？我态度不明。</p><p>他有那么多好处，我当然希望他成功，让我看看他真正的价值。</p><p>但是，在那个交易就是支付的时代，在那个程序被写在代码里的时代。那个时代可能不是比这个时代变了一些，不是解决了一两个信用卡评分的问题，可能我们一觉醒来，世界是不一样的。</p><p>总之，以驯化不确定性的方法来继续前进。虽然那可能的不一样到底有多不一样，我还不知道。</p><p>这是搜索指数</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*86Q3JhEKCMr0QWzjdiB5aw@2x.png" /><figcaption>2013年还有一次尖尖的爆发</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/0*EsFx79W98DH7bL0a.jpg" /></figure><p>这是社交指数</p><p>当然，这尖尖的一次凸起，让我想到，提高转化率的更好的方法是持续不断的曝光，而不是单次大曝光。当然，我只是猜测，或许应该看看传播理论。</p><p>最后，偶然看到了百度指数的需求图谱</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Tiw-tNcwB4oN3eaJhOfCQw@2x.png" /><figcaption>竟然没有和BlockChain相关的搜索需求</figcaption></figure><p>这代表了大多数人的搜索选择吧，连和区块链相关的搜索需求都没有。或许大家可能都还不了解比特币。我也不太了解，不说别人，我应该再看几本书了。也就是说，我和大家都还有很长的路要走。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bdd2b5c7276a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[健身动作总结]]></title>
            <link>https://medium.com/@siningliu/%E5%81%A5%E8%BA%AB%E5%8A%A8%E4%BD%9C%E6%80%BB%E7%BB%93-6b0cc1a004a0?source=rss-48609ce6d0f2------2</link>
            <guid isPermaLink="false">https://medium.com/p/6b0cc1a004a0</guid>
            <dc:creator><![CDATA[Sining  Liu]]></dc:creator>
            <pubDate>Sat, 20 May 2017 11:55:15 GMT</pubDate>
            <atom:updated>2017-05-20T11:55:15.544Z</atom:updated>
            <content:encoded><![CDATA[<p>增肌的定义应该是，增强你比较弱的肌肉的能力。一个方法就是，你给肌肉压力，肌肉会因为（瞬时的）压力而储备更多力量上的冗余。</p><h4>肩膀</h4><p>所谓肩膀，是手臂根部外侧的肌肉。是一块不小的肌肉呢。对于一块不小的肌肉，你说他会整块整块的动吗？请注意，肌肉是也是肉哦，是软的哦。</p><p>就好像你双手拉一块布，整块布都会稍微紧一点，但只有你双手连接线（只考虑平面内的直线）上的那段布会被拉的最紧。</p><p>肌肉也是这样的，一大块肌肉很难同时被拉伸，每次拉都是使关节运动（改变两块骨头夹角）的最短的那段肌肉被拉伸的最彻底。肩膀这块肉就可以被分为前中后三条线路来分别练习拉伸（其实你也可以分为15条线路，但是显然分太多类不利于人类记忆）。每条线路上的拉伸，你都要保证那条线路就是发力的最短线路，这样才能被准确的拉伸到。</p><p>这里说句题外话，假设每块骨头都是一条直线（比如手臂里面的骨头）或者一个平面图形（比如肩胛骨）。那么，经过两块骨头，就有可能做一个平面。在这个平面上，如果我们要改变两个骨头的夹角，就可采用高中物理里那些杠杆类的应用题当做模型，一个骨头不同，当做墙壁，一个骨头动，当做木杆，木杆和墙壁的连接点是关节，图中的力F1来自于你要锻炼的肌肉，而且是刚刚提到的发力最短的那个线路。</p><p>然而，所有上面的假设有一个前提：平面。如果那两块骨头不在一个平面内，你就没办法用这个模型，虽然，你可以用两个平面，来分别描述力的情况。但是，多了一个方向的力，很有可能会引入另外一块肌肉的拉伸。让你不能做到专心刺激一块肌肉。甚至，被借力的肌肉（你训练目标之外的肌肉）的发力比你目标肌肉的还多，那不是练错了地方吗？</p><p>所以，正确的练习是做什么？</p><p>摆好你那几块骨头的位置，精准拉伸某块肌肉上的一条线路。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/260/1*PJbyQwGlAE3R9TsOXYRZKA.jpeg" /><figcaption>可以把一些道理运用到别处，叫做迁移；这是一次正确的迁移吗？</figcaption></figure><p>那么拉伸到什么程度呢？力竭。</p><p>什么是力竭呢，我还说不好，有可能是多次反复发力后出现的肌肉乏力，也可能呢是一次超重的重物给肌肉带来的随机性压力。</p><p>这两个都会是肌肉（这种生物组织）的压力源。生物组织会对压力产生反馈，储备更多能力。</p><h4>其他</h4><p>其他肌肉的练习方法我会再补充。</p><h4>价值问题</h4><p>你说，我上面写的这些有价值么。如果是被人们需要的，当然会有价值（显然这里有一个隐含条件，人们认同或者将会认同的东西才有价值）。那如何被人们需要，首先这个理论得是正确的，而且能让别人看出来是正确的。别人无法预期效果，可能就不会认为他是正确的。所以，肯定要有人来实践。</p><p>这里还有一个问题，就是什么是健康？之前和某驴同学讨论到，现在大多数app都还在用「卡路里」这样一个东西来表达人们的健康。目前来看，我们没有看到更好的概念来告诉你，你是不是健康，是更健康了还是相反？没有，真的没有。</p><p>之前很多人熟悉的一个概念「亚健康」可能也是一个无法落地的概念（无法落地不代表不存在，有时候，相信本身就会影响人们的行为，从而表现为存在）。这里的可能应该是99%，依据是<a href="https://zh.wikipedia.org/wiki/%E4%BA%9A%E5%81%A5%E5%BA%B7">Wikipedia中的亚健康词条</a>和<a href="http://view.news.qq.com/zt2012/sub_healthy/index.htm">一个腾讯新闻的专题报道</a>。（终于动手查了，终于不再凭感觉了，给自己鼓鼓掌）</p><p>某马爸爸说健康产业很大，我们却连健康是什么都还不知道。这说明啥？</p><p>不过话说回来，做出来才有更多的认知，那么谁去做我上面说的理论呢？我来啊。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6b0cc1a004a0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[连接redis，再看volumes]]></title>
            <link>https://medium.com/@siningliu/%E8%BF%9E%E6%8E%A5redis-%E5%86%8D%E7%9C%8Bvolumes-2d2869853bde?source=rss-48609ce6d0f2------2</link>
            <guid isPermaLink="false">https://medium.com/p/2d2869853bde</guid>
            <dc:creator><![CDATA[Sining  Liu]]></dc:creator>
            <pubDate>Thu, 02 Mar 2017 09:30:21 GMT</pubDate>
            <atom:updated>2017-03-02T09:30:21.285Z</atom:updated>
            <content:encoded><![CDATA[<p>今天的故事有两个：</p><p>首先，我想要把一个container部署到服务器上并且在docker内部连接上redis。我的docker-compose 都写好了links但是就是连接不上，看logs发现了报错，内容大概是连接不上redis。</p><p>然后，是一直没有搞明白的volumes有了一些进展，让将来实现不用build，直接把code部署到container里面增添了一些可能。</p><h4>请告诉Rails如何找到redis的地址</h4><p>我之前部署过ActionCable，需要用到redis，那时候redis和rails的连接是开passenger，然后在cable.yml中把url写成 localhost就行了。但是在docker上面部署的时候就不行，为什么不行呢？我一会来讲，先看报错：</p><p>如果我把Redis的url写成localhost，会看到这条error</p><pre>`rescue in establish_connection&#39;: Error connecting to Redis on localhost:6379 (Errno::ECONNREFUSED) (Redis::CannotConnectError)</pre><p>我google了一下，<a href="http://stackoverflow.com/questions/34729752/sidekiq-error-connecting-to-redis-on-127-0-0-16379-errnoeconnrefused-on-doc">找到一篇SOF（stackoverflow，下同）</a>，跟我的错误一样一样的。回答者说：</p><blockquote>Your app tries to connect on the localhost interface of the container it is running, but redis is running in a different container.</blockquote><blockquote>Modify your app config to use the link name of the redis container(redis in your case) as hostname for the connection.</blockquote><p>他的这句话让我想到了db。我在compose中也写了db这个container（从mysql的官方image实例出来的一个container），并且在database.yml这个数据库配置文件中指明了host是db。请看我的代码：</p><pre># docker-compose.yml<br>#...<br>services:<br>	db:<br>		image: mysql:latest<br>#...<br></pre><pre># config/database.yml<br>default: &amp;default<br>	adapter: mysql2<br>	host: db<br>#...</pre><p>就是这样rails才能找到db的位置。</p><p>这个时候我在想，是不是我没有改变rails中config里面redis的地址呢？这是必然的，因为我从来没有改过那里。另外，<a href="https://nickjanetakis.com/blog/dockerize-a-rails-5-postgres-redis-sidekiq-action-cable-app-with-docker-compose">我是按照另外一篇教材写的docker-compose.yml</a>。他的rails内的配置项都是通过一个gem自动生成的，所以我就用了同样的gem，生成了一个项目。然后我首先看到的所用redis的gem不一样。为什么会先看gem，因为logs里面的报错指向的位置是在类似于ruby2.3.0/gem/redis3.3.3这样的地方。所以我就去看了gemfile。tutorial的项目用的是redis-rails我用的是redis，虽然最后发现redis-rails的dependencies里面有redis。</p><p>bundle 了 redis-rails之后，发现还是不行，还是指向localhost。<a href="https://github.com/redis-store/redis-rails#usage">我就去看了redis-rails的github，上面有了一些关于config的介绍</a>。最重要的是这段：</p><p>README说我可以通过一个hash来配置redis的store</p><pre>config.cache_store = :redis_store, {<br>host: &#39;localhost&#39;,<br>port: 6379,<br>db: 0,<br>password: &#39;mypassword&#39;,<br>namespace: &#39;cache&#39;<br>}</pre><p>这里的localhost不就是那个被指向的地方么？而按照之前提到的SOF的答案，我应该把这里改成我自己的redis的地址啊，那个地址叫做redis（你也可能起了一些别的名字）。</p><p>这个时候我还是没有想清楚自己应该怎么配置这个config（其实应该自己尝试一下的，哎）。其中那个db写什么我不太清楚（接口），然后我就去看了tutorial的项目代码，里面写着：</p><pre>config.cache_store = :redis_store, &#39;redis://:yourpassword@redis:6379/0/cache&#39;</pre><p>这里面的url有点意思。最前面的redis://的意思就像是http://一样，可能是传输协议，后面到@之前的部分应该是用户名和密码的对。最后的@到端口应该就是类似于github上这种下载的urlgit@github.com:liusining/test-fork.git ，代表着服务器的地址，或者叫做domain。后面的后面就是namespace了。</p><p>我找着例子在自己的config/application.rb中配了一个，然后就成功了。意思是Redis可以用了。</p><h4>Volumes指向哪里？</h4><p>前面的故事只说了一下我研究volumes的目的，但我现在有一个更现实的问题，就是我的写法跟tutorial上的不一样，而且我不知道为什么不能一样。写成一样的，会有一些问题，就是报错说：Cannot locate Gemfile or .bundle/ directory。</p><p><a href="http://stackoverflow.com/questions/36635723/cannot-locate-gemfile-or-bundle-directory">我查了这个错误</a>，我估计我的错误就是因为用docker启动rails的时候，目录根本不在rails的根目录下面。这个时候，我的写法是这样的：</p><pre>services:<br>app:<br>	volumes: <br>		- .:/stockholm-unidev<br>#...</pre><p>但是，如果我把volumes改成 /stockholm-unidev这种路径，就能够正常启动rails，说明那个时候是在根目录下面的。为了理解我把image里面弄成了什么样子，我做了一下这样的实验，下面我主要介绍这个实验：</p><p>为了看清只写/stockholm-unidev的时候的路径。我用了如下这个命令，并得到结果为</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/577/0*agjIO2mL4MRz459Z.jpg" /></figure><p>或者再看这个：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/388/0*U-NYsuytmehH99OQ.jpg" /></figure><p>这个时候路径，就是指向container里面的绝对路径。请记住，这个时候我的volumes是/stockholm-unidev</p><p>当我把他写成.:/stocholm-unidev的时候，他就成了这个样子</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/348/0*kekULzplMhdtxEzD.jpg" /></figure><p>也就是说：路径是对的，但是目录下面没有东西了。</p><p>后来，我以为是因为我的目录名称和我本地的名称不一致，所以又试了把container里面的目录改成跟我的目录一样（但不是用rebuild的方法改），而是把volumes写成：</p><p>- .:/stockholm-server。虽然我没有在image里面建立这个目录。</p><p>这个写法的效果值得看看。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/0*fsTLb9iYJMuz8wwt.jpg" /></figure><p>上面这一连串的指令分别代码：</p><ol><li>当我把volumes改成/stockholm-server之后/stockholm-unidev恢复了</li><li>工作目录当然还是/stockholm-unidev</li><li>根目录多了一个/stocholm-server文件夹，经过文辉同学提醒，当然会多一个文件夹，这是compose映射上去的。</li><li>检查/stockholm-server是空的</li></ol><p>也就是说，rails的工作目录一直是/stockholm-unidev，只不过，当我把他写成.:/stocholm-unidev的时候，/stockholm-unidev就空了。就像，当我写成 .:/stockholm-server的时候，映射到image里面的/stocholm-server也是空的，所以，究其原因，docker竟然认为我的.所代表的目录是空的，那好，我来个直接的，我写成绝对目录，看看什么情况，结果是把.换成绝对目录也是空的。后来，可以不是空的了，多了一个./app的目录，但是剩下的还是空的。但是出现了一个小小的，虽然是空的./app还是让我想到，docker其实没有把我本地的.认为是空的。</p><p>这让我想到，或许因为那个image在服务器上，隔着遥远的距离，映射不上去啊。很有可能就是这样，所以我用映射的方式去上传code总是过不去。又或者，<a href="http://stackoverflow.com/a/30976905/6633748">照这个SOF上的高票答案的解释</a>，我打的那个点，会让container去服务器上找.这个映射，或者用compose的官方文档来所，是在host上来找这个 .。如果是在本地，那就是localhost，如果是在远端就是remote host 呗。哪种是更正确的呢。<a href="http://stackoverflow.com/a/30512262/6633748">另外一个SOF问答给了另外一个表述</a>：</p><blockquote>local volumes are not mounted when using docker-machine</blockquote><p>也就是说他倾向于第二种解释。那么，基于这种解释，解决办法就是在host上安装一个volumes，并且做一个绝对路径的映射呗。甚至<a href="https://github.com/synack/docker-rsync">提供了一个开源项目</a>来解决这个问题。</p><p>因此，目前来看更有可能正确的是（注意，是可能）是说host上没有那个路径，所以代码映射不上去。那么那个/stockholm-server以及后来的那个./app有是怎么回事呢？</p><p>这个 /stockholm-server还可以解释成，远端的host上构建了一个空的目录到container里，并且起名叫/stockholm-server。但是那个app呢？</p><p>更近一步，我把volumes写成这样：</p><p>- ../:/stockholm-unidev</p><p>结果就变成了</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/0*UBhn1BJV4CAbH44K.jpg" /></figure><p>也就是说，docker可能还是做了一点映射，只不过这个映射没做完，和服务器的连接就没有了。这个解释倾向于我自己的那个第一种解释。</p><p>这里存疑。</p><h4>Recall</h4><ol><li>要想在docker内部安装其他服务器，需要把服务器地址写为服务器名称，就像google.com是我在的电脑在访问google的电脑一样。因为每个container都是一样虚拟的电脑。总之，<strong>连接其他container要写那个container的名称</strong></li><li>本地的dir无法映射到服务器上的container的dir。有可能是因为通过docker-machine的服务器连接中断，也有可能是因为container在映射服务器本身的路径，而不是你本地的路径。</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2d2869853bde" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[使用docker的3条小技巧]]></title>
            <link>https://medium.com/@siningliu/%E4%BD%BF%E7%94%A8docker%E7%9A%843%E6%9D%A1%E5%B0%8F%E6%8A%80%E5%B7%A7-bc0d7c034cf0?source=rss-48609ce6d0f2------2</link>
            <guid isPermaLink="false">https://medium.com/p/bc0d7c034cf0</guid>
            <dc:creator><![CDATA[Sining  Liu]]></dc:creator>
            <pubDate>Wed, 01 Mar 2017 01:42:52 GMT</pubDate>
            <atom:updated>2017-03-01T01:42:52.546Z</atom:updated>
            <content:encoded><![CDATA[<p>最近在使用docker，发现下面三条小技巧可以考虑刻意练习一下。</p><h4>更换服务器ip</h4><p>有时候，我们需要更换服务器的ip，比如在AWS上，我们从一个公有ip切换到一个弹性ip，这个时候你会不会担心docker-machine连不上原来的服务器，<a href="https://medium.com/@siningliu/%E6%89%93%E5%BC%80%E5%92%8C%E5%85%B3%E9%97%ADdocker-machine-env-938cd201c4e6#.qnu0nx7vc">因为docker-machine要访问原来服务器的地址啊</a>。我是这么担心的，但是用了docker-machine regenerate-certs &lt;machine name&gt;之后，ip地址就会更新了。</p><h4>用docker-machine一键登录服务器</h4><p>你用docker-machine架设服务器，用docker build 网上建image。但是如果你想要增加一个 authorized_keys 之类文件的时候，你还是要登录主机的呀。<a href="https://docs.docker.com/machine/reference/ssh/">这个时候用</a><a href="https://docs.docker.com/machine/reference/ssh/">docker-machine ssh &lt;machine name&gt;就可以一键登录了</a>。跟直接用ssh 登录时一样的。只不过，这里docker-machine 替你调用的密钥是他已经在主机上写上的（人家替你搭建主机的时候就已经写好了呢）。</p><h4>设置container的时间</h4><p>linux系统本身是支持date指令的。所以，当你封装好一个image后，可以用下面的命令来看</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bc0d7c034cf0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[打开和关闭docker-machine env]]></title>
            <link>https://medium.com/@siningliu/%E6%89%93%E5%BC%80%E5%92%8C%E5%85%B3%E9%97%ADdocker-machine-env-938cd201c4e6?source=rss-48609ce6d0f2------2</link>
            <guid isPermaLink="false">https://medium.com/p/938cd201c4e6</guid>
            <dc:creator><![CDATA[Sining  Liu]]></dc:creator>
            <pubDate>Mon, 27 Feb 2017 06:29:39 GMT</pubDate>
            <atom:updated>2017-02-27T06:29:39.610Z</atom:updated>
            <content:encoded><![CDATA[<p>我在之前的一篇小文章中谈到，可以用 docker-machine env &lt;host name&gt; 来连接server上的环境。今天我们复习一下，并且看看如何关闭打开的环境。</p><h4>打开环境</h4><p>我们看到，当我们运行 docker-machine env &lt;host name&gt; 之后，命令返回一些信息（我之前竟然错过了，只字不差的阅读哪里去了？）</p><pre>#~/rails-projects/meal_plan master*<br>❯ docker-machine env stockholm-staging<br>export DOCKER_TLS_VERIFY=&quot;1&quot;<br>export DOCKER_HOST=&quot;tcp://52.80.2.252:2376&quot;<br>export DOCKER_CERT_PATH=&quot;/Users/apple/.docker/machine/machines/stockholm-staging&quot;<br>export DOCKER_MACHINE_NAME=&quot;stockholm-staging&quot;<br># Run this command to configure your shell:<br># eval $(docker-machine env stockholm-staging)</pre><p>注意看：</p><ol><li>第一行给TLS验证赋值为1（应该是成功了说验证通过）。</li><li>第二行给Docker host赋值为52.80.2.252:2376，这里面52.80.2.252丝毫不差是我的服务器地址，2376丝毫不差的是docker machine创建的安全组中打开的端口，所以，我推测docker machine是通过2376这个端口来访问服务器的。</li><li>第三行给cert path赋值了一个文件地址，这个文件地址应该是ssh文件，公钥在我的服务器上，私钥是刚刚的那个地址。</li><li>第四行是machine的名称。</li><li>第五行，第六行应该说的是用上面赋值的内容去做一个连接。</li></ol><p>这样，连接之后，你的docker都将指向服务器，而不是你的本地。这就是为什么<a href="https://coderjourney.com/learning-rails-deploying-rails-part-8/">一篇Rails部署的教学</a>中，能够直接把本地的docker打包成image到服务器上的原因。因为所有的docker-compose, docker run 都指向服务器。举个栗子：</p><p>在设置env为指向服务器后，输入 docker images，可以看到的是：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/786/0*LoYVYrrbt2WwNNJO.jpg" /></figure><p>这是服务器上的images。</p><p>当然，如果你用的不是docker命令，而是ls, cat这些命令，操作指令还是由本机来完成的。</p><h4>关闭环境</h4><p>如果我之前对每一行的回报信息的解读是正确的话，那么下面的关闭环境的操作就可以按照上面的套路来理解了。</p><p>想要关闭指向特定服务器的环境，你需要使用 docker-machine env -u。这里 -u 是unset的意思。运行后会得到如下结果：</p><pre>~/rails-projects/meal_plan master*<br>❯ docker-machine env -u<br>unset DOCKER_TLS_VERIFY<br>unset DOCKER_HOST<br>unset DOCKER_CERT_PATH<br>unset DOCKER_MACHINE_NAME<br># Run this command to configure your shell:<br># eval $(docker-machine env -u)</pre><p>这里面的前四行把之前打开env时赋值的结果全部清空，而当你运行eval $(docker-machine env -u)之后，这些被清空的值就真的被执行了，docker命令的指向也就不再是服务器。</p><p>可以说，docker-machine env 并不是让你真的登录到服务器，不是保持跟服务器的连接，而是当你操作docker命令的时候，往env中设置的host上发送数据。</p><h4>Recall</h4><ol><li>打开env其实是设置env的参数，让其指向服务器，并通过 eval 来运行这些参数。</li><li>关闭env其实是清空这些参数，并通过 eval 来执行清空的参数。</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=938cd201c4e6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[查docker logs，找不到container里的路径]]></title>
            <link>https://medium.com/@siningliu/%E6%9F%A5docker-logs-%E6%89%BE%E4%B8%8D%E5%88%B0container%E9%87%8C%E7%9A%84%E8%B7%AF%E5%BE%84-8a229bb15898?source=rss-48609ce6d0f2------2</link>
            <guid isPermaLink="false">https://medium.com/p/8a229bb15898</guid>
            <dc:creator><![CDATA[Sining  Liu]]></dc:creator>
            <pubDate>Fri, 24 Feb 2017 06:24:09 GMT</pubDate>
            <atom:updated>2017-02-24T06:24:09.014Z</atom:updated>
            <content:encoded><![CDATA[<h4>查报错</h4><p>现在，我的Rails运行在我的container中，环境是类似于production的一个环境（staging）。当我想要测试这个环境是否搭建起来的时候，得到这样一个错误</p><blockquote>An unhandled lowlevel error occurred. The application logs may have details.</blockquote><p>Rails竟然知道这是一个lowlevel 的错误，微笑。让我就去查了logs。</p><p>得到这样的更具体的搞错信息：</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*wY_6qydKfIQGz_uIjX5blw.png" /></figure><p>最后检查secrets.yml发现是我把secret_key_base写成了 sercret_key_base。好，现在这个问题解决了，但是这里面主要的问题是怎么查logs。我之前不会查log，甚至因为弄错了volumes（下一节谈到），而没法找到logs。</p><p>或许我可以用<a href="http://www.jianshu.com/p/a81300ed8145">进入container的方法</a>来查logs。但是因为偶然看到<a href="https://hub.docker.com/_/mysql/">mysql 官方文档上的一个说明</a>(大意是说：mysql support docker to inspect logs)，改变了我的想法：</p><p>docker logs &lt;container name&gt;</p><p>我之前得到的图中的报错信息就是这么来的。</p><h4>找不到container里的文件路径</h4><p>当我想要去运行一个container的时候，我用了这样的命令</p><p>docker-compose -f docker-compose.stg.yml run --rm stg-app rake db:create db:migrate RAILS_ENV=staging</p><p>但是得到的结果却说</p><blockquote>/stockholm-server/Gemfile not found</blockquote><p>这里需要说明的是，我用compose是因为<a href="https://docs.docker.com/compose/compose-file/#/service-configuration-reference">compose会把yml里面的很多设置项翻译成docker run 的options</a>。用原句说就是：</p><blockquote>much like passing command-line parameters to docker run</blockquote><p>而关于文件路径的那部分options叫做volumes。我在自己的compose中写成了这样，所以没有找到文件：</p><pre>services:<br>app:<br>volumes:<br>- .:/stockholm-server</pre><p>当然的是，我已经建立的app目录，在根目录下面，但是问什么找不到呢？</p><p>后来，我把路径换成了所谓的绝对路径就找到了：</p><pre>services:<br>app:<br>volumes:<br>- /stockholm-server</pre><p>这里面不同的是.和:，他们是什么意思呢，让我们一起来看看compose关于这一部分的介绍：</p><pre>volumes:<br>  # Just specify a path and let the Engine create a volume<br>  - /var/lib/mysql</pre><p>这里面的问题是the Engine，但是谁是engine，这个目录更engine有什么关系。下面这张图对于描述the Engine 有帮助么？</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/456/0*786FHe4YvzDmGhPX.png" /></figure><h4>Recall</h4><ol><li>volumes知道compose去找那项目文件</li><li>docker logs &lt;container name&gt; 可以用户查找logs，如果container支持的话。</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8a229bb15898" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Docker Compose Rails Example]]></title>
            <link>https://medium.com/@siningliu/a-docker-compose-rails-example-ada2bd81e89c?source=rss-48609ce6d0f2------2</link>
            <guid isPermaLink="false">https://medium.com/p/ada2bd81e89c</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Sining  Liu]]></dc:creator>
            <pubDate>Tue, 21 Feb 2017 09:48:35 GMT</pubDate>
            <atom:updated>2017-02-21T09:48:35.840Z</atom:updated>
            <content:encoded><![CDATA[<p>By now, <a href="https://medium.com/@siningliu/a-dockerfile-example-58f915cbaf44#.skmx5xu9a">we’ve known how to build an image through Dockerfile</a>. Actually, Dockerfile can help us build images of any projects. Today, I’m going to analyze an example on setting up Rails with Docker. And this is also from Docker’s official website.</p><h4>Define a rails container</h4><p>As you know, Docker will run projects in containers. The very first step of setting up a new rails project need to be to define the container’s environment. What should we include in a container that rails can run?</p><p>Ruby, of course. And other kind of packages.</p><p>Therefore, the first line of Dockerfile can be</p><pre>FROM ruby:2.3.3<br>RUN apt-get update -qq &amp;&amp; apt install -y build-essential libpq-dev nodejs</pre><p>All the packages appear in second line is to install ubuntu tools. I’ll skip the introductions of them, and actually, I can’t tell it clearly at this time. But one thing I can tell is these packages seems to be required in each rails container. If I can’t tell you guys why these stuff are required, I, at least, will summery which packages are needed.</p><h4>How will you build you rails</h4><p>The Dockerfile above is not enough. You could imagine it can help you install gems or something like bundle install. We’ll define this step in Dockerfile, so add the following lines:</p><pre>RUN mkdir /myapp<br>WORKDIR /myapp<br>ADD Gemfile /myapp/Gemfile<br>ADD Gemfile.lock /myapp/Gemfile.lock<br>RUN bundle install<br>ADD . /myapp</pre><p>In my point of view, what these lines do respectively are:</p><ol><li>Create a directory in the container called myapp</li><li>Copy Gemfile from local directory to the container, under myapp</li><li>Copy Gemfile.lock as well</li><li>Bundle install in local directory</li><li>Copy all stuff to the container</li></ol><p>But what are the stuff built from bundle install. Let’s go ahead to see what is needed in Gemfile.</p><h4>Install rails</h4><p>Although we have’t actually set up docker, you can know the container will have ruby environment, since it’s built based on that. And it seems like that ruby image include bundle tools. So, at this moment, one thing last to start rails is rails itself. Of course, you can install rails from gem install rails, besides, adding gem &#39;rails&#39; to Gemfile also works. Both methods enable you to use rails new.</p><p>We now choose the latter one. Therefore, Gemfile will looks like:</p><pre>source &#39;https://rubygems.org&#39;<br>gem &#39;rails&#39;, &#39;5.0.0.1&#39;</pre><p>And touch Gemfile.lock is required, though, for unknown reasons.</p><h4>Set up and link your services with docker-compose</h4><p>Think about our project. Is there anything else not satisfied? Where is the database?</p><p>In rails I used without docker, db is created when we use rails db:create. Rails have a convention on creating db, and database.yml follow that convention. Since in docker we run database in a different container, we’d better rewrite database.yml to link it to the right container. But before that, I need to set up a db. So write the following codes in docker-compose.yml:</p><pre>version: &quot;2&quot;<br>services:<br> db:<br>    image: postgres</pre><p>You can see, we use an image named postgres. It will run as the db container. And why version got 2 is not clear.</p><p>Another thing we will do in docker-compose is set up rails. In the former step, we only installed rails, instead of making it start.</p><p>If we call the rails service web, your docker-compose will have these codes:</p><pre>version: &quot;2&quot;<br>services:<br> db:<br>    image: postgres<br> web:<br>    build: .<br>    command: bundle exec rails s -p 3000 -b &#39;0.0.0.0&#39;<br>    volumes:<br>      - .:/myapp<br>    ports:<br>      - &quot;3000:3000&quot;<br>    depends_on:<br>      - db</pre><p>What’s the meaning of them?</p><p>First, build: . is just like docker build, which I introduced in <a href="https://medium.com/@siningliu/a-dockerfile-example-58f915cbaf44#.5evhb26nz">a previous article.</a> It tell docker that Dockerfile is in the current directory and build it.</p><p>Second, command: bundle exec rails s -p 3000 -b &#39;0.0.0.0&#39; is very readable for a rails developer, right? (Btw, I get an idea from this sentence that the degree of readability varies among people, instead of a intrinsic characteristic of things, and you can guess what determines your readability at this time and how will it change in the future).</p><p>What I can’t explain are volumes, ports, depends_on. I guess volumes represent your working directory, ports tell your ports accept traffic, and depends_on link rails and db.</p><h4>Create a rails project</h4><p>Would you run rails new to create a project, and how will docker create a project inside the container?</p><p>There is a trick. The first time you want to generate a project, you need to do docker-compose run web rails new . --force --database-postgresql.</p><p>I loss the information docker returned to me, but I guess docker run Dockerfile at first to build ruby env and gem install rails. and then rails new . And this is proved by <a href="https://docs.docker.com/compose/rails/#/build-the-project">docker official guide</a>.</p><p>Next time, when you edit Gemfile and what to build the project again, or what compose it on your server, just run docker-compose build, since you’ve generated the skeleton(many rails files) in the container, just build it again, instead of generating it again.</p><h4>Connect to database</h4><p>As we’ve said before, database connection should be written down at database.yml. There should be nothing mysterious. Look at the codes:</p><pre>development: &amp;default<br> adapter: postgresql<br> encoding: unicode<br> database: myapp_development<br> pool: 5<br> username: postgres<br> password:<br> host: db<br>test:<br>  &lt;&lt;: *default<br>  database: myapp_test</pre><p>Remember these information are only used to connect your db, instead of creating db. If you use the convention of rails to set up db, there is no need to change the yml file. However, we run postgres in a container like real db environment on server, so we need to add username and password to the yml file. At this time, we only use default configs.</p><h4>Run your rails</h4><p>After this, we can run rails with docker-compose up, and create db with docker-compose run web rake db:create. This order is suggested by the tutorial. But I guess it will be acceptable or even better with the reverse order.</p><p>In addition, one notice the tutorial saying is if you got A server is already runing, delete /tmp/pids/server.pid, as I’ve encountered before.</p><h4>Recall</h4><ol><li>Write the container’s base with FROM ruby:2.3.3 and RUN apt-get</li><li>Install rails through Gemfile (and Gemfile.lock) or gem install rails(not included).</li><li>Copy rails code to container with WORKDIR ADD RUN bundle install in Dockerfile</li><li>Define services, namely, db and web(rails), in docker-compose.yml. Specifically, write down build, setting up command, volumes, ports, and db.</li><li>Connect rails to db. Add username and password to configs.</li><li>Generate a project with docker-compose run web rails new . --force --database=postgres --skip-bundle. Or if you already have a project, docker-compose build</li><li>Run your project. docker-compose up, and create db, docker-compose run web rake db:create</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ada2bd81e89c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Dockerfile Example]]></title>
            <link>https://medium.com/@siningliu/a-dockerfile-example-58f915cbaf44?source=rss-48609ce6d0f2------2</link>
            <guid isPermaLink="false">https://medium.com/p/58f915cbaf44</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Sining  Liu]]></dc:creator>
            <pubDate>Thu, 16 Feb 2017 03:08:12 GMT</pubDate>
            <atom:updated>2017-02-16T03:08:12.600Z</atom:updated>
            <content:encoded><![CDATA[<p>This article is mainly focusing on how to build a docker image using Dockerfile. It’s very simple but can be seen as a minimum viable knowledge (MVK) on using Dockerfile. And since the simpleness, you can focus on the main idea of how Dockerfile works, instead of complicated code which no longer help in another project.</p><p><a href="https://docs.docker.com/engine/getstarted/step_four/">The example is from docker’s official documents, building a robot whale from a public image</a>, but some explanation is added in this article.</p><h4>Create your Dockerfile</h4><p>If you want docker to build an image from Dockerfile, you need to tell where the file is. For convenience, we’d better to make a new directory and put only one Dockerfile in it.</p><pre>mkdir docker-projects<br>cd docker-projects<br>touch Dockerfile</pre><h4>Write command line in Dockerfile</h4><p>There are only three lines we need to write in this Dockerfile. However, from my experience on exploring docker, this three lines are very representative. You probably will use them in any other project.</p><pre>FROM docker/whalesay:latest<br>RUN apt-get -y update &amp;&amp; apt-get install -y fortunes<br>CMD /usr/games/fortune -a | cowsay</pre><p>The first line tell docker which image the new project is based. If your machine has the image locally, docker will set it up and use it. If not, docker will pull the image from docker hub.</p><p>The 2nd line tell the container run apt-get command, which is set up from the first line. And if you have deployed something, like rails, on ubuntu server, you know this command is regular. As well, you can know that docker/whalesay is based on ubuntu.</p><p>The 3rd line, as the last line, uses CMD as final hit. usr/games/fortune must be a bin file that can run as a command. When docker reach the last line, it’ll run this bin file, and you’ll see the output. And I guess this CMD define the action that docker do when docker run &lt;image name&gt; is implemented.</p><p>Finally, save this file and turn to the terminal.</p><h4>Build image</h4><p>Now, we are ready to build the image. Run:</p><pre>docker build -t docker-whale .</pre><p>And you’ll see a lot of messages come out. Let’s explain them.</p><p>First, look at the command.</p><p>docker build tell docker that we want it to build something. And -t option help us add a tag to the image, namely, latest. The forth part, docker-whale will be the new image’s name. Finally, don’t forget the point, it declare that Dockerfile is in current directory.</p><p>Next, let’s look on the information after the build command</p><pre>❯ docker build -t docker-whale .<br>Sending build context to Docker daemon 2.048 kB<br>Step 1/3 : FROM docker/whalesay:latest<br>latest: Pulling from docker/whalesay<br>e190868d63f8: Pull complete<br>#...<br>8eed3712d2cf: Pull complete<br>Digest: sha256:178598e51a26abbc958b8a2e48825c90bc22e641de3d31e18aaf55f3258ba93b<br>Status: Downloaded newer image for docker/whalesay:latest<br> ---&gt; 6b362a9f73eb<br>Step 2/3 : RUN apt-get -y update &amp;&amp; apt-get install -y fortunes<br> ---&gt; Running in a72153fddc36<br>Ign http://archive.ubuntu.com trusty InRelease<br>Get:1 http://archive.ubuntu.com trusty-updates InRelease [65.9 kB]<br>#...<br>Setting up fortunes (1:1.99.1-7) ...<br>Processing triggers for libc-bin (2.19-0ubuntu6.6) ...<br> ---&gt; fa0d57a63ee3<br>Removing intermediate container a72153fddc36<br>Step 3/3 : CMD /usr/games/fortune -a | cowsay<br> ---&gt; Running in 91977f13a897<br> ---&gt; 10aa7a8b7ba0<br>Removing intermediate container 91977f13a897<br>Successfully built 10aa7a8b7ba0</pre><p>The line start with Sending build context shows the Dockerfile is sent to Docker daemon, so that it can be implemented. When this step was finished, docker will docker take action as you told him in Dockerfile.</p><p>As a result, you can see Step 1/3:, followed by the codes which are exactly the same as you wrote in Dockerfile, and other steps have the similar output as well.</p><p>As we have said before, the each line in Dockerfile will implement something, and now you see the result through the output. And a detailed knowledge is that after the image has been downloaded to local machine, docker set it up in an intermediate container, and run following step in that container. To prove that, you can read the message just after the Step 2, which says Running in a72153fddc36 . As the document of docker show, this long code represent an intermediate container’s id. Then, when this step has been finished, the intermediate would be removed, just as the message before Step 3 shows. And another intermediate container will created when docker turns to next step. You can read it from the messages as well.</p><p>Finally, a message saying successfully built come out, followed by a new container id, which is exactly the id of your new image’s container.</p><h4>Run the image</h4><p>After successfully build your image, you will see it appears in docker images.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/911/0*Q6x4sSorgKvYfcjZ.jpg" /></figure><p>Then, simply run this image with:</p><p>docker run docker-whale</p><p>It generate a whale saying something.</p><h4>Recognize the pattern</h4><p>From this example, I can tell the every step will appear again and again in other docker projects. Let’s recognize the pattern so that we can know what other docker project author is doing.</p><ol><li>Write Dockerfile with image base, namely, using From, and other commands that building ubuntu environment, etc.</li><li>Build the image by using docker build.</li><li>Run the image by using docker run</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=58f915cbaf44" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Docker Machine Example]]></title>
            <link>https://medium.com/@siningliu/a-docker-machine-example-f6623d2551fe?source=rss-48609ce6d0f2------2</link>
            <guid isPermaLink="false">https://medium.com/p/f6623d2551fe</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Sining  Liu]]></dc:creator>
            <pubDate>Sat, 11 Feb 2017 07:02:58 GMT</pubDate>
            <atom:updated>2017-02-11T07:02:58.396Z</atom:updated>
            <content:encoded><![CDATA[<p>I’m trying to use docker, but not familiar with docker-machine. Fortunately, there is <a href="https://docs.docker.com/machine/examples/aws/">a tutorial in docker’s website</a>, and I finish it successfully. To combine this part of knowledge with more advanced deployment, I should recall it so that I can master it and use it without much struggle( a little is acceptable, but too much will defeat you. I’m not saying that on should not take big challenge, instead, telling that it may be better to find a viable way).</p><h4>Install</h4><p>If you have installed docker on your Mac, you can use docker-machine command.</p><h4>Create your machine</h4><p>docker-machine can help you create a host with docker installed, and the only thing you need to do is to use docker-machine create command. In this situation, docker-machine is the command and create is the subcommand.</p><p>Moreover, you need to give some options as follows.</p><h4>Find your driver</h4><p>When you want to create a host with docker-machine, you need to tell which driver you want to use. The driver can be AWS EC2, which is exactly I chose, Digital Ocean, etc. You can find all supported drivers from <a href="https://docs.docker.com/machine/drivers/">this site</a>.</p><p>And then you need to declare the driver in your command with--driver option. So, by now, the command looks like docker-machine create --driver amazonec2</p><h4>Select your region</h4><p>For AWS China users, they can only choose the region as cn-north. For their international users, if the --region option is not offered, it will be a default region, which is us-east.</p><p>By now, the command looks like docker-machine create --driver amazonec2 --amazoneec2-region cn-north-1</p><h4>Provide your credentials</h4><p>If you want to create a host on AWS, you need to provide credentials or your request will be rejected. However, the credentials can be stored in a file named ~/.aws/credentails and there is no need to write them in your command, making your request safe and easier.</p><h4>Give a name for your host</h4><p>Now, we come to the final part of the command, host name. Just write something like app-production, which will become the name of your ec2 instance.</p><p>By now, the command looks like docker-machine create --driver amazonec2 --amazonec2-region cn-north-1 app-production</p><p>And then, press enter and wait. The docker( docker engine exactly) will be installed on your remote server. And if the process is crashed at some step, you can follow the suggestion given by docker machine, or delete the host with docker-machine rm app-production and start your process from beginning.</p><h4>Set up a simple environment</h4><p>If docker-machine return the message saying you’ve created a machine, you can do the next several steps.</p><h4>Check your host and go into the host’s env</h4><p>Use this command: docker-machine ls, and you will get the info about your host.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/828/0*vvR1x41A-8HrfF1p.jpg" /></figure><p>If your machine is not active(without the star sign in the ACTIVE column), you need to use docker-machine env app-production, which will setup an environment that can run command on app-production. And as you know, docker-machine is the command, env is the subcommand, and app-production is my server’s name(not showed in the picture above).</p><p>After this command, the docker will remind you to run eval $(docker-machine env stockholm-staging) . Do it, and you will get into the host’s env.</p><p>Although the command may have worked, you can’t really see the effect of it. To know it clearly, you can run docker-machine ip app-production and docker-machine inspect app-production, and if you get the host’s ip address and host info respectively, you have definitely succeed.</p><h4>Set up an nginx server</h4><p>Actually, you can have more interesting tests(as docker’s website says), by set up an nginx server. Run docker run -d -p 8000:80 --name webserver kitematic/hello-world-nginx.</p><p>Remember, the command will run on your remote server, instead of your local machine. It will pull something from docker hub, namely, the images of kitematic/hello-world-nginx. And I think the -d option represent daemon, -p and its value 8000:80 tell host to run on port 8000, and --name will be the container’s name(Just guess).</p><p>After the command is finished open your browser and go to your ip address with port 8000, you can see the page as below(as a prerequisite, you need to open inbound port 8000 from your AWS console ).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*3X2959nt0X52j800.jpg" /></figure><h4>Recall</h4><ol><li>Use docker-machine create with options like --driver, --amazonec2-region and the host name you want.</li><li>Go into the host’s env with docker-machine env &lt;machine-name&gt; followed by eval $(docker-machine env &lt;machine-name&gt;)</li><li>Check the env with docker-machine ip &lt;machine-name&gt; or docker-machine inspect &lt;machine-name&gt;</li><li>Set up a docker container, like docker run -d -p 8000:80 --name webserver &lt;docker hub name&gt;</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f6623d2551fe" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>