Isolating Rails environment using Fig

I have stumbled upon a new tools called Fig. This is written by Docker to utilise container technology for isolating development environment (so you no longer need to install postgresql, nginx, etc on your own OS anymore).

Although the tutorial for developing Rails in Fig website is easy to follow for beginner, it has some big caveats that are not suited for development:

  • Each time you add a new gem, you have to build the image all over again, which is very time consuming.
  • If you run fig run rails generate model or something like that, the generated files are owned by root, which makes you have to run chown after that.

Using that tutorial as a base, I have create my own Dockerfile and fig.yml to solve those two problems.

fig.yml

db:
  image: postgres
  ports:
    - "5432"
data:
  build: backend
  command: bash
web:
  build: backend
  command: bundle exec rails server -p 3000
  volumes_from:
    - data
  volumes:
    - .:/home/deployer/app
  ports:
    - "3000:3000"
  links:
    - db

Dockerfile

FROM ruby:2.2
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN apt-get -qqy install sudo

# Create local user for development, remember to replace `1000` to your user's uid
RUN adduser --disabled-password --gecos '' --uid 1000 deployer
RUN adduser deployer sudo
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
USER deployer

# Create local bundle installation
RUN mkdir /home/deployer/.bundle
ENV GEM_HOME /home/deployer/.bundle
ENV BUNDLE_APP_CONFIG /home/deployer/.bundle
ENV PATH /home/deployer/.bundle/bin:$PATH

# Using a separate data volume to persist this on different `fig run`
VOLUME /home/deployer/.bundle

# Copy gem bundler to local installation
RUN echo "cp -r /usr/local/bundle/* /home/deployer/.bundle" >> /home/deployer/.bashrc

# Linking volumes makes `.bundle` owned by root so we need to fix this before each run command
RUN echo "sudo chown -R deployer:deployer /home/deployer/.bundle" >> /home/deployer/.bashrc

# Create a folder for syncing the app
RUN mkdir /home/deployer/app
WORKDIR /home/deployer/app

Now each time you want to run bundle install again, just execute this command:

fig run --rm web bundle install

Problems now solved:

  • Bundled gems are persisted between each run by using a separate container.
  • All commands are runned under deployer user, which has the same uid as your host user.

Clear input buffer

When programming, sometimes you will need to clear the input buffer to make sure there won’t be anything miscellaneous inside it. The most popular problem (I think) related to input buffer and input data is: fgets() function will not pause to let you enter data when it detects an ‘\n’ character inside the buffer (or stdin). But why is there a ‘\n’ char? Mostly because you have used scanf() (or some functions similar to it) right so it left ‘\n’ char.

And what is the solution? Well, we’ll create a function clear_buffer() to do it.

int clear_buffer() {
int ch;
while ((ch = getchar()) != '\n' && ch != EOF);
return 0;
}

(assumed you have included stdio.h in the main program for getchar() to work)
Now you can call this function everywhere you really need to flush the buffer. I said “really” because if there’s nothing in the buffer, the program will wait until there is ‘\n’ (i.e you have to press enter).

Credits to CProgramming.com Forum

SWT browser is not available

Phew! Then, this is my first post using this blogging system (I have been using WordPress self-hosted to build my website but haven’t used this WordPress.com to blog yet). Cheer!

Now, get to the point!

I’m using Eclipse PDT to learn PHP. In the past, I used ubuntu 10.10 and didn’t have a problem using the SWT browser integrated inside Eclipse. But, after changing to Natty (11.04), I realized this problem: “SWT browser is not available” when I want to view the webpage right inside Eclipse. Well, after googling for sometime, I found that it need at least 3 things to run:

  • Mozilla 1.4
  • GTK 2.*
  • XULRunner 1.7.x – 1.9.x

The first two conditions are satisfied out-of-the-box. But when I used Synaptic to search, in Natty, the version of XULRunner is 2.x, not 1.9.x. Fortunately, there also exists the package xulrunner-1.9.2 for you install. Just install it (then restart eclipse if it’s openning) and, voila => It’s surely available now, right?