LXC/LXD for Rails development

Beyond Docker

Docker is well known and these days the most common solution for application delivery process. And plenty of recipies for dockerizing a Rails application exist all over the internet.

But what to do in case of we need to separate our development environment in a different way. Let's say we would like to place our application and database within a kind of virtual server environment and have direct access to operating system.

Here comes on the stage LXC container and its system container manager LXD.

Go with LXD

Here it is a simple workflow of setup Rails and PostgreSQL on Ubuntu with LXD.

LXD setup

Install LXD

host$ sudo apt install lxd lxd-client

Add user into lxd group

host$ sudo usermod --append --groups lxd $USER

Initial config

host$ sudo lxd init

Create and launch a LXC container

host$ lxc launch ubuntu:16.04 myapp-container

Check the container exists

host$ lxc list

Go to the conttainer shell

host$ lxc exec myapp-container -- sudo --login --user ubuntu
The first -- string denotes that the command parameters for lxc should stop there, and the rest of the line will be passed as the command to be executed inside the container. The command is sudo --login --user ubuntu, which provides a login shell for the preconfigured account ubuntu inside the container.

Install the application environment

Refresh packages system

container$ sudo apt update
container$ sudo apt upgrade

Install requirements

container$ sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev

Install PostgreSQL

container$ sudo apt install postgresql postgresql-contrib libpq-dev
container$ sudo -u postgres createuser --interactive --pwprompt

Install RVM and Ruby

container$ gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
container$ curl -sSL https://get.rvm.io | bash -s stable
container$ source /home/ubuntu/.rvm/scripts/rvm
container$ rvm list known
container$ rvm install 2.5.6
container$ rvm --default use 2.5.6
container$ rvm list

Install bundler gem

container$ gem install bundler

Install JS runtime environment

container$ curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
container$ sudo apt-get install -y nodejs

Install Rails

container$ gem install rails

Bootstrap new Rails application

Create, setup and run the application

container$ rails new myapp -d postgresql
container$ cd myapp
container$ vim config/database.yml
container$ rails s

Map application directory into container

host$ lxc config device add myapp-container myapp-disk disk source=/myapp path=/home/ubuntu/myapp
host$ lxc config device add myapp-container myapp-disk disk path=/var/www/html source=/home/$USER/Development/lxdtest/public/

Comments