<?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 SteveLTN on Medium]]></title>
        <description><![CDATA[Stories by SteveLTN on Medium]]></description>
        <link>https://medium.com/@steveLTN?source=rss-f72f52854faa------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*kXyr1FBrYfRyWixpSj3VTw.jpeg</url>
            <title>Stories by SteveLTN on Medium</title>
            <link>https://medium.com/@steveLTN?source=rss-f72f52854faa------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 21:26:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@steveLTN/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[Understanding CSRF Tokens]]></title>
            <link>https://medium.com/steveltn/understanding-csrf-tokens-1a3b7bb02d87?source=rss-f72f52854faa------2</link>
            <guid isPermaLink="false">https://medium.com/p/1a3b7bb02d87</guid>
            <category><![CDATA[web-security]]></category>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[csrf]]></category>
            <category><![CDATA[web]]></category>
            <dc:creator><![CDATA[SteveLTN]]></dc:creator>
            <pubDate>Mon, 08 May 2017 21:54:13 GMT</pubDate>
            <atom:updated>2017-05-08T21:55:50.662Z</atom:updated>
            <content:encoded><![CDATA[<h4>Why they are important and how to make them effective</h4><h3>TL;DR</h3><p>CSRF tokens work. But not alone. To protect your site form CSRF attacks, you also need to:</p><ul><li>Set CORS headers correctly</li><li>Never mutate site state on GET requests</li></ul><p>CSRF stands for <a href="https://en.wikipedia.org/wiki/Cross-site_request_forgery">Cross-Site Request Forgery</a>. I’m not digging into details now as I’ll show it to you in just a second. All we need to know for now is that a malicious website can drive the users’ browsers to send requests to another website without the users’ acknowledgements.</p><p>Our experiment started with a discussion between my colleague Simon and I. We have an app with Rails backend and React.js fronted. We know that Rails has CSRF token verification by default. It verifies that the CSRF token in the request headers or in form data matches the one in the encrypted cookie on each non-GET request. But it’s painful to configure on a single-page app using, for instance, React.js. Since neither of us had enough knowledge of how CSRF tokens really work, we turned to Google. Unfortunately, we couldn’t find an article describes it clearly. So we decided to make some little experiments.</p><p>Though the examples here are in Rails, the general concept applies to other web frameworks as well.</p><h3>Hacking Our Own Site. Attempt 1</h3><p>We built a minimal Rails app and had it running on site.alice.com which we will call “Site Alice” from now on. Site Alice merely allows us to <a href="https://en.wikipedia.org/wiki/Create,_read,_update_and_delete">CRUD</a> posts. We deliberately turned off CSRF token verification and allowed CORS for all domains.</p><p>We also built a supposably malicious “Site Mallory”, who has only one static HTML page with the following snippet:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b2d4beb3930efcc2fa649f0fe6343c9c/href">https://medium.com/media/b2d4beb3930efcc2fa649f0fe6343c9c/href</a></iframe><p>We opened Site Mallory in our browser. Boom! A new post is created in Site Alice!</p><h4>What happened here?</h4><p>The JavaScript on Site Mallory drove user’s browser to send a POST request to Site Alice. When browsers do so, they include Site Alice’s cookies even if the request is initiated by Site Mallory. From Site Alice’s perspective, the user posted a post by him/herself. This all happened without the user knowing it — he/she merely opened a webpage.</p><h4>That’s what CSRF Tokens are for</h4><p>By adding protect_from_forgery to controllers, Rails:</p><ul><li>stores a CSRF token in the user’s encrypted cookie. This token doesn’t change as long as the cookie is not cleared</li><li>includes the token as a hidden field authenticity_token in all Rails-rendered forms</li><li>includes the token in AJAX request headers if you use jquery-rails</li><li>verifies the token in form data or in AJAX headers, make sure either of them matches the token stored in the cookie</li></ul><p>For single-page apps who most likely don’t send AJAX requests by jquery-rails, <a href="http://api.rubyonrails.org/classes/ActionView/Helpers/CsrfHelper.html">Rails API Doc</a> suggests you to render the CSRF token into a meta tag with helper csrf_meta_tag and:</p><blockquote>For AJAX requests other than GETs, extract the “csrf-token” from the meta-tag and send as the “X-CSRF-Token” HTTP header.</blockquote><p>So we added protect_from_forgery to our controller. It worked indeed. Site Mallory’s forged request would’t go through as Rails blocks it when verifying CSRF tokens.</p><p>You might ask, since we were to render CSRF token into HTML, what prevents us from sending a GET request and extract the token from the HTML using JavaScript? Let’s see our second attempt to hack Site Alice.</p><h3>Hacking Our Own Site. Attempt 2</h3><p>We changed the script tag on Site Mallory to:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa1bd8459d2d4d80a3d0b8a03a2a156e/href">https://medium.com/media/fa1bd8459d2d4d80a3d0b8a03a2a156e/href</a></iframe><p>The POST request went through ! All we did is just:</p><ol><li>send a GET request to fetch an HTML page which contains the CSRF token</li><li>extract the token and include it in the headers of next POST request.</li></ol><p>CSRF tokens didn’t seem to provide much protection at all.</p><h4>CORS Headers!</h4><p>Well, we are not totally vulnerable because we have <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS">CORS headers</a>! Let’s set up CORS headers properly so scripts on Site Mallory couldn’t send requests to Site Alice! Problem solved!</p><p>Wait a minute… If CORS solves the problem, why do we need CSRF tokens at all? Are they just redundant? Let’s find out with hacking attempt 3.</p><h3>Hacking Our Own Site. Attempt 3</h3><p>This time we have proper CORS headers, but no CSRF token verification. According to <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS">MDN</a>,</p><blockquote>For security reasons, browsers restrict cross-origin HTTP requests initiated from <strong>within scripts</strong>.</blockquote><p>What if it’s <strong>not initiated within scripts</strong>? To answer that, we have our third HTML snippet:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/093c4506c2aebe82fafdde8843e69606/href">https://medium.com/media/093c4506c2aebe82fafdde8843e69606/href</a></iframe><p>The value of authenticity_token is empty, but the request still went through! It turned out, <a href="https://en.wikipedia.org/wiki/Same-origin_policy">same-origin policy</a> doesn’t apply to form submissions! Ouch!</p><p>That made it pretty clear that proper CORS headers and CSRF token verification are both necessary. But is there another way in even if we use both of them?</p><h3>Hacking Our Own Site. Attempt 4</h3><p>This time the snippet on Site Mallory is extremely simple:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f9e2fdee7ba3b4a6787b006a33363180/href">https://medium.com/media/f9e2fdee7ba3b4a6787b006a33363180/href</a></iframe><p><strong>with a caveat</strong>: we temporary created another route who maps a GET request to posts#create action on Site Alice:</p><pre>get &#39;/posts/create&#39;, controller: &#39;posts#create&#39;</pre><p>When the page on Site Mallory loaded, we immediately see a new post created on Site Alice.</p><p>By default, same-origin policy only applies on requests other than GET , a simple malicious image link would trick the browser and create a post without user’s knowing it.</p><p>That why we should never mutate site state on GET requests.</p><h3>Conclusion</h3><p>Now we know why CSRF tokens are necessary and why itself is not enough.</p><p>In order to protect our sites from CSRF attack, we need to:</p><ul><li>Have CSRF token verification</li><li>Set CORS headers correctly</li><li>Never mutate site state on GET requests</li></ul><p>Now as we fully understand it, we know the key concept of CRSF tokens is that they are accessible by request initiated by its own site but not by other sites’ requests. To meet this requirement, rendering CSRF tokens into a meta tag is not the only way. To make it easier on our React site, we decided to keep it in Redux store.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1a3b7bb02d87" width="1" height="1" alt=""><hr><p><a href="https://medium.com/steveltn/understanding-csrf-tokens-1a3b7bb02d87">Understanding CSRF Tokens</a> was originally published in <a href="https://medium.com/steveltn">SteveLTN</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Brief Introduction to HTTPS-PORTAL]]></title>
            <link>https://medium.com/steveltn/a-brief-introduction-to-https-portal-80bb3286eebc?source=rss-f72f52854faa------2</link>
            <guid isPermaLink="false">https://medium.com/p/80bb3286eebc</guid>
            <category><![CDATA[lets-encrypt]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[https]]></category>
            <dc:creator><![CDATA[SteveLTN]]></dc:creator>
            <pubDate>Sun, 07 May 2017 00:47:12 GMT</pubDate>
            <atom:updated>2017-05-07T00:47:12.181Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Originally posted on 2016–02–08 on </em><a href="http://steveltn.me./"><em>http://steveltn.me.</em></a></p><h4>TL;DR</h4><p><a href="https://github.com/SteveLTN/https-portal">HTTPS-PORTAL</a> is a Docker image that turns your HTTP site into HTTPS with very little configuration. It works just like an HTTP server such as Nginx or Apache, but with HTTPS automatically configured.</p><h4>Starting from Let’s Encrypt</h4><p>Let’s Encrypt is great. It’s great in the way that it provides SSL certificate for free. And more importantly, with the introduction of ACME protocol, it made obtaining and renewing SSL certificates automated.</p><h4>But there are problems …</h4><p>It’s not easy enough to use. You can choose from either the official client or great third-party clients such as <a href="https://github.com/diafygi/acme-tiny">acme-tiny</a> and <a href="https://github.com/Neilpang/le">le</a>. But none of them seems easy enough to use for me.</p><p>Setting up HTTPS basically involves three things:</p><ol><li>Allowing Let’s Encrypt to verify your ownership of the domain</li><li>Configuring your HTTP server to use the certificates</li><li>Setting up timed jobs to renew the certificates automatically (because Let’s Encrypt certificates are short-lived)</li></ol><p>Unfortunately, none of the clients above can do all three things automatically.</p><h4>How HTTPS-PORTAL works</h4><p>HTTPS-PORTAL takes a different approach. It is an Docker image that works as an HTTP server by itself. It does all three things listed above, and requires minimal configuration.</p><p>HTTPS-PORTAL tries to make minimal impact to your host OS. All it does is to listen on port 80 and 443, and to proxy the incoming HTTPS request to an HTTP endpoint, which could be another Docker container or simply a web application listening to an arbitrary port. It is isolated with your host OS, therefore it doesn’t need to comprehend the host OS environment. It avoids the need to configure your HTTP server, because it ships its own. It doesn’t need to temporary take down your HTTP server running on port 80 while renewing the certificates, because it <strong>is</strong> the HTTP server running on 80 and it knows how to do the verification.</p><p>Internally, HTTPS-PORTAl contains an Nginx, plus a piece of controlling code to obtain and install certificates from Let’s Encrypt, as well as scheduled jobs to renew certificates when necessary. But if you treat it as a blackbox, it is a portal that converts your HTTP sites into HTTPS ones.</p><h4>Use cases</h4><p>HTTPS-PORTAL can be used in different ways. The following are some typical scenarios.</p><h4>Traditionally deployed web apps</h4><p>Say you are running a Ruby on Rails application, which listens to port 3000. Now you want it to run on HTTPS on the domain www.example.com. Basically the only thing you need to do is to run the container:</p><pre>docker run -p 80:80 -p 443:443 -e PRODUCTION=&quot;true&quot; \<br>  -e DOMAINS=&quot;www.example.com-&gt;http://dockerhost:3000&quot; steveltn/https-portal</pre><p>In the real world, to make everything work you need to at least point your DNS record to the hosting machine. And I recommend you to use docker-compose so you don&#39;t forget the command later. You can see examples on <a href="https://github.com/SteveLTN/https-portal">the GitHub repo</a>.</p><h4>Docker-deployed web apps</h4><p>If you already deployed your web apps via Docker, I assume you know how docker-compose works. <a href="https://medium.com/2015/12/18/nginx-acme/">This post</a> demonstrates how a WordPress instance can be set up by using Docker.</p><h4>Multiple dockerized apps on the same server</h4><p>Sometimes you have several small projects running on the same host. Docker is a great way to keep them isolated. But you want a single entrypoint to listen on port 80 &amp; 443, and proxies the request to the corresponding app based on the requested domain.</p><p>Greatly inspired by <a href="https://github.com/jwilder/nginx-proxy">jwilder’s nginx-proxy</a>, HTTPS-PORTAL features automatic container discovery, which is similar to the way nginx-proxy works. Actually, HTTPS-PORTAL uses the same library that powers nginx-proxy, which is also written by jwilder. With this setup, you can launch or take down apps without making other apps running on the same host temporarily unavailable. Details can be found on the <a href="https://github.com/SteveLTN/https-portal">the README page</a>. A big thank to jwilder here!</p><h4>Edit May 1 2016</h4><p>Well, this blog is not running on HTTPS-PORAL any more because I decided to save the cost of the VM for hosting it. I now just use GitHub pages instead.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=80bb3286eebc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/steveltn/a-brief-introduction-to-https-portal-80bb3286eebc">A Brief Introduction to HTTPS-PORTAL</a> was originally published in <a href="https://medium.com/steveltn">SteveLTN</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dead-simple HTTPS Set up with Docker and Let’s Encrypt]]></title>
            <link>https://medium.com/steveltn/dead-simple-https-set-up-with-docker-and-lets-encrypt-ee7c9916b6f4?source=rss-f72f52854faa------2</link>
            <guid isPermaLink="false">https://medium.com/p/ee7c9916b6f4</guid>
            <category><![CDATA[lets-encrypt]]></category>
            <category><![CDATA[https]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[SteveLTN]]></dc:creator>
            <pubDate>Sun, 07 May 2017 00:46:51 GMT</pubDate>
            <atom:updated>2017-05-07T00:46:51.463Z</atom:updated>
            <content:encoded><![CDATA[<h3>Dead-simple HTTPS Setup with Docker and Let’s Encrypt</h3><p><em>Originally posted on 2015–12–18 on http://steveltn.me.</em></p><p>Let’s Encrypt is great, it made HTTPS configuration simpler than ever before. But if you just want to set up HTTPS for your personal blog or small projects, things can be even simpler by using Docker.</p><p>Hence I created the little project <a href="https://github.com/steveltn/https-portal">HTTPS-PORTAL</a>, a Docker image that encapsulates Nginx, a Let’s Encrypt client and configurations for auto-renewals. Using it, you can set up your HTTPS site in a minute (excluding downloading and key-generation time). Let’s see how to set up a HTTPS-powered WordPress site using the image.</p><h4>See It in Action</h4><p>Before start, please have Docker toolchain installed on your local computer. Instructions can be found <a href="https://www.docker.com/">here</a>.</p><p>To deploy a web app, you need a Linux machine in the cloud. <a href="https://www.digitalocean.com/">DigitalOcean</a> is a really good cloud machine provider. The following example assumes that you use DigitalOcean. Before proceeding, you need to obtain an <strong>access token</strong> from DigitalOcean. There is no need to create machines before hand, we will create one with docker-machine.</p><h4>Create Cloud Machine</h4><p>docker-machine command is used to create host Linux machines to run Docker. Let&#39;s create a 1GB Linux machine on DigitalOcean. You need a name of the new machine, let&#39;s call it my_wordpress.</p><pre>docker-machine create \<br>    --driver digitalocean \<br>    --digitalocean-access-token &lt;your digitalocean access token&gt; \<br>    --digitalocean-size 1gb \<br>    my_wordpress</pre><p>When the creation succeeds, run eval $(docker-machine env my_wordpress) to populate environment variables for the docker client.</p><h4>Configure your DNS</h4><p>Now if you visit your DigitalOcean web console, you will see a new machine created. Get the IP address of that new machine in the web console, or just run:</p><pre>docker-machine ip my_wordpress</pre><p>After knowing the IP of your new machine, go to your DNS provider and create a record for wordpress.example.com that resolves to the IP.</p><h4>Prepare dokcer-compose file</h4><p>docker-compose is a great tool to define your service stack. It defines the relationships across your individual containers. Let&#39;s create a folder to place the compose file:</p><pre>mkdir my_wordpress &amp;&amp; cd my_wordpress</pre><p>Then create a docker-compose.yml:</p><pre># docker-compose.yml for my_wordpress site<br><br>https-portal:<br>  image: steveltn/https-portal<br>  ports:<br>    - 80:80<br>    - 443:443<br>  links:<br>    - wordpress<br>  environment:<br>    - &quot;DOMAINS=wordpress.example.com -&gt; http://wordpress&quot;<br><br>wordpress:<br>  image: wordpress<br>  links:<br>    - db:mysql<br><br>db:<br>  image: mariadb<br>  ports:<br>    - 3306:3306<br>  environment:<br>    MYSQL_ROOT_PASSWORD: &lt;your mysql password&gt;</pre><p>This compose file creates three containers.</p><p>The first one is HTTPS-PORTAL, the Nginx HTTP server with automatic HTTPS configuration. It opens port 80 and 443, and forwards the domain wordpress.example.com to the wordpress container.</p><p>The second container is WordPress itself. This image is provided by WordPress officially. It requires a MySQL database, so we link it and call our database container db.</p><p>The last one is MariaDB. It’s a fork of MySQL and is 100% compatible. Put your own password there. We map the port 3306 of MySQL to the host machine because in the future we might want to access the database directly. There is need to tell wordpress container of the MySQL password. WordPress container will figure it out automatically.</p><h4>Start the service</h4><p>In my_wordpress folder, run:</p><pre>docker-compose up</pre><p>The Docker daemon on the server will start downloading necessary files and initializing the containers. When it’s done, you can visit your website at https://wordpress.example.com. HTTP requests will be automatically redirected to HTTPS.</p><p>When you visit the site, you’ll find the certificate of the site is not trusted. That’s because Let’s Encrypt currently has a tight rate limit of issuing certificates. To prevent wasting a certificate for mis-configuration, HTTPS-PORTAL starts in staging mode by default. You can ignore the warning and test that everything works. After that, press Ctrl-C to stop the containers.</p><h4>Switch to Production mode</h4><p>Once you confirm that the site functions well, you can edit your docker-compose.yml, Add a line:</p><pre>- &quot;DOMAINS=wordpress.example.com -&gt; http://wordpress&quot;<br>    - &quot;PRODUCTION=true&quot;                # &lt;- New line to enable production mode</pre><p>Start the containers again in detached mode:</p><pre>docker-compose up -d</pre><p>Your site is ready!</p><p>Update: We’ve renamed Nginx-ACME to <a href="https://github.com/steveltn/https-portal">HTTPS-PORTAL</a>, which describes better what it does.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ee7c9916b6f4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/steveltn/dead-simple-https-set-up-with-docker-and-lets-encrypt-ee7c9916b6f4">Dead-simple HTTPS Set up with Docker and Let’s Encrypt</a> was originally published in <a href="https://medium.com/steveltn">SteveLTN</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Brief Note for JavaScript Garden]]></title>
            <link>https://medium.com/steveltn/a-brief-note-for-javascript-garden-8d09d0b646c8?source=rss-f72f52854faa------2</link>
            <guid isPermaLink="false">https://medium.com/p/8d09d0b646c8</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[SteveLTN]]></dc:creator>
            <pubDate>Sun, 07 May 2017 00:46:17 GMT</pubDate>
            <atom:updated>2017-05-07T14:22:04.111Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Originally posted on 2015–10–31 on </em><a href="http://steveltn.me./"><em>http://steveltn.me.</em></a></p><p>Recently I read about <a href="http://bonsaiden.github.io/JavaScript-Garden/">JavaScript Garden</a>, and took some (extremely brief) notes.</p><h4>Objects</h4><ul><li>Everything is an object, except for null and undefined.</li><li>Object.delete deletes a key.</li><li>{ k: v } and { &#39;k&#39;: v } are equivalent.</li></ul><h4>Prototypes</h4><ul><li>Instances do not have prototypes by default, their constructors do.</li><li>Prototype should be an instance, instead of constructors.</li><li>in and for in both traverse the prototype chain.</li></ul><h4>this</h4><ul><li>There are <a href="http://bonsaiden.github.io/JavaScript-Garden/#function.this">five scenarios</a>.</li></ul><h4>Constructor</h4><ul><li>this refers to the new object.</li></ul><h4>Scope</h4><ul><li>There is no block scope</li><li>foo = 42 defines global variable; var foo = 42 defines scope variable.</li><li>Only function arguments and variables defined by var are scope variables.</li><li>var and function statement will be moved to the top of their enclosing scope.</li></ul><h4>Arrays</h4><ul><li>Don’t use for in because it traverses the prototype chain. Use for instead.</li><li>You can truncate arrays by assigning length property.</li><li>new Array() is broken. Use [1, 2, 3] instead.</li></ul><h4>Types</h4><ul><li>== is broken, it <a href="http://bonsaiden.github.io/JavaScript-Garden/#types.equality">coerces types</a>. === doesn&#39;t.</li><li>== and === compares identity instead of equality, when either side of it is an object.</li><li>typeof and instanceof are <a href="http://bonsaiden.github.io/JavaScript-Garden/#types.typeof">broken</a>.</li><li>The correct way of getting an object’s class is by using Object.prototype.toString.</li><li>The only use of typeof is to check if a variable is defined.</li><li>The only use of instanceof is to deal with custom made objects that originate from the same JavaScript context</li><li>JavaScript will apply type coercion wherever possible.</li></ul><h4>Core</h4><ul><li>The scope of eval is <a href="http://bonsaiden.github.io/JavaScript-Garden/#core.eval">broken</a>.</li><li>setTimeout and setInterval calls eval internally, if the arguments are strings.</li><li>You can override the global variable undefined.</li><li>JavaScript could <a href="http://bonsaiden.github.io/JavaScript-Garden/#core.semicolon">guess wrong</a> when automatically inserting semicolons.</li><li>delete doesn&#39;t work when DontDelete attribute is set, hence can only be used to delete properties explicitly set on normal objects.</li></ul><h4>setTimeout and setInterval</h4><ul><li>The timeout and interval are inaccurate.</li><li>this in the callback refers to the global object</li><li>setInterval can be blocked, causing function calls stacking. You can use setTimeout and set new timeouts in the callback instead.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8d09d0b646c8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/steveltn/a-brief-note-for-javascript-garden-8d09d0b646c8">A Brief Note for JavaScript Garden</a> was originally published in <a href="https://medium.com/steveltn">SteveLTN</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deploy Rails Applications using Docker]]></title>
            <link>https://medium.com/steveltn/deploy-rails-applications-using-docker-4eccdcf05da0?source=rss-f72f52854faa------2</link>
            <guid isPermaLink="false">https://medium.com/p/4eccdcf05da0</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[SteveLTN]]></dc:creator>
            <pubDate>Sun, 07 May 2017 00:45:35 GMT</pubDate>
            <atom:updated>2017-05-07T00:45:49.412Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Originally posted on 2014–03–15 on </em><a href="http://steveltn.me./"><em>http://steveltn.me.</em></a></p><p><strong><em>Warning</em></strong><em>: Things changed a lot since 2014. The way introduced in this post is no longer the best practice anymore.</em></p><h3>What is Docker?</h3><p><a href="https://www.docker.io/">Docker</a> is “an open source project to pack, ship and run any application as a lightweight container”. It works much like a virtual machine, wraps everything (filesystem, process management, environment variables, etc.) into a container. Unlike a VM, it uses <a href="https://linuxcontainers.org/">LXC</a> (Linux kernel container) instead of a hypervisor. LXC doesn’t have its own kernel, but shares the Linux kernel with the host and other containers instead. Based on LXC, Docker is so lightweight, that it introduces almost no performance drawback while running the application.</p><p>Docker also provides a smart way to manage your images. Through Dockerfile and its caching mechanism, one can easily redeploy an updated image without transferring large amounts of data.</p><h3>Why Docker?</h3><p>Because you never know whether your application will work on the server even if you have run and tested it out on you localhost. That’s because the environment on the server differs from your computer so much. The configuration of RVM, ruby, gemsets differs. If we can test the application in our localhost before deploy, and we know that if it works on localhost, it works on the server, we can save a lot of time debugging deployment issues on the server.</p><p>For massive deployments, using an VM image is really handy. You create a VM instance and apply your image, and it works in minutes. Besides the convenience, however, there are problems:</p><ol><li>You have to upload the whole new image even if you just made a small update.</li><li>There is a significant performance loss.</li><li>Your probably run your application on a VPS, which is already a virtualized environment. You can’t run a VM on top of another.</li></ol><p>In the case of Docker, respectively:</p><ol><li>You don’t have to upload the whole image again. Docker is based on AuFS, which tracks the diff of the whole filesystem.</li><li>The performance loss is ignorable since it runs on the host kernel.</li><li>You can run Docker on a VM because Docker is not a VM.</li></ol><h3>Magic of layers</h3><p>Of course you can use Docker container in the same way as you use a VM image: Upload your container, run it and replace the whole image when updating it. But that’s not the preferred way. To understand how Dockerfile works, let’s do experiments step by step. You probably want to know:</p><ul><li>An <strong>image</strong> is an image of a filesystem, like a disk image</li><li>a <strong>container</strong> is an instance of an image, like a VM instance</li><li>Docker needs to be run with root privelege</li></ul><p>##Helloworld</p><p>First, get a Linux machine, <a href="https://www.docker.io/gettingstarted/">install Docker</a> on it. To make things easier, I used a <a href="https://www.digitalocean.com/">DigitalOcean</a> $5/month instance, with “Docker 0.8 on Ubuntu 13.10” image. It saves my time to install Docker.</p><p>In order to print a Helloworld message, we need a working docker container. The easiest way of obtaining it is fetching one from Docker official registry. A docker registry is much like a GitHub for docker images. Run:</p><pre>host# docker run ubuntu /bin/echo hello world</pre><p>where:</p><ul><li>ubuntu is the name of image from official registry. Normally you use USERNAME/TAG to identify an image on the registry. But if you skip the USERNAME, it means the official Docker image.</li><li>/bin/echo hello world is the command you run in the container.</li></ul><p>The output looks like this:</p><pre>host# docker run ubuntu /bin/echo hello world<br>Unable to find image &#39;ubuntu&#39; (tag: latest) locally<br>Pulling repository ubuntu<br>9f676bd305a4: Download complete<br>9cd978db300e: Download complete<br>eb601b8965b8: Download complete<br>5ac751e8d623: Download complete<br>9cc9ea5ea540: Download complete<br>511136ea3c5a: Download complete<br>6170bb7b0ad1: Download complete<br>321f7f4200f4: Download complete<br>f323cf34fd77: Download complete<br>1c7f181e78b9: Download complete<br>7a4f87241845: Download complete<br>hello world</pre><p>Each line of download is for a <a href="http://docs.docker.io/en/latest/terms/layer/">layer</a>.</p><p>Docker <strong>DOESN’T</strong> write into the image. Instead, it creates a <strong>layer</strong> on top of the existing image, which contains the modifications you made to the filesystem. Migrating from a previous state of filesystem to a recent one is applying one or more layers on top of the old image, just like patching files.</p><p>When a container is stopped, you can commit it. Committing a container is creating an additional layer on top of the base image. As expected, the official ubuntu image is also made up of several layers.</p><p>After downloading the image, docker boots a container from the image, runs the command, and prints the result. In our case, the printed result is the last line hello world.</p><h4>Modify your image and commit it</h4><p>Let’s try to modify the image and commit it.</p><p>First, start an interactive shell:</p><pre>host# docker run -i -t ubuntu /bin/bash</pre><p>Notice that the shell pops up instantly. The time cost to start a shell consists two parts: downloading time and booting time. Since we already downloaded the image when doing the helloworld, docker caches it. The booting time is ignorable, because unlike a VM, a Docker container doesn’t boot the kernel, doesn’t start system service, etc…</p><p>Now let’s install a random software, for example, nginx. Run the following command in the container shell:</p><pre>root@54e8da3b1db0:/# apt-get update<br>root@54e8da3b1db0:/# apt-get install -y nginx<br>root@54e8da3b1db0:/# exit</pre><p>You are back to your host shell. run</p><pre>host# docker ps -l</pre><p>to list your containers. You’ll see something like:</p><pre>CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES<br>54e8da3b1db0        ubuntu:12.04        /bin/bash           5 minutes ago       Exit 0                                  clever_einstein</pre><p>By far your changes are not committed. Without commiting, you cannot use it as a base image. To commit it, run</p><pre>root@docker-toy-machine:~# docker commit -m &quot;Install Nginx&quot; 54e8 steveltn:nginx<br>4e66300102f4218b312fb4352221682ff42614351b18506e51792491e432111d</pre><p>where 54e8 is the first characters of the container ID. Check the available images:</p><pre>root@docker-toy-machine:~# docker images<br>REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE<br>steveltn            nginx               4e66300102f4        4 seconds ago       237.6 MB<br>ubuntu              13.10               9f676bd305a4        5 weeks ago         178 MB<br>ubuntu              saucy               9f676bd305a4        5 weeks ago         178 MB<br>ubuntu              13.04               eb601b8965b8        5 weeks ago         166.5 MB<br>ubuntu              raring              eb601b8965b8        5 weeks ago         166.5 MB<br>ubuntu              12.10               5ac751e8d623        5 weeks ago         161 MB<br>ubuntu              quantal             5ac751e8d623        5 weeks ago         161 MB<br>ubuntu              10.04               9cc9ea5ea540        5 weeks ago         180.8 MB<br>ubuntu              lucid               9cc9ea5ea540        5 weeks ago         180.8 MB<br>ubuntu              12.04               9cd978db300e        5 weeks ago         204.4 MB<br>ubuntu              latest              9cd978db300e        5 weeks ago         204.4 MB<br>ubuntu              precise             9cd978db300e        5 weeks ago         204.4 MB</pre><p>You can see the nginx image is created. You can use your new image as a base, you can run another command on it now. But this time let’s run a service on it.</p><h4>Run Nginx in a container</h4><p>Usually, after you run nginx form terminal, it spawns several processes and the master process exit. In that case, docker will terminate it&#39;s container. To prevent that, we want to modify the Nginx configuration file to keep the master process running after spawning.</p><pre>host# docker run 4e66300102f4 /bin/bash -lc &#39;echo &quot;daemon off;&quot; &gt;&gt; /etc/nginx/nginx.conf&#39;<br>host# docker ps -l<br>CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES<br>9352f354c4ab        steveltn:nginx      /bin/bash -lc echo &quot;   42 seconds ago      Exit 0                                  romantic_curie<br>host# docker commit -m &quot;Turn off nginx daemon mode&quot; 9352f354c4ab steveltn:nginx<br>bc94dfbdc83a894e8837769ea8a52d93a4fa8628bf3a1b8748e3a5ffbfd9a760<br>host# docker images<br>REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE<br>steveltn            nginx               bc94dfbdc83a        14 seconds ago      237.6 MB</pre><p>You can see the new image ID is the same as the result of commit. As you can see, the process of modifying an image is run a command on a image (which creates a container from image), then commit the container to make a new image. By doing this, a new layer is added to the image.</p><p>Next step is to run nginx as a service.</p><pre>docker run -p 80:80 bc94dfbdc83a /usr/sbin/nginx</pre><p>-p 80:80 forwards the 80 port of host to the 80 port of the container. bc94dfbdc83a is the image ID.</p><p>Now visit the 80 port of the host, you’ll see the familiar “Welcome to nginx!”. Yay!</p><p>You can use docker ps to see all Docker processes and us docker kill to terminate them.</p><h3>Using Dockerfile</h3><p>Until now we are doing everything manually, step by step. This is not what we want for automated deployment. In order to achieve automation, we need to introduce Dockerfile.</p><p>A Dockerfile is a configuration file for Docker, which specifies how to create your customized container from a base image. Instead of running the commands line by line, we just write all of them into the Dockerfile. For example, for the previous container running an nginx, we create a Dockerfile like this:</p><pre># Dockerfile for installing and running Nginx<br><br># Select ubuntu as the base image<br>FROM ubuntu<br><br># Install nginx<br>RUN apt-get update<br>RUN apt-get install -y nginx<br>RUN echo &quot;daemon off;&quot; &gt;&gt; /etc/nginx/nginx.conf<br><br># Publish port 80<br>EXPOSE 80<br><br># Start nginx when container starts<br>ENTRYPOINT /usr/sbin/nginx</pre><p>As you might have guessed,</p><ul><li>FROM means to choose a base image from the registry</li><li>RUN means to run a command in the container</li><li>EXPOSE means to expose a port to the host</li><li>ENTRYPOINT is the initialization command for the container when running it</li></ul><p>You can put this Dockerfile on the server and build from the directory containing it. But Docker provides something better. If you provide a <a href="http://github.com/">GitHub</a> repository, it will clone the repository, use it as the context, and load the Dockerfile in the root of the repository. So I just do:</p><pre>host# docker build -t steveltn/nginx github.com/steveltn/toy-rails-project-for-docker<br>Step 0 : FROM ubuntu<br> ---&gt; 9cd978db300e<br>Step 1 : RUN apt-get update<br> ---&gt; Running in 7395bc0c6b70<br>    === suppressed ===<br> ---&gt; 6ad2ab717026<br>Step 2 : RUN apt-get install -y nginx<br> ---&gt; Running in c7e1df6ef59a<br>    === suppressed ===<br> ---&gt; 6ce63dc3c19d<br>Step 3 : RUN echo &quot;daemon off;&quot; &gt;&gt; /etc/nginx/nginx.conf<br> ---&gt; Running in fdc4954637a4<br> ---&gt; 41144f01b920<br>Step 4 : EXPOSE 80<br> ---&gt; Running in e31a205de745<br> ---&gt; 15d5c1e287c2<br>Step 5 : ENTRYPOINT /usr/sbin/nginx<br> ---&gt; Running in 17a5e24835d9<br> ---&gt; 7d09c4e0c09c<br>Successfully built 7d09c4e0c09c</pre><p>If you run docker ps -l to see all of the containers, you will find the newly built one. But since we tagged it by -t steveltn/nginx , we can use the tag as a reference. Next step, let&#39;s boot the container.</p><pre>host# docker run -p 80:80 steveltn/nginx</pre><p>Visit port 80, we now have exactly the same nginx as before. You can use -d to run the container as a daemon.</p><p>Now try to build the container again. The building command finishes in less than a second! Why is it so fast? It turns out Docker will cache the intermediate state (in the form of layers) between each RUN command in the Dockerfile. When you run the same command on the same container, Docker just applies the layer created by the last run of that command instead of running it again on top of the current filesystem. That&#39;s why the apt-get update and apt-get install -y nginx finished instantly. The layer magic is achieved by <a href="http://aufs.sourceforge.net/">AuFS</a>, which essential works as a version control system like git (without forking and merging).</p><h3>Deploy a Rails project</h3><p>To keep things simple, I just created an empty rails project. To simulate the common condition, I used <a href="https://github.com/defunkt/unicorn">Unicorn</a> as the Rack server.</p><p>We put the Dockerfile in the home directory of the Rails project, and add a directory /config/container to store configuration files for services in the container, e.g. nginx.</p><p>The Dockerfile looks like this:</p><pre># Dockerfile for a Rails application using Nginx and Unicorn<br><br># Select ubuntu as the base image<br>FROM ubuntu<br><br># Install nginx, nodejs and curl<br>RUN apt-get update -q<br>RUN apt-get install -qy nginx<br>RUN apt-get install -qy curl<br>RUN apt-get install -qy nodejs<br>RUN echo &quot;daemon off;&quot; &gt;&gt; /etc/nginx/nginx.conf<br><br># Install rvm, ruby, bundler<br>RUN curl -sSL https://get.rvm.io | bash -s stable<br>RUN /bin/bash -l -c &quot;rvm requirements&quot;<br>RUN /bin/bash -l -c &quot;rvm install 2.1.0&quot;<br>RUN /bin/bash -l -c &quot;gem install bundler --no-ri --no-rdoc&quot;<br><br># Add configuration files in repository to filesystem<br>ADD config/container/nginx-sites.conf /etc/nginx/sites-enabled/default<br>ADD config/container/start-server.sh /usr/bin/start-server<br>RUN chmod +x /usr/bin/start-server<br><br># Add rails project to project directory<br>ADD ./ /rails<br><br># set WORKDIR<br>WORKDIR /rails<br><br># bundle install<br>RUN /bin/bash -l -c &quot;bundle install&quot;<br><br># Publish port 80<br>EXPOSE 80<br><br># Startup commands<br>ENTRYPOINT /usr/bin/start-server</pre><p>The ADD directive is too copy the files from the context (the directory/GitHub repository in which you build the image) to a path in the container filesystem. When running commands after adding files, Docker will automatically decide whether to use a cached layer depending on whether the added files have been changed.</p><p>The config/container/nginx-sites.conf file look like this:</p><pre># nginx-sites.conf<br><br>server {<br>    root /rails/public;<br>    server_name 95.85.4.231 _;<br>    location / {<br>        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<br>        proxy_redirect off;<br>        proxy_set_header Host $http_host;<br>        if (!-f $request_filename) {<br>            proxy_pass http://localhost:8080;<br>            break;<br>        }<br>    }<br>}</pre><p>start-server.sh:</p><pre># start-server.sh<br><br>#!/bin/bash<br>cd /rails<br>source /etc/profile.d/rvm.sh<br>bundle exec unicorn -D -p 8080<br>nginx</pre><p>Now we have everything ready. On any server with Docker installed, run</p><pre>host# docker build -t steveltn/nginx github.com/steveltn/toy-rails-project-for-docker<br>host# docker run -p 80:80 steveltn/nginx</pre><p>for building and running the image respectively. Two commands, everything.</p><h3>Tips and Thoughts</h3><p>0. I just run the Rails app in development environment for simplicity. For a proper Rails application, you should use production environment.</p><ol><li>Since we replace the image when redeploy, database shouldn’t be in the Docker container.</li><li>In our example, we use the official ubuntu image as the base and do everything on top of it. You can always install everything you need in advance, push the image to the <a href="https://index.docker.io/">Docker Index</a>, and use your own image as the base image. This reduces the flexibility, but increases the consistency. You definitely don’t want bundle install in the image because you are likely to change the Gemfile of your project, but putting apt-get install is a good idea.</li><li>If you are not comfortable with share you image, you can <a href="http://blog.docker.io/2013/07/how-to-use-your-own-registry/">create your own registry</a>, or use a private registry like <a href="https://quay.io/">Quay</a>.</li><li>You can use Capistrano to automate the deploy process.</li><li>To avoid doing a bundle install from scratch after modifying any file, see <a href="http://ilikestuffblog.com/2014/01/06/how-to-skip-bundle-install-when-deploying-a-rails-app-to-docker/">this blog post</a>.</li><li>I don’t know how to do a zero-downtime deployment right now.</li><li>Docker currently supports Linux only. If you use OS X as I do, you probably want to use <a href="http://www.vagrantup.com/">Vagrant</a> to test your build before deploying.</li></ol><p>You can find the Rails repository <a href="https://github.com/SteveLTN/toy-rails-project-for-docker">here</a>.</p><p>As mentioned by <a href="https://twitter.com/nonsens3">@nonsens3</a>, “You can have a zero downtime deployment with a proxy like hipache. It would route traffic to the new container when its ready.”</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4eccdcf05da0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/steveltn/deploy-rails-applications-using-docker-4eccdcf05da0">Deploy Rails Applications using Docker</a> was originally published in <a href="https://medium.com/steveltn">SteveLTN</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>