<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.8.5">Jekyll</generator><link href="https://mping.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://mping.github.io/" rel="alternate" type="text/html" /><updated>2019-09-25T10:27:21+00:00</updated><id>https://mping.github.io/feed.xml</id><title type="html">Miguel</title><subtitle>Random thoughts</subtitle><entry><title type="html">On Distributed Stuff</title><link href="https://mping.github.io/distributed/" rel="alternate" type="text/html" title="On Distributed Stuff" /><published>2019-09-17T12:10:31+00:00</published><updated>2019-09-17T12:10:31+00:00</updated><id>https://mping.github.io/distributed</id><content type="html" xml:base="https://mping.github.io/distributed/">&lt;p&gt;–
Feel free to correct any inaccuracies
–&lt;/p&gt;

&lt;h3 id=&quot;sources&quot;&gt;sources&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;www.bailis.org/blog/linearizability-versus-serializability/&lt;/li&gt;
  &lt;li&gt;https://dddpaul.github.io/blog/2016/03/17/linearizability-and-serializability/&lt;/li&gt;
  &lt;li&gt;https://aphyr.com/posts/313-strong-consistency-models&lt;/li&gt;
  &lt;li&gt;http://gvsmirnov.ru/blog/tech/2014/02/10/jmm-under-the-hood.html&lt;/li&gt;
  &lt;li&gt;http://preshing.com/20120625/memory-ordering-at-compile-time/&lt;/li&gt;
  &lt;li&gt;https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html&lt;/li&gt;
  &lt;li&gt;https://www.logicbig.com/tutorials/core-java-tutorial/java-multi-threading/happens-before.html&lt;/li&gt;
  &lt;li&gt;internet&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;intro&quot;&gt;intro&lt;/h2&gt;

&lt;p&gt;Today I want to share what I learned about distributed systems.&lt;/p&gt;

&lt;p&gt;There are a couple of notions that are intrinsic to any kind of system where the parties have to agree (or not) on values, the so-called &lt;strong&gt;consistency models&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Why would I want to talk about this topic, if there are smarter people already explaining it? I feel there’s a huge gap between people that are starting to research these concepts and people that already employ them everyday. Terms like consistency model, compare-and-swap, wait-free, processes, total ordering, histories, etc can be confusing to someone who’s starting, so I’ll try and fill the gap.&lt;/p&gt;

&lt;h2 id=&quot;consistency-models&quot;&gt;Consistency Models&lt;/h2&gt;

&lt;p&gt;Consistency models define how memory operations apply within a distributed system. Right off the bat, let’s dissect a bit:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Distributed means there’s more than one process (can be a thread, or a different host)&lt;/li&gt;
  &lt;li&gt;On a distributed system, operations (read or write) may take some time (there’s latency involved), and within that time the system is processing other reads and writes&lt;/li&gt;
  &lt;li&gt;Consistency models can be enforced by your RDBMS (MySQL) but also by your CPU; it doesn’t necessary apply just to multiple hosts&lt;/li&gt;
  &lt;li&gt;If consistency models are enforced correctly (ie, if they do what they say the do), they help developers reason correctly about the program&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;linearizability&quot;&gt;Linearizability&lt;/h3&gt;

&lt;p&gt;What &lt;strong&gt;linearizable&lt;/strong&gt; means is that &lt;strong&gt;when&lt;/strong&gt; you tell your distributed system to write a value and it ACKs the write, all subsequent reads &lt;strong&gt;will&lt;/strong&gt; get the written value. Basically it says that all reads and writes should appear atomic from the point of view of the processes that read/write the value.&lt;/p&gt;

