Build a zen command-line environment with git, tmux, oh-my-zsh, mosh (and docker)

TL;DR

This article aims at teaching how to install lovely command-line tools in a recoverable way.

You’ll be able to multiplex zsh terminals over a UDP connection (using mobile shell):

Zen Command-Line with tmux oh-my-zh

The reader is strongly encouraged to browse thoroughly the Dockerfile put in reference,
which steps summarize how to build the premises of such a text-only environment.

Disclaimer: To some extent, you might feel a bit dizzy because of the specially crafted mise en abyme.

The dizziness is a typical side effect of linux container abuse…

No worries, the feeling will just vanish with time (or you might just end up killing the wrong processes).

Dot files

Each and every user of linux distributions (or similarly flavoured operating systems)
might take a minute or two to acknowledge the significance of their own dot_files.

Even though they are hidden by design, I believe our productivity directly depends on the care they receive from us.

We all have heard (if not even worse) of hard drives just dying in some random boxes.

Still feeling a bit sceptical? Pretty numbers have been published on Backblaze blog just to satisfy our curiosity.

Being a big fan of upcycling doesn’t strictly imply there could be some happy ending for few choosen hard drives, anyway.

The bottom line is, the more precious and rapidly changing our data feel
and the more regular we shall have backup for them ready to be restored.

I insist on the latter part as knowing precisely
how to restore backups is the only way to really feel confident about them.

Let us see how to proceed in order to get things done i.e. hidden dot files safe.

Experimenting with Docker

The experiment is bootstrapped with the now classic installation of vagrant and virtual box.

Installing Virtual box

For instance, in a box running Wheezy 7.3, we would execute the commands:

# Add a GPG key downloaded from virtual box official website 
wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | sudo apt-key add -

# Download package lists from repositories
sudo apt-get update

# Install virtualbox
sudo apt-get install virtualbox-4.    

Binaries for other operating systems can be fetched from the official VirtualBox download page.

Installing Vagrant

The Vagrant download page offers 64-bit installation package for Debian:

# Install dependencies to share folders with NFS
sudo apt-get install nfs-kernel-server nfs-command

# Download Vagrant installation package 
wget https://dl.bintray.com/mitchellh/vagrant/vagrant_1.4.3_x86_64.deb -O /tmp/vagrant_1.4.3_x86_64.deb  

# Install Vagrant 
sudo dpkg -i /tmp/vagrant_1.4.3_x86_64.deb

Installing Docker

# Clone the linux container engine repository 
# (we assume Git has already been installed in the host)
git clone https://github.com/dotcloud/docker.git && cd docker

# Run Vagrant
vagrant up

# Access the vagrant box provided by DotCloud 
# to use docker from their official box
vagrant ssh

Customizing our shell

# Install git, vim and mobile shell in the vagrant box
sudo apt-get install git vim mosh

# Clone the repository containing a Dockerfile
git clone https://github.com/thierrymarianne/zen-cmd.git zen-cmd

# Build a container from the Dockerfile 
# and tag it with name "zen-cmd" if the build is successful
cd zen-cmd && docker build -t zen-cmd .  

From this point, one shall have received a positive message (Successfully built) accompanied by a pretty hash.

These 12 characters are to be kept preciously.

Believe it or not, we are done already here or better said,
our personal shell has been set up according to our container building script (Dockerfile).
The pretty hash identifies a docker image which can now be run in order to use our command-line interface.

In a nutshell,

  • Installing a custom-tailored command-line environment only took us the time of making some notes in the shape of a Dockerfile about what needs to be customized.
  • Executing a single command was enough to restore our command-line environment personalized over time

Fellows of little faith are absolutely right in showing doubts about this so let us run the interactive shell (within a container within a vagrant box),
in order to proceed, one just needs to execute the following commands

# Copy predefined SSH configuration from the article repository
cp -R ./ssh/* /home/vagrant/.ssh

# Start an ssh agent
ssh-agent /bin/bash

# Let our ssh agent handling a key allowed to access the container
# with passphrase being "elvish_word_for_friend"
# (One shall certainly generate its own pair of keys 
# using `ssh-keygen -f path_to_private_key` otherwise)
chmod 0400 ~/.ssh/zen-cmd && ssh-add ~/.ssh/zen-cmd

# Run openssh daemon from our brand new container
docker run -d -t zen-cmd /usr/sbin/sshd -D & export LC_ALL=en_US.UTF-8 && /usr/bin/mosh-server new -p 6000

# Save container id for inspection
CID=`docker ps | grep zen-cmd | cut -d ' ' -f 1 | head -n 1`; 

# Alias "zen-cmd" container ip address to "zen-cmd" host
sudo /bin/bash -c "echo `docker inspect -format '{{ .NetworkSettings.IPAddress }}' $CID`'    zen-cmd' >> /etc/hosts"

# Access our portable command-line environment using mosh-client
mosh zen-cmd

# In the container, run tmux to multiplex zsh terminals
tmux

Let us dive into the details of this automated script :

  • Our package lists are updated (the same way we did before Installing VirtualBox).
  • Packages needed to compile binaries are installed
  • Target directories are created respectively to
    • clone sources
    • install binaries from sources
  • Git is installed
  • Tmux, oh-my-zsh repositories are cloned
  • Tmux, oh-my-zsh are installed and configured
  • Zsh is set as default shell
  • Password authentication is disabled for ssh
  • Mosh server is installed
  • UTF-8 locale required to run mobile shell is generated
  • Privilege separation directory is created for ssh
  • Our vagrant ssh public key is added to authorized keys of our container
  • SSH and Mobile shell ports are opened

Leveraging git

Since we mostly deal with plain text files here, git appears to be a quite legitimate version control system.

Even GitHub made a point in popularizing the habit of sharing them (the dot_files).

What are the direct benefits coming out of it?
According to their unofficial guide,

  • boxes are kept in sync
  • technology watch becomes easier
  • knowledge is redistributed

End Of Line

I hope you have enjoyed this setup which has the clear advantages of being portable, testable and recoverable.

Syntax to write your own Dockerfile can be found in Docker official documentation.


You liked this article? You'd probably be a good match for our ever-growing tech team at Theodo.

Join Us

  • joffrey

    Cool article, happy to see the french tech scene get into docker!

    If I might, when you say “These 12 characters are to be kept preciously.” when talking about the hash, it would probably be easier to tag your image after it’s been built with “docker tag [hash] foobar”, so you can use “foobar” to run/manipulate it later. Or use the “-t” option of docker build :)

    Cheers!

  • thierrym

    Thank you so much Joffrey!
    I’ve updated the article to reflect the right way of using tags! Cheers!