continue to last blog http://allenwei.cn/setup-rails-development-environment-with-docker/

Beside postgresql, we may need redis and memcache, it will become complex to start development environment

We'll talk about how to simplfy setup local environment with fig

Install Fig

To install fig, you need run follow command

curl -L https://github.com/docker/fig/releases/download/1.0.1/fig-`uname -s`-`uname -m` > /usr/local/bin/fig; chmod +x /usr/local/bin/fig  

Config Fig

You need create fig.yml in root path of your Rails project

rails:  
  build: .
  ports:
    - "3000:3000"
  volumes:
    - ./:/usr/src/app
  links:
    - "postgresql:postgresql"
postgresql:  
  image: postgres
  environment:
    POSTGRES_PASSWORD: "mypassword"

You can see fig support all configuration provided by docker

By simply add links you can specify the dependended container of your rails project

Build your container

before you start the rails container, we need build the image again, because fig will give the container a different name.

fig build  

tips: if you need more than one image want to build, fig will build all of them, very handy.

Start Rails container

because we've configured postgresql dependency in fig.yml, we can start all containers together

fig up  

Now you can see postgresql started along with Rails

You may notice your database is missing, because we are using different postgresql container now.

to create your database container is very simple

fig run rails db:create  

then migrations

fig run rails db:migrate  

to restart rails

fig restart rails  

to run some command in container

fig run rails bash  

what's more run your test

fig run rails rspec  

Add more service

rails:  
  build: .
  ports:
    - "3000:3000"
  volumes:
    - ./:/usr/src/app
  links:
    - "postgresql:postgresql"
    - "redis:redis"
    - "memcache:memcache"
postgresql:  
  image: postgres
  environment:
    POSTGRES_PASSWORD: "mypassword"
redis:  
  image: redis
memcache:  
  image: sylvainlasnier/memcached

you can get redis/memecache configuration by show environment variables in your rails container

fig run rails env  

you may noticed fig started redis and memcache

in next blog I'll talked about setup production environment locally with Vagrant and Docker