Starter files for running a sinatra app in docker. Great for quick json apis.
$ brew install docker
My preferred way of running docker on OS X is with boot2docker-xhyve.
$ brew install xhyve
$ git clone https://github.com/ailispaw/boot2docker-xhyve
$ cd boot2docker-xhyve
$ make
Though this is a single container application, Docker Compose makes it easy to start and stop docker containers with a shared volume, a specified port, and environment variables.
curl -L https://github.com/docker/compose/releases/download/1.4.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
docker-compose build
docker-compose up
Your server is now running on port 3000 of your docker ip which you can get from running
make ip
from within the boot2docker-xhyve
directory. It will probably look something
like http://192.168.64.2:3000/users
.
docker-compose stop
The rerun
gem is
supposed to help with changes automatically being reloaded
but I don't think it is working the way it should. The main server.rb
file is
using Sinatra Reloader so any changes
to server.rb
should get picked up. But changes to other files will require a
docker-compose stop
and docker-compose start
which is not ideal.
Because the server is running on the vm, the docker ip is only visible to your machine which means the simulator can talk to it but your external device can not. Luckily it is not to hard to port-forward on mac osx.
- Turn on Remote Login in System Preferences -> Sharing
- Run the command below replacing the first ip address with your own (192.168.0.102) and the second ip address with your docker ip (192.168.64.21).
ssh -L 192.168.0.102:3000:192.168.64.2:3000 -N 127.0.0.1
The command starts a process that forwards the port. Once you kill the process the port is no longer forwarded which is nice.