(Optional/Mac-OSX)
Install boot2docker
Docker runs in a VirtualBox VM through an image called boot2docker. The reason we have to use boot2docker and VirtualBox is because the Mac OSX filesystem is not compatible with the type of filesystem required to support Docker. Hence, we must run our Docker containers within yet another virtual machine.Download and install VirtualBox.
Now install boot2docker and Docker Compose.
$ brew install boot2docker docker-composeInitialize and start up boot2docker
$ boot2docker init $ boot2docker startConfigure your Docker host to point to your boot2docker image.
$ $(boot2docker shellinit)You’ll need to run this for every terminal session that invokes the docker or docker-compose command – better export this line into your .zshrc or .bashrc.
Install docker from the web
Install dockerInstall docker-compose from docker web
Install docker-composeCreate Dockerfile
Create on project root with following content# ./Dockerfile FROM ruby:2.2 MAINTAINER name@wearepeople.io ENV APP_PATH /app # This will be a host folder mounted to cache installed gems ENV BUNDLE_PATH /bundle # Install apt based dependencies required to run Rails as # well as RubyGems. As the Ruby image itself is based on a # Debian image, we use apt-get to install those. RUN apt-get update -qq && apt-get install -y build-essential libpq-dev RUN mkdir -p $APP_PATH # Expose port 3000 to the Docker host, so we can access it # from the outside. EXPOSE 3000 # Configure the main working directory. This is the base # directory used in any further RUN, COPY, and ENTRYPOINT # commands. WORKDIR $APP_PATH # Copy the main application. COPY . $APP_PATH # The main command to run when the container starts. Also CMD ./start
Create .dockerignore
Create .dockerignore file with following contents:.git* log/* tmp/* Dockerfile README.rdoc
Create de comopse config
Create docker-compose.yml file with following contents:# docker-compose.yml app: build: . command: "./start" volumes: - .:/app - .bundle:/bundle # gem cache folder ports: - "3000:3000" links: - db - mail - redis environment: BUNDLE_JOBS: 4 db: image: postgres ports: - "5432" redis: image; redis ports: - "6379" mail: image: schickling/mailcatcher ports: - "1080:1080"
Adapt application configurations accordingly:
- config/database.yml
development: &default adapter: postgresql encoding: unicode database: appname_development pool: 5 username: postgres password: host: db test: <<: *default database: appname_test
- ./start
#!/usr/bin/env bash echo "Removing old server pid's if any..." rm -f tmp/pids/server.pid echo "Checking bundle dependencies..." bundle check || bundle install echo "Booting up..." bundle exec rails s -p 3000 -b 0.0.0
- config/initializers/session_store.rb
redis_server = ENV['REDIS_PORT_6379_TCP_ADDR'] || 'localhost' # docker sets ENV, production uses localhost for now redis_port = ENV['REDIS_PORT_6379_TCP_PORT'] || '6379' Coaching::Application.config.session_store :redis_store, servers: "https://#{redis_server}:#{redis_port}/session"
Build containers
docker-compose build
Initialize Gems & Database
docker-compose run app bash -c 'bundle install && rake db:setup'
Run app
This will start app container (and dependencies) as a daemon, thus leaving the terminal freedocker-compose up -d
And this allows us to follow the process output/log as 'tail -f' or 'foreman start' would do
docker-compose logs