Nick Li

A blogging about node.js unix C ruby mongodb for hackers.

Vagrant Docker Ssh

| Comments

Vagrant Docker

If you are build a saas, using VMs and management tools. You will find vagrant is useful for additional features.

But Virtual machines take too much time to load. Now there is a new trending called using docker. Docker is written in go, if you haven’t heard of, you should probably go to check it out. In this article I am going to run a docker container in vagrant virtual machine

What is vagrant

Vagrant is a tool for building complete development environments. With an easy-to-use workflow and focus on automation, Vagrant lowers development environment setup time, increases development/production parity, and makes the “works on my machine” excuse a relic of the past.

you can also think it as a VM without the GUI. At its core, Vagrant is a simple wrapper around Virtualbox/VMware.

A few interesting features:

  • Boatloads of existing images, just check Vagrantbox.es for example.
  • Snapshot and package your current machine to a Vagrant box file (and, consequently, share it back).
  • Ability to fine tune settings of the VM, including things like RAM, CPU, APIC…
  • Vagrantfiles. This allows you to setup your box on init: installing packages, modifying configuration, moving code around…
  • Integration with CM tools like Puppet, Chef and Ansible.

Install vagrant

Check the docs

Docker

Docker is a Linux container, based on lxc (self-described as “chroot on steroids”) and AUFS. Instead of providing a full VM, like you get with Vagrant, Docker provides you lightweight containers, that share the same kernel and allow to safely execute independent processes.

Docker is attractive for many reasons:

  • Lightweight; images are much lighter than full VMs, and spinning off a new instance is lightning fast (in the range of seconds instead of minutes).
  • Version control of the images, which makes it much more convenient to handle builds.
  • Lots of images (again), just have a look at the docker public index of images.

Now Let’s get started.

  • You should have virtualbox and vagrant ready.
  • download an image
1
2
3
$ vagrant init precise64 http://cloud-images.ubuntu.com/vagrant/precise/current/precise-server-cloudimg-amd64-vagrant-disk1.box
$ vagrant up
$ vagrant ssh
  • you are all done
  • There’s a 4 if you want to access your (soon to be) deployed app; you will need to dig around the Vagrant documentation to perform port forwarding, proper networking and update manually your Vagrantfile

in virtualbox now

1
2
3
4
5
6
7
 $ sudo apt-get update
 $ sudo apt-get install linux-image-generic-lts-raring linux-headers-generic-lts-raring
 $ sudo reboot
 $ sudo sh -c "curl https://get.docker.io/gpg | apt-key add -"
 $ sudo sh -c "echo deb http://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list"
 $ sudo apt-get update
 $ sudo apt-get install lxc-docker
  • verity it worked by trying to build your first container:

$ sudo docker run -i -t ubuntu /bin/bash

  • let’s create a Dockerfile to build a new image
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
FROM ubuntu
 MAINTAINER My Self me@example.com

 # Update apt sources list to fetch mongodb and a few key packages
 RUN echo "deb http://archive.ubuntu.com/ubuntu precise universe" >> /etc/apt/sources.list
 RUN apt-get update
 RUN apt-get install -y python git

 # Finally - we wanna be able to SSH in
 RUN apt-get install -y openssh-server
 RUN mkdir /var/run/sshd
 RUN echo 'root:screencast' | chpasswd

 # And we want our SSH key to be added
 RUN mkdir /root/.ssh && chmod 700 /root/.ssh
 ADD id_rsa.pub /root/.ssh/authorized_keys
 ADD id_rsa.pub /root/.ssh/id_rsa.pub
 ADD id_rsa /root/.ssh/id_rsa
 RUN chmod 400 /root/.ssh/authorized_keys && chown root:root /root/.ssh/* && chmod 600 /root/.ssh/*

 # Expose a bunch of ports .. 22 for SSH and 3000 for our node app
 EXPOSE 22 3000

 CMD /usr/sbin/sshd -D
  • get ssh-key
1
2
3
$ ssh-keygen
$ cp -a /home/vagrant/.ssh/id_rsa.pub .
$ cp -a /home/vagrant/.ssh/id_rsa .
  • Now build it, more info about ssh into docker can be found here

sudo docker build -t eg_sshd .

Then run it. You can then use docker port to find out what host port the container’s port 22 is mapped to:

1
2
3
$ sudo docker run -d -P --name test_sshd eg_sshd
$ sudo docker port test_sshd 22
# 0.0.0.0:49153

And now you can ssh to port 49153 on the Docker daemon’s host IP address (ip address or ifconfig can tell you that):

1
2
$ ssh root@127.0.0.1 -p 49153
$$

Now you are in a docker container. Yeah! Finally, clean up after your test by stopping and removing the container, and then removing the image.

1
2
3
$ sudo docker stop test_sshd
$ sudo docker rm test_sshd
$ sudo docker rmi eg_sshd

Let’s wrap it up

So we just saw (roughly) how these tools can be used, and how they can be complementary:

Vagrant will provide you with a full VM, including the OS. It’s great at providing you a Linux environment for example when you’re on MacOS. Docker is a lightweight VM of some sort. It will allow you to build contained architectures faster and cheaper than with Vagrant.

It takes a bit of reading to get more familiar with these tools, this kind of technology allows you to automate and commoditize huge parts of your development and ops workflows. I strongly encourage you to make that investment. It has helped me tremendously increase the pace and quality of my throughput.

Git Cheatsheet

| Comments

Setup

git clone <repo>

clone the repository specified by ; this is similar to “checkout” in some other version control systems such as Subversion and CVS

Add colors to your ~/.gitconfig file:

[color]
  ui = auto
[color "branch"]
  current = yellow reverse
  local = yellow
  remote = green
[color "diff"]
  meta = yellow bold
  frag = magenta bold
  old = red bold
  new = green bold
[color "status"]
  added = green
  changed = yellow
  untracked = red

Highlight whitespace in diffs

[color]
  ui = true
[color "diff"]
  whitespace = red reverse
[core]
  whitespace=fix,-indent-with-non-tab,trailing-space,cr-at-eol

Setup Private Git Server

| Comments

  1. Install Git
1
2
3
ssh myserver.com
sudo apt-get update
sudo apt-get install git-core
  1. Setup git user
1
2
3
4
5
6
sudo adduser git
sudo mkdir /home/git/.ssh
sudo cp ~/.ssh/authorized_keys /home/git/.ssh/
sudo chown -R git:git /home/git/.ssh
sudo chmod 700 !$
sudo chmod 600 /home/git/.ssh/*

Vim Cheatsheet

| Comments

Cursor movement

  • h - move left
  • j - move down
  • k - move up
  • l - move right
  • w - jump by start of words (punctuation considered words)
  • W - jump by words (spaces separate words)
  • e - jump to end of words (punctuation considered words)
  • E - jump to end of words (no punctuation)
  • b - jump backward by words (punctuation considered words)
  • B - jump backward by words (no punctuation)
  • 0 - (zero) start of line
  • ^ - first non-blank character of line
  • $ - end of line
  • G - Go To command (prefix with number - 5G goes to line 5)

Mongodb Document Size Limit

| Comments

As a MongoDB beginner
When designing a database for large quantities of data
Then I should be aware of the document size 16MB limit in BSON format

The Document limit size is 16MB unless you’re using GridFS (but it seems like if you can’t get GridFS from 3rd parties like MongoHQ or MongoLab?). Remember that MongoDB by default puts the working set of your database in RAM.

So if you expect to have millions of some things then you better put those things in a separate collection, even if semantically it may make more sense to embed them in another document. If you’re using mongoid that simply means having a standalone model referencing another model.

Useful discussions on Stack Overflow:

reference

How This Blog Came to Be

| Comments

Since everyone have a blog, then I started one,

This blog is useing Octopress which is baseed on jekyll

It’s pretty easy to get started. Now let’s set it up.

Before You Begin

If ‘ruby –version’ doesn’t say you’re using Ruby 1.9.3, revisit your rbenv or RVM installation

Setup Octopress

1
2
3
git clone git://github.com/imathis/octopress.git octopress
cd octopress    # If you use RVM, You’ll be asked if you trust the .rvmrc file (say yes).
ruby --version  # Should report Ruby 1.9.3

Next, install dependencies.

1
2
3
4
gem install bundler
rbenv rehash # If you use rbenv, rehash to be able to run the bundle command
rvm rehash # If you use rvm
bundle install

Install the default Octopress theme.

1
rake install

Deploying

Github Pages

Hosting your blog with Github’s Pages service is free and allows custon domains

Check how to deploy to github pages

Configuring Octopress

Change your blog in _config.yml or check Octopress configuring