&lt;p&gt;What would NOT be a linearizable execution? Here’s a snippet courtesy of Stack Overflow:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;public class Main extends Thread {

    boolean keepRunning = true; //should be volatile

    public void run() {
        while (keepRunning) {}
        System.out.println(&quot;Thread terminated.&quot;);
    }

    public static void main(String[] args) throws InterruptedException {
        Main t = new Main();
        t.start();
        Thread.sleep(1000);
        t.keepRunning = false;
        System.out.println(&quot;keepRunning set to false.&quot;);
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;keepRunning&lt;/code&gt; is shared between two threads (thread &lt;code class=&quot;highlighter-rouge&quot;&gt;t&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;main&lt;/code&gt; thread)&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;main&lt;/code&gt; writes to shared value &lt;code class=&quot;highlighter-rouge&quot;&gt;keepRunning&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;because we are not enforcing any consistency model, &lt;code class=&quot;highlighter-rouge&quot;&gt;keepRunning&lt;/code&gt; is always &lt;code class=&quot;highlighter-rouge&quot;&gt;true&lt;/code&gt; from &lt;code class=&quot;highlighter-rouge&quot;&gt;main&lt;/code&gt;’s point of view; so the program won’t stop.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A way to fix this is to make &lt;code class=&quot;highlighter-rouge&quot;&gt;keepRunning&lt;/code&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;volatile&lt;/code&gt;, thus enforcing linearizability. Linearizable means that when a write to &lt;code class=&quot;highlighter-rouge&quot;&gt;keepRunning&lt;/code&gt; is finished (by executing &lt;code class=&quot;highlighter-rouge&quot;&gt;keepRunning = false&lt;/code&gt;), eventually the other thread will see the new value.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;volatile&lt;/code&gt; keyword takes care of that because it makes sure reads and writes are atomic. So why wouldn’t setting a variable be atomic?&lt;/p&gt;

&lt;h4 id=&quot;short-digression-on-cpu-architectures&quot;&gt;Short digression on CPU architectures&lt;/h4&gt;

&lt;p&gt;Modern systems can have multiple CPUs and multiple representations of the same value; typically a CPU will have registers, caches (L1, L2, L3), and main memory (the DIMMs on your motherboard). Registers are much faster to access than L1, L1 is faster than L2 and so on. Besides, a single assembly operation has some latency, so the CPU makes all sorts of tricks to make the code fast, including pipelining, reordering, speculative execution, etc.&lt;/p&gt;

&lt;p&gt;Compilers can also reorder generated assembly as long as they deem it safe.
So how to fix this conundrum? Well, it depends on the language and the runtime (and the architecture). In case of JVM-based languages, the JVM has your back. The Java Language Specification introduced &lt;strong&gt;semantics&lt;/strong&gt; that define what to expect, the so-called &lt;em&gt;within-thread as-if-serial semantics&lt;/em&gt;, meaning that for a single &lt;code class=&quot;highlighter-rouge&quot;&gt;Thread&lt;/code&gt;, the program order will be what that &lt;code class=&quot;highlighter-rouge&quot;&gt;Thread&lt;/code&gt; observes:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;new Runnable(() -&amp;gt; {
  a = 1;
  b = 2;
  c = 0;
  // this thread will observe value in the program order
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;BUT&lt;/strong&gt;, what happens between threads?&lt;/p&gt;

&lt;h3 id=&quot;happens-before--synchronizes-with&quot;&gt;Happens-Before &amp;amp; synchronizes-with&lt;/h3&gt;

&lt;p&gt;Happens-before is a &lt;strong&gt;guarantee&lt;/strong&gt; that the language &amp;amp; runtime gives about the behaviour of a program. It’s a guarantee about the relation between reads and writes. It basically says that if instruction &lt;code class=&quot;highlighter-rouge&quot;&gt;A&lt;/code&gt; has a &lt;em&gt;happens-before&lt;/em&gt; relation with &lt;code class=&quot;highlighter-rouge&quot;&gt;B&lt;/code&gt;, the results of &lt;code class=&quot;highlighter-rouge&quot;&gt;A&lt;/code&gt; will surely be observed in &lt;code class=&quot;highlighter-rouge&quot;&gt;B&lt;/code&gt;. It’s not about actually happening before in wallclock time, it’s about what &lt;em&gt;happens-before&lt;/em&gt; (&lt;code class=&quot;highlighter-rouge&quot;&gt;A&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;B&lt;/code&gt;) will be observed as if it happened before  - the actual assembly can happen in any order.&lt;/p&gt;

&lt;p&gt;AFAIK, &lt;em&gt;synchronizes-with&lt;/em&gt; is &lt;em&gt;happens-before&lt;/em&gt; within Threads.&lt;/p&gt;

&lt;p&gt;So, in a single-threaded program, there’s a &lt;em&gt;happens-before&lt;/em&gt; guarantee of program order, meaning that each source code line has a &lt;em&gt;happens-before&lt;/em&gt; relation with the next line. &lt;b&gt;&lt;em&gt;Happens-before&lt;/em&gt; is transitive too.&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;What about multi-thread programs? The Java spec defines some ways of establishing a &lt;em&gt;happens-before&lt;/em&gt; relation:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Single thread and program order&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Monitor locks and unlocks (&lt;code class=&quot;highlighter-rouge&quot;&gt;synchronized&lt;/code&gt; keyword)&lt;/p&gt;

    &lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  //T1
  synchronized(lock) {...}//monitor unlock
	
  //T2
  //monitor lock
  synchronized(lock) {...}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Thread &lt;code class=&quot;highlighter-rouge&quot;&gt;start&lt;/code&gt; and all of thread’s actions&lt;/p&gt;

    &lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  Thread.start(() -&amp;gt; {a=1; b=2;})
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Thread &lt;code class=&quot;highlighter-rouge&quot;&gt;join&lt;/code&gt; and all of the joining thread’s actions&lt;/p&gt;

    &lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  //T1
  t = Thread.start(() -&amp;gt; {a=1; b=2;})
	
  //T2
  t.join()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Volatile writes and reads&lt;/p&gt;

    &lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  //T1
  volatile int a = 0;
  ...
  a = 1
	
  //T2
  if (a == 1) ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;p&gt;Coming back, so what exactly does the JVM do?&lt;/p&gt;

&lt;p&gt;By calling javap on the classfile &lt;code class=&quot;highlighter-rouge&quot;&gt;javap -h -p out.Main&lt;/code&gt; you will see this:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  volatile boolean keepRunning;
    descriptor: Z
    flags: ACC_VOLATILE
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;But can we see the actual assembly?&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;-XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly -Xcomp -XX:CompileCommand=compileonly,*Main&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;I got sidestepped while trying to see the assembly, had to follow this guide to allow the JVM to print the assembly: https://github.com/liuzhengyang/hsdis&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;...
0x0000000109ca4697: lock addl $0x0,(%rsp)     ;*putstatic keepRunning
                                                ; - test.Main::main@21 (line 17)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name></name></author><summary type="html">– Feel free to correct any inaccuracies –</summary></entry><entry><title type="html">Clojure, Emacs and Cider</title><link href="https://mping.github.io/clojure-emacs-cider/" rel="alternate" type="text/html" title="Clojure, Emacs and Cider" /><published>2015-11-17T21:10:31+00:00</published><updated>2015-11-17T21:10:31+00:00</updated><id>https://mping.github.io/clojure-emacs-cider</id><content type="html" xml:base="https://mping.github.io/clojure-emacs-cider/">&lt;p&gt;Or, what you need to know before diving into clojure’s REPL.&lt;/p&gt;

&lt;h1 id=&quot;intro&quot;&gt;Intro&lt;/h1&gt;

&lt;p&gt;I’ve been coming to clojure lately, and I finally decided to give emacs a go.
Unfortunately, setting up emacs for clojure is a PITA, for lots of reasons.&lt;/p&gt;

&lt;p&gt;Let’s try to shed some light on that.&lt;/p&gt;

&lt;h2 id=&quot;terminology&quot;&gt;Terminology&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Clojure&lt;/strong&gt;: a programming language from the lisp family. Plenty about that on the internet&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Leiningen&lt;/strong&gt;: a clojure runner, not unlike npm or maven or whatever. Actually its different because the config file is written in clojure itself. It’s basically a command-line utility to interact with clojure programs, which include launching a network repl, or &lt;strong&gt;nREPL&lt;/strong&gt;, for interacting with your clojure code&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Emacs&lt;/strong&gt;: an editor written in some kind of lisp. This is important, because you customize emacs by writing lisp (which is a different from clojure)
    &lt;ul&gt;
      &lt;li&gt;major modes: determines the editing behaviour of the current buffer. Only 1 active at any time&lt;/li&gt;
      &lt;li&gt;minor modes: alters behaviour in well-known way. Many active at same time&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;init.el&lt;/code&gt;: where the customization of emacs starts. Should be in &lt;code class=&quot;highlighter-rouge&quot;&gt;~/.emacs.d/init.el&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;Cider&lt;/strong&gt;: a set of &lt;a href=&quot;https://www.gnu.org/software/emacs/manual/html_node/emacs/Major-Modes.html&quot;&gt;emacs modes&lt;/a&gt; for working with clojure
Cider makes use of a lib called &lt;code class=&quot;highlighter-rouge&quot;&gt;org.clojure/tools.nrepl&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2 id=&quot;setting-up&quot;&gt;Setting up&lt;/h2&gt;

&lt;p&gt;There are already alot of tutorials on clojure and leiningen. We’re gonna dive into emacs and cider.
For emacs, I followed &lt;a href=&quot;http://www.braveclojure.com/basic-emacs/&quot;&gt;this guide&lt;/a&gt; (actually the braveclojure site is excellent for clojure also), but you will need to tweak some things to make sure everything runs smoothly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note 1:&lt;/strong&gt; the guide doesn’t have the link to the git repo that you should clone, right now its still online at https://github.com/flyingmachine/emacs-for-clojure so you should &lt;code class=&quot;highlighter-rouge&quot;&gt;git clone https://github.com/flyingmachine/emacs-for-clojure ~/.emacs.d&lt;/code&gt;. I’m guessing the authors are prepping for the book release so they changed the text to reflect that.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note 2:&lt;/strong&gt; When you clone that repo, you are actually getting some packages besides the configuration. If you &lt;code class=&quot;highlighter-rouge&quot;&gt;ls ~/.emacs.d/elpa&lt;/code&gt; you will see alot of folders.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note 3:&lt;/strong&gt; &lt;del&gt;Don’t forget to edit your  &lt;code class=&quot;highlighter-rouge&quot;&gt;~/.lein/profiles.clj&lt;/code&gt; and include cider-nrepl:&lt;/del&gt;&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;{:user {:plugins [[cider/cider-nrepl &quot;0.10.0-SNAPSHOT&quot;]}} ;; ignore, see Note 4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Note 4:&lt;/strong&gt; CIDER used to require you to keep its dependencies in your lein profiles. But CIDER and cider-nrepl have the same version numbers so its easy to keep them in sync. You are putting a hardcoded 0.10-snapshot but CIDER is 0.14.0 on melpa stable and 0.15 on melpa.
But CIDER now injects its dependencies so stating them is no longer required, but only serves to explicitly mismatch the frontend (CIDER) from the backend (cider-nrepl)&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h3 id=&quot;what-it-is-exactly-that-im-doing-here&quot;&gt;What it is exactly that I’m doing here?&lt;/h3&gt;

&lt;p&gt;You’re:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;using &lt;strong&gt;emacs&lt;/strong&gt; as an editor&lt;/li&gt;
  &lt;li&gt;using &lt;strong&gt;leiningen&lt;/strong&gt; with a plugin called &lt;code class=&quot;highlighter-rouge&quot;&gt;cider-nrepl&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;using an emacs package called &lt;strong&gt;cider&lt;/strong&gt;  to send clojure code to the repl and get results, &lt;em&gt;all from inside emacs&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I honestly don’t know exactly how it works  because I’m a clojure noob. This is how I &lt;em&gt;think&lt;/em&gt; it works.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;when you launch a repl (&lt;code class=&quot;highlighter-rouge&quot;&gt;lein repl&lt;/code&gt; for instance), and you have the &lt;code class=&quot;highlighter-rouge&quot;&gt;cider-nrepl&lt;/code&gt; plugin the plugin hooks onto the repl&lt;/li&gt;
  &lt;li&gt;when you call &lt;code class=&quot;highlighter-rouge&quot;&gt;cider-jack-in&lt;/code&gt; you are &lt;em&gt;launching a repl&lt;/em&gt; behind the scenes&lt;/li&gt;
  &lt;li&gt;cider is “just” for editing files (in emacs lingo, buffers)&lt;/li&gt;
  &lt;li&gt;cider-nrepl takes care of communicating with the repl, ie, sending clojure code from emacs to the repl and getting results, exceptions, etc&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Careful.
Emacs uses cider, which is a major-mode. Cider communicates with the nrepl, so cider and nrepl must be version-compatible.
You have to be careful with the cider and nrepl version you are using, otherwise you will have warnings and the repl may not work.
This means you have to pick cider from the proper repo; if you clone the emacs settings mentioned above, you are already getting a specific cider version&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h3 id=&quot;melpa-elpa-marmalade&quot;&gt;Melpa, Elpa, Marmalade&lt;/h3&gt;

&lt;p&gt;You can grab packages (plugins) for emacs from different repos. There’s Melpa, Elpa and Marmalade. &lt;a href=&quot;http://emacs.stackexchange.com/questions/268/what-are-the-practical-differences-between-the-various-emacs-package-repositorie&quot;&gt;Here’s&lt;/a&gt; a link explaining the differences. Make sure you grab cider from the proper repo, and that the cider (actually cider-nrepl) version matches with the cider-nrepl you configure in your leiningen.&lt;/p&gt;

&lt;p&gt;It took me a while to figure out that different repos have different versions (ex: stable vs snapshot).
What I actually did was remove all package folders from &lt;code class=&quot;highlighter-rouge&quot;&gt;~/.emacs.d/elpa&lt;/code&gt; and add all repos in &lt;code class=&quot;highlighter-rouge&quot;&gt;~/.emacs.d/init.el&lt;/code&gt;.
Emacs will install packages on the first run&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;;; Define package repositories
(require 'package)
(add-to-list 'package-archives
             '(&quot;elpa&quot; . &quot;https://elpa.gnu.org/&quot;))
(add-to-list 'package-archives
             '(&quot;marmalade&quot; . &quot;http://marmalade-repo.org/packages/&quot;) t)
(add-to-list 'package-archives
             '(&quot;tromey&quot; . &quot;http://tromey.com/elpa/&quot;) t)
(add-to-list 'package-archives
             '(&quot;melpa&quot; . &quot;http://melpa.milkbox.net/packages/&quot;) t)
(add-to-list 'package-archives
             '(&quot;melpa-stable&quot; . &quot;http://stable.melpa.org/packages/&quot;) t)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And below:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;;; The packages you want installed. You can also install these
;; manually with M-x package-install
;; Add in your own as you wish:
(defvar my-packages
 '(;; makes handling lisp expressions much, much easier
   ;; Cheatsheet: http://www.emacswiki.org/emacs/PareditCheatsheet
   paredit

   ;; key bindings and code colorization for Clojure
   ;; https://github.com/clojure-emacs/clojure-mode
   clojure-mode

   ;; extra syntax highlighting for clojure
   clojure-mode-extra-font-locking

   ;; integration with a Clojure REPL
   ;; https://github.com/clojure-emacs/cider
   cider
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h3 id=&quot;emacs-cider-repl&quot;&gt;Emacs, Cider, Repl&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;REPL: 
When you are using cider, you are actually sending code to the REPL for evaluation, and getting results back.
If you change a file, you have to either reload the classpath or send the code again&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s get this show on the road.&lt;/p&gt;

&lt;p&gt;Start a new project:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ lein new testdrive
$ ls testdrive
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You should see a bunch of files, which include &lt;code class=&quot;highlighter-rouge&quot;&gt;project.clj&lt;/code&gt; as well as &lt;code class=&quot;highlighter-rouge&quot;&gt;src&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;test&lt;/code&gt; folders.
There should be a &lt;code class=&quot;highlighter-rouge&quot;&gt;core.clj&lt;/code&gt; under &lt;code class=&quot;highlighter-rouge&quot;&gt;testdrive/core/&lt;/code&gt; that looks like this:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;(ns testdrive.core)
(defn foo
  &quot;I don't do a whole lot.&quot;
  [x]
  (println x &quot;Hello, World!&quot;))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Lets go to emacs:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ emacs .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Navigate to the &lt;code class=&quot;highlighter-rouge&quot;&gt;core.clj&lt;/code&gt; file with arrows and enter key. Now we want to fire a repl through cider.
Press &lt;code class=&quot;highlighter-rouge&quot;&gt;Alt+X&lt;/code&gt; (in emacs lingo, its &lt;code class=&quot;highlighter-rouge&quot;&gt;M-x&lt;/code&gt;), then write &lt;code class=&quot;highlighter-rouge&quot;&gt;cider-jack-in&lt;/code&gt; and then &lt;code class=&quot;highlighter-rouge&quot;&gt;ENTER&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The repl will start. Now you try to call the &lt;code class=&quot;highlighter-rouge&quot;&gt;foo function&lt;/code&gt; from within the repl:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;user&amp;gt; (in-ns 'testdrive.core)
#object[clojure.lang.Namespace 0x79a385c4 &quot;testdrive.core&quot;]
testdrive.core&amp;gt; (foo 1)
CompilerException java.lang.RuntimeException: Unable to resolve symbol: foo in this context, compiling:(/tmp/form-init5405367346234268941.clj:1:1) 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;WHAAAAAT? I hate you emacs/clojure/repl.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Note: please see the shortcuts at the end on how to navigate emacs, split the frame, etc.
Its much easier to split frame and switch between the source and the repl.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Not so fast&lt;/strong&gt;. When you fire the repl, it just spins a clojure repl, that’s all. You haven’t loaded anything yet (I mean, eval’d).
So now you want to load the &lt;code class=&quot;highlighter-rouge&quot;&gt;core.clj&lt;/code&gt; onto the repl. There’s a bunch of cider shortcuts for this, I do &lt;code class=&quot;highlighter-rouge&quot;&gt;Ctrl-c Ctrl-k&lt;/code&gt;  (again, in emacs lingo its &lt;code class=&quot;highlighter-rouge&quot;&gt;C-c C-k&lt;/code&gt;) (the Ctrl key is not released). The &lt;a href=&quot;https://github.com/clojure-emacs/cider&quot;&gt;cider readme&lt;/a&gt; states that this means “load (eval) the current buffer”.&lt;/p&gt;

&lt;p&gt;Now if you go to the repl again and try&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;testdrive.core&amp;gt; (foo 1)
1 Hello, World!
nil
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;YEEAAAH.&lt;/p&gt;

&lt;p&gt;If you don’t want to go to the repl, you can write this in  &lt;code class=&quot;highlighter-rouge&quot;&gt;core.clj&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;(foo 1)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And press &lt;code class=&quot;highlighter-rouge&quot;&gt;C-c C-e&lt;/code&gt;. You should see the result on the same line:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;(foo 1) =&amp;gt; nil
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now change the &lt;code class=&quot;highlighter-rouge&quot;&gt;foo&lt;/code&gt; function to “return” something (as you know, in clojure the last result is the return value).&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;(defn foo
  &quot;I don't do a whole lot.&quot;
  [x]
  (println x &quot;Hello, World!&quot;)
  333)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Try again with &lt;code class=&quot;highlighter-rouge&quot;&gt;C-c C-e&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;(foo 1) =&amp;gt; nil
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;WHAAAAAAT? FUUUUUUU!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Remember&lt;/strong&gt;: the repl has the old function definition. If you change the function, you have to eval it again.
Position the cursor at the end of the new definition for &lt;code class=&quot;highlighter-rouge&quot;&gt;foo&lt;/code&gt; and eval it through &lt;code class=&quot;highlighter-rouge&quot;&gt;C-c C-f&lt;/code&gt; (or any other way fwiw).
Now try to call foo again through &lt;code class=&quot;highlighter-rouge&quot;&gt;C-c C-e&lt;/code&gt; (so that it shows the result in our buffer):&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;(foo 1) =&amp;gt; 333
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;Note: when using cider, the position of the cursor is important to determine
what is going to be evaluated&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Basically, what this allows you is to load and run all the code that you want without ever leaving the repl (emacs in this case).
Cider has alot of goodies for interactive development:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;C-c ,&lt;/code&gt; for running tests for a given namespace (eg: if you’re in the &lt;code class=&quot;highlighter-rouge&quot;&gt;testdrive.core&lt;/code&gt; buffer/namespace, it will run &lt;code class=&quot;highlighter-rouge&quot;&gt;testdrive.core-test&lt;/code&gt; which correspondes to the file &lt;code class=&quot;highlighter-rouge&quot;&gt;testdrive/core_test.clj&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;We already seen &lt;code class=&quot;highlighter-rouge&quot;&gt;C-c C-e&lt;/code&gt; for eval’ing and displaying the results (kinda like lighttable)&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;C-c C-x&lt;/code&gt; for reloading the classpath (eg: you changed some code, and want to run tests)&lt;/li&gt;
  &lt;li&gt;and others&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;Caveat: if you add a dependency on project.clj, you have to restart cider 
because the classpath is defined only once at startup (its a java thing).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h3 id=&quot;shortcuts&quot;&gt;Shortcuts&lt;/h3&gt;

&lt;p&gt;I recommend the following pages:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://coderwall.com/p/ufflua/clojure-development-with-emacs-live-key-bindings&quot;&gt;https://coderwall.com/p/ufflua/clojure-development-with-emacs-live-key-bindings&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/clojure-emacs/cider&quot;&gt;https://github.com/clojure-emacs/cider&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Please kindly correct me if you find any bugs. You can raise an issue: &lt;a href=&quot;https://github.com/mping/mping.github.io/issues&quot;&gt;https://github.com/mping/mping.github.io/issues&lt;/a&gt;
And finally, stay tuned. I’ll try and post some more clojure interactive development goodies.&lt;/p&gt;</content><author><name></name></author><summary type="html">Or, what you need to know before diving into clojure’s REPL.</summary></entry><entry><title type="html">Clojurescript and nodejs</title><link href="https://mping.github.io/clojurescript-nodejs/" rel="alternate" type="text/html" title="Clojurescript and nodejs" /><published>2015-02-13T16:49:49+00:00</published><updated>2015-02-13T16:49:49+00:00</updated><id>https://mping.github.io/clojurescript-nodejs</id><content type="html" xml:base="https://mping.github.io/clojurescript-nodejs/">&lt;p&gt;Here’s a little snippet for driving express from clojurescript (run in cljs repl):&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;(ns express_sample
  (:require [cljs.nodejs :as node]))

(def express (node/require &quot;express&quot;))
(def app (express))

(defn -main [&amp;amp; args]
  (doto app
    (.get &quot;/&quot; (fn [req res]
                (.send res &quot;Hello World&quot;)))
    (.listen 3000)))

(-main)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name></name></author><summary type="html">Here’s a little snippet for driving express from clojurescript (run in cljs repl):</summary></entry><entry><title type="html">Docker experiences</title><link href="https://mping.github.io/docker-experiences/" rel="alternate" type="text/html" title="Docker experiences" /><published>2015-01-26T12:10:31+00:00</published><updated>2015-01-26T12:10:31+00:00</updated><id>https://mping.github.io/docker-experiences</id><content type="html" xml:base="https://mping.github.io/docker-experiences/">&lt;p&gt;Some thoughts on my experiences with docker.&lt;/p&gt;

&lt;h1 id=&quot;intro&quot;&gt;Intro&lt;/h1&gt;

&lt;p&gt;Lately my friend convinced me to enter the Docker bandwagon, and for the last few days I’ve read about most of the tools on Docker out there. Turns out I should have started from the basics, although &lt;a href=&quot;https://tutum.co&quot;&gt;tutum.co&lt;/a&gt; seems very nice, once it is production-ready. &lt;a href=&quot;http://fig.sh&quot;&gt;fig&lt;/a&gt; and &lt;a href=&quot;http://dokku.com&quot;&gt;dokku&lt;/a&gt; also look cool.&lt;/p&gt;

&lt;p&gt;But I started from scratch.&lt;/p&gt;

&lt;h2 id=&quot;docker&quot;&gt;Docker&lt;/h2&gt;

&lt;p&gt;Docker terminology is a PITA. Here’s my mental guide&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;images&lt;/strong&gt; are like vm images, which you customize mostly through a &lt;code class=&quot;highlighter-rouge&quot;&gt;Dockerfile&lt;/code&gt;
    &lt;ul&gt;
      &lt;li&gt;these are built in a tree-like, so docker reuses “checkpoints” for images that share something (eg: nginx and mongo based on ubuntu: ubuntu will be shared, so you just download ubuntu one time)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;images&lt;/strong&gt; are normally tagged for easy identification
    &lt;ul&gt;
      &lt;li&gt;
        &lt;table&gt;
          &lt;tbody&gt;
            &lt;tr&gt;
              &lt;td&gt;an example is the &lt;code class=&quot;highlighter-rouge&quot;&gt;dockerfile/java&lt;/code&gt; image that has tags for (oracle&lt;/td&gt;
              &lt;td&gt;open)jdk6/7/8&lt;/td&gt;
            &lt;/tr&gt;
          &lt;/tbody&gt;
        &lt;/table&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;containers&lt;/strong&gt; are processes that run images&lt;/li&gt;
  &lt;li&gt;you can run &lt;strong&gt;containers&lt;/strong&gt; in daemon mode, but for getting started I found it’s easier to run and &lt;code class=&quot;highlighter-rouge&quot;&gt;Ctrl+C&lt;/code&gt; to quit it, otherwise you’ll have a bunch of running &lt;strong&gt;containers&lt;/strong&gt; without noticing&lt;/li&gt;
  &lt;li&gt;you should read the Docker docs carefully.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;why-docker&quot;&gt;Why Docker&lt;/h3&gt;
&lt;p&gt;You’ll get a better answer elswhere, but for me it’s:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;isolation (and security, and multiple versions etc etc)&lt;/li&gt;
  &lt;li&gt;easy replicability(is this an english word?) of images across machines&lt;/li&gt;
  &lt;li&gt;scalability is nice, but not for small apps like mine
    &lt;ul&gt;
      &lt;li&gt;frameworks like &lt;a href=&quot;http://deis.io&quot;&gt;deis&lt;/a&gt;, &lt;a href=&quot;https://flynn.io/&quot;&gt;flynn.io&lt;/a&gt;, etc promess to easily scale a cluster up and down through the use of technologies such as docker&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;docker-concepts-and-tricks&quot;&gt;Docker concepts and tricks&lt;/h3&gt;

&lt;p&gt;####You should (generally speaking):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;either build an image from a &lt;code class=&quot;highlighter-rouge&quot;&gt;Dockerfile&lt;/code&gt;, or pull an image from docker index&lt;/li&gt;
  &lt;li&gt;use a container per service. Example:
    &lt;ul&gt;
      &lt;li&gt;1 container for mongo&lt;/li&gt;
      &lt;li&gt;1 for nginx&lt;/li&gt;
      &lt;li&gt;1 for java running tomcat or whatever&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;the rationale for this is, generally speaking, separation of concerns.&lt;/li&gt;
  &lt;li&gt;so, you should use a different image for each service; although I’m also running eg: wordpress from a single docker image (includes &lt;code class=&quot;highlighter-rouge&quot;&gt;wordpress + mysql&lt;/code&gt;). I’m a practical guy.&lt;/li&gt;
  &lt;li&gt;share data with docker volumes: &lt;code class=&quot;highlighter-rouge&quot;&gt;docker --v src_folder:target_folder_in_container&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;either link containers to expose eg: mongo to the main app container through &lt;code class=&quot;highlighter-rouge&quot;&gt;docker run --name src&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;docker run --link src:src&lt;/code&gt; or use some kind of Docker ambassador pattern
    &lt;ul&gt;
      &lt;li&gt;A stupid thing is that if you remove an image through its id, the name stays behind. So if you ran an image named &lt;code class=&quot;highlighter-rouge&quot;&gt;web&lt;/code&gt;and stopped the container, you  will have to do &lt;code class=&quot;highlighter-rouge&quot;&gt;docker rm web&lt;/code&gt; before starting another container with the same name&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;docker run -d&lt;/code&gt; dameonizes the container, I only pass the &lt;code class=&quot;highlighter-rouge&quot;&gt;-d&lt;/code&gt; flag when I’m sure that the command is correct&lt;/li&gt;
  &lt;li&gt;If you’re running docker through eg: git hooks, don’t use &lt;code class=&quot;highlighter-rouge&quot;&gt;docker -t&lt;/code&gt;, otherwise the script won’t wait for the docker command to finish&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;####You can (you should know this):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;enter a running container through &lt;code class=&quot;highlighter-rouge&quot;&gt;docker exec -t -i CONTAINER_ID /bin/bash&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;enter a stopped container through &lt;code class=&quot;highlighter-rouge&quot;&gt;docker run ... --entrypoint=/bin/bash ...&lt;/code&gt;, regardless of the containers &lt;code class=&quot;highlighter-rouge&quot;&gt;CMD&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;ENTRYPOINT&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;parse &lt;code class=&quot;highlighter-rouge&quot;&gt;docker inspect&lt;/code&gt;with &lt;a href=&quot;http://stedolan.github.io/jq/manual/&quot;&gt;jq&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;deploying-an-app&quot;&gt;Deploying an app&lt;/h3&gt;

&lt;p&gt;For this example I’ll be using CoreOS on Digital Ocean.
Here’s my referrer if you want to give it a try: &lt;a href=&quot;https://www.digitalocean.com/?refcode=1bfa1739fe3c&quot;&gt;https://www.digitalocean.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My app consists of&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;a mongodb image&lt;/li&gt;
  &lt;li&gt;an nginx image&lt;/li&gt;
  &lt;li&gt;a clojure image, this is essentially an image to run a fatjar&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Additionally, I needed&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;a socat image&lt;/li&gt;
  &lt;li&gt;a lein image&lt;/li&gt;
  &lt;li&gt;careful, see &lt;a href=&quot;http://www.martinklepsch.org/posts/running-a-clojure-uberjar-inside-docker.html&quot;&gt;http://www.martinklepsch.org/posts/running-a-clojure-uberjar-inside-docker.html&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;the lein image would later become a leiningen/node/bower/maven image, because my app is clojure on the backend, angular on the frontend with a node/bower toolchain to build the js.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s get these points one by one.&lt;/p&gt;

&lt;h3 id=&quot;mongodb&quot;&gt;MongoDB&lt;/h3&gt;

&lt;p&gt;We want to run mongodb, but we want the data out of the container. For that, we’ll use docker volumes.
We also want to expose the mongod service to other containers. For that, we’ll use container linking.
Your preferred image should expose a volume, check the image’s Dockerfile:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;VOLUME [&quot;/data/db&quot;]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;By now you should either have pulled a mongo image or created your own Dockerfile and an image based on it.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker run -P -v /home/mongo/data:/data/db --name mongo some/mongo
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If your dockerfile has &lt;code class=&quot;highlighter-rouge&quot;&gt;EXPOSE&lt;/code&gt; instructions, the &lt;code class=&quot;highlighter-rouge&quot;&gt;-P&lt;/code&gt; will randomly expose the ports on the &lt;strong&gt;host&lt;/strong&gt; container.
Don’t worry you only need to know the port if you want to connect to it.
If you’re running the latest docker, you can enter this container with &lt;code class=&quot;highlighter-rouge&quot;&gt;docker exec&lt;/code&gt; and inspect the db. Otherwise, you can try some tutorials to set up &lt;code class=&quot;highlighter-rouge&quot;&gt;nsenter&lt;/code&gt; or simply connect through the exposed ports:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;3ce6046e2ee4        mping/mongo:latest        &quot;mongod&quot;               44 hours ago        Up 44 hours         0.0.0.0:49155-&amp;gt;28017/tcp, 0.0.0.0:49156-&amp;gt;27017/tcp   mongo
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In this example you have local ports &lt;code class=&quot;highlighter-rouge&quot;&gt;49155, 49156&lt;/code&gt; binded to the container’s &lt;code class=&quot;highlighter-rouge&quot;&gt;28017,27017&lt;/code&gt;ports which are mongo ports. I’m guessing the docker way would be to launch another mongo container and connect to the server, but I just use &lt;code class=&quot;highlighter-rouge&quot;&gt;docker exec -it 3ce6046e2ee4 /bin/bash&lt;/code&gt; and voilá.&lt;/p&gt;

&lt;h3 id=&quot;app&quot;&gt;App&lt;/h3&gt;

&lt;p&gt;Assuming we have a docker image for our app (more on this later), we just need to run it and instruct docker to use the mongo service. Notice that the &lt;code class=&quot;highlighter-rouge&quot;&gt;DATABASE_URL&lt;/code&gt; is set to an address called &lt;code class=&quot;highlighter-rouge&quot;&gt;mongo&lt;/code&gt;. This will work because when you link docker containers, the &lt;code class=&quot;highlighter-rouge&quot;&gt;/etc/hosts&lt;/code&gt; file has an entry that sets the proper ip for the &lt;code class=&quot;highlighter-rouge&quot;&gt;name&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; docker run -p 3000:3000 \
	--name web \
	--link mongo:mongo \
	-h mysite.com \
	-e DATABASE_URL=mongodb://mongo:27017/myapp \
	-e ENV=prod \
	mydocker/image
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;nginx&quot;&gt;nginx&lt;/h3&gt;

&lt;p&gt;Assuming you have a proper nginx image, you just need to run it and link it to the conf files.
The interesting bit is setting an upstream to &lt;code class=&quot;highlighter-rouge&quot;&gt;web&lt;/code&gt; in &lt;code class=&quot;highlighter-rouge&quot;&gt;sites-available&lt;/code&gt;; remember that when you link containers, docker will update the container’s &lt;code class=&quot;highlighter-rouge&quot;&gt;/etc/hosts&lt;/code&gt; with the ip for the linked container. So &lt;code class=&quot;highlighter-rouge&quot;&gt;web&lt;/code&gt; will resolve to the running container named &lt;code class=&quot;highlighter-rouge&quot;&gt;web&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;upstream app_server {
		server web:3000;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Afterwards, just run it:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker run -d -h mysite.com -p 8888:80 -p 4443:443 \
	--link web:web \
	-v `pwd`/nginx/sites-enabled:/etc/nginx/sites-enabled \
	-v `pwd`/nginx/certs:/etc/nginx/certs \
	-v `pwd`/nginx/logs:/var/log/nginx \
	local/nginx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;What’s all this hassle for? Well, for the most of it, you get to have docker images that are ready to run and are not specifically configured to a particular configuration. You can spin another mongo or nginx and use it in different projects, as long as the volumes and links are correct.&lt;/p&gt;

&lt;h2 id=&quot;coreos&quot;&gt;CoreOS&lt;/h2&gt;

&lt;p&gt;CoreOS on DO has a fedora image that you can run, which has most of linux utilities (eg: nano).
It’s available on &lt;code class=&quot;highlighter-rouge&quot;&gt;/usr/bin/toolbox&lt;/code&gt; and it mounts the whole drive as a docker volume under &lt;code class=&quot;highlighter-rouge&quot;&gt;/media/root&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Docs are here: https://coreos.com/docs/cluster-management/debugging/install-debugging-tools/&lt;/p&gt;

&lt;h2 id=&quot;clojure&quot;&gt;Clojure&lt;/h2&gt;

&lt;p&gt;I’m experimenting with &lt;code class=&quot;highlighter-rouge&quot;&gt;lein uberimage&lt;/code&gt;, a plugin that generates a docker image for your uberjar.
The main advantage of having an image, instead of building a jar and generically running it, is that you get your own app image that can be pushed to a repo, rolled back, etc.&lt;/p&gt;

&lt;p&gt;Unfortunately, I hit alot of roadblocks:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;uberimage&lt;/code&gt; plugin expects a docker API on localhost:2375; on my dev machine (ubuntu), I had to update the docker daemon to run on &lt;code class=&quot;highlighter-rouge&quot;&gt;127.0.0.1&lt;/code&gt; aside with the unix socket. Careful not to expose the docker api on all interfaces (don’t use &lt;code class=&quot;highlighter-rouge&quot;&gt;0.0.0.0&lt;/code&gt;)&lt;/p&gt;

    &lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; less /etc/init/docker
 ...
 DOCKER_OPTS=&quot;--host=tcp://localhost:2375 --host=unix:///var/run/docker.sock&quot;
 ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;If you’re building an image from a container (CoreOS forces you to do everything from a container), you’ll need to communicate with the host docker api to let the leiningen plugin push the image. I had to install a &lt;code class=&quot;highlighter-rouge&quot;&gt;socat&lt;/code&gt; image and expose it with a name.
This is actually a good practice that allows you to expose the host docker api to a give container, either by exposing the socket through volumes or the http api through the named container &lt;code class=&quot;highlighter-rouge&quot;&gt;docker-http&lt;/code&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;docker &lt;a href=&quot;https://github.com/sequenceiq/docker-socat/blob/master/start&quot;&gt;socat&lt;/a&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;run the image in the background&lt;/p&gt;

    &lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; docker run -d -p 2375:2375 --volume=/var/run/docker.sock:/var/run/docker.sock --name=docker-http someimageof/lein
 ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;when specifying the docker api, use the named docker:
     docker run … someimageof/lein uberimage -H http://docker-http:2375 -t mydocker/image&lt;/p&gt;

    &lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; docker run -i -t --link docker-http:docker-http -v /var/run/docker.sock:/tmp/docker.sock:rw -v /home/core/ezcode.deploy:/project someimageof/lein uberimage -H http://docker-http:2375 -b someimageof/jdk8 -t mydocker/image
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;but-but&quot;&gt;But… But…&lt;/h3&gt;
&lt;p&gt;I lost the whole weekend trying to run a &lt;code class=&quot;highlighter-rouge&quot;&gt;!&quot;#&amp;amp;&quot;@&lt;/code&gt; jar. The fix that worked for me was this:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;java -Xmx512m -noverify -jar -Djava.security.egd=file:/dev/urandom target/myjar-standalone.jar
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So how did I got there? First I used &lt;code class=&quot;highlighter-rouge&quot;&gt;jps&lt;/code&gt; to see where the launching was being stuck:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;jps -l
15990 sun.tools.jps.Jps
15980 target/ezcode-0.1.0-standalone.jar
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then I used &lt;code class=&quot;highlighter-rouge&quot;&gt;jstack&lt;/code&gt; to inspect the stack of the running java application:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;jstack 15980
	...
	java.lang.Thread.State: RUNNABLE
		at java.io.FileInputStream.readBytes(Native Method)
		at java.io.FileInputStream.read(FileInputStream.java:220)
		at sun.security.provider.SeedGenerator$URLSeedGenerator.getSeedBytes(SeedGenerator.java:493)
		at sun.security.provider.SeedGenerator.generateSeed(SeedGenerator.java:117)
		at sun.security.provider.SecureRandom.engineGenerateSeed(SecureRandom.java:114)
		at sun.security.provider.SecureRandom.engineNextBytes(SecureRandom.java:171)
		- locked &amp;lt;0x00000007d8b0ee08&amp;gt; (a sun.security.provider.SecureRandom)
		at java.security.SecureRandom.nextBytes(SecureRandom.java:433)
		- locked &amp;lt;0x00000007d8b0ee60&amp;gt; (a java.security.SecureRandom)
		at java.security.SecureRandom.next(SecureRandom.java:455)
		at java.util.Random.nextInt(Random.java:189)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Some thread on some &lt;code class=&quot;highlighter-rouge&quot;&gt;secureRandom&lt;/code&gt; call or whatever caught my eye. Googling about it, I found this: http://docs.codehaus.org/display/JETTY/Connectors+slow+to+startup which had the workaround at the bottom:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Set the system property: -Djava.security.egd=file:/dev/urandom
Make sure that the java.security file contains this setting:
	securerandom.source=file:/dev/urandom
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And this only worked on jdk8. I couldn’t even apply this in jdk7.&lt;/p&gt;</content><author><name></name></author><summary type="html">Some thoughts on my experiences with docker.</summary></entry></feed